3

如何在 RadioButtons 小部件上获得较窄的宽度和较大的高度,并且仍然有不重叠的圆形单选按钮?

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:

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'。如果我删除后者,我会得到: 单选按钮不是圆形的,即使我将它们的半径设置为 0.02

我问的原因是这些单选按钮将是其右侧绘图的窄边栏。所以它应该是窄而高的。

4

1 回答 1

3

您可以在创建 ' 后更改圆圈的高度,使用补丁RadioButton的属性, :matplotlib.patches.Circleheight

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在上面的示例中,我通过使用轴的位置来计算缩放高度的多少。请注意,我们还需要考虑图形的纵横比。

在此处输入图像描述

于 2016-11-23T12:54:21.253 回答