如何在RadioButtons小部件上获得较窄的宽度和较大的高度,并且仍具有不重叠的圆形单选按钮? [英] How do I get narrow width and a large height on a RadioButtons widget, and still have round radio buttons that don't overlap?
问题描述
如何在 RadioButtons 小部件上获得窄的宽度和大的高度,并且仍然具有不重叠的圆形单选按钮?
How do I get narrow width and a large height on a RadioButtons widget, and still have round radio buttons that don't overlap?
plt.figure()
rax = plt.axes([0.1, 0.1, 0.6, 0.6], frameon=True ,aspect='equal')
labels = [str(i) for i in range(10)]
radios = RadioButtons(rax, labels)
for circle in radios.circles: # adjust radius here. The default is 0.05
circle.set_radius(0.02)
plt.show()
以上方法之所以有效,是因为它将轴实例的宽度和高度设置为0.6,但是我想将宽度设置为0.1,将高度设置为0.6:
The above works because it sets width and height of the axes instance to 0.6, but I want width to be, say, 0.1 and height to be 0.6:
plt.figure()
rax = plt.axes([0.1, 0.1, 0.1, 0.6], frameon=True, aspect='equal')
labels = [str(i) for i in range(10)]
radios = RadioButtons(rax, labels)
for circle in radios.circles: # adjust radius here. The default is 0.05
circle.set_radius(0.02)
plt.show()
这只会使结果超小,宽度为0.1乘高度为0.1(我想是因为正在使用aspect ='equal'.如果删除后者,则会得到以下结果:
This just makes the result super tiny, with width 0.1 by height 0.1 (I suppose because aspect='equal' is being used. If I remove the latter, I get this:
我问的原因是这些单选按钮将是右侧绘图的狭窄侧边栏.因此它应该窄而高.
The reason I ask is that these radio buttons will be a narrow sidebar to a plot on its right. So it should be narrow but tall.
推荐答案
您可以在创建 RadioButton
' 后使用 matplotlib 的属性更改圆圈的高度.patch.Circle
patch, height
:
You can alter the height of the circles after the creation of the RadioButton
', using the property of the matplotlib.patches.Circle
patch, height
:
import matplotlib.pyplot as plt
from matplotlib.widgets import RadioButtons
plt.figure()
rax = plt.axes([0.1, 0.1, 0.1, 0.6], frameon=True)
labels = [str(i) for i in range(10)]
radios = RadioButtons(rax, labels)
rpos = rax.get_position().get_points()
fh = fig.get_figheight()
fw = fig.get_figwidth()
rscale = (rpos[:,1].ptp() / rpos[:,0].ptp()) * (fh / fw)
for circ in radios.circles:
circ.height /= rscale
plt.show()
重要的是,此处我们没有将 aspect
设置为 equal
.相反,我们将人为地改变圆圈的高度.在上面的示例中,我通过使用 rax
轴的位置来计算缩放高度的大小.请注意,我们还需要考虑图形的长宽比.
Importantly, we don't set the aspect
to equal
here. Instead we are going to artificially change the height of the circles. In the example above, I calculate how much to scale the height by using the position of the rax
axes. Note we also need to account for the aspect ratio of the figure.
这篇关于如何在RadioButtons小部件上获得较窄的宽度和较大的高度,并且仍具有不重叠的圆形单选按钮?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!