我尝试设置RadioButtons圆半径。根据下面的MWE,按钮消失了。但是,移除circ.set_radius(10)将恢复按钮。使用circ.height和circ.width恢复按钮,如果操作得当,它们是完美的圆形。你知道是什么原因导致无法使用set_radius吗?
import matplotlib.pyplot as plt
from matplotlib.widgets import RadioButtons
buttonlist = ('at current position', 'over width around cur. pos.', 'at plots full range')
axradio = plt.axes([0.3, 0.3, 0.2, 0.2])
radios = RadioButtons(axradio, buttonlist)
for circ in radios.circles:
circ.set_radius(10)
plt.show()补充一下:我在Windows上使用的是Python 3.6.8 (32位版本)。Matplotlib 3.3.2。
发布于 2020-10-09 04:52:45
一些评论。如果你创建新的轴,默认的限制是x和y的(0,1)。因此,如果你用radius=10创建一个圆,你就看不到这个圆。尝试将半径设置为较小的值(即0.1)
另一件事是,大多数情况下,x轴和y轴的aspect ratio不相等,这意味着圆看起来像椭圆。这里有不同的选项,一种是使用关键字aspect='equal'或aspect=1
import matplotlib.pyplot as plt
from matplotlib.widgets import RadioButtons
buttonlist = ('at current position', 'over width around cur. pos.', 'at plots full range')
axradio = plt.axes([0.3, 0.3, 0.6, 0.2], aspect=1)
radios = RadioButtons(axradio, buttonlist)

另一种选择是使用this answer并获得轴的纵横比。有了这个,你可以像以前一样调整宽度和高度,但这样就不需要猜测正确的比率是什么了。这种方法的优点是,相对于轴的宽度和高度,您可以更加灵活。
def get_aspect_ratio(ax=None):
"""https://stackoverflow.com/questions/41597177/get-aspect-ratio-of-axes"""
if ax is None:
ax = plt.gca()
fig = ax.get_figure()
ll, ur = ax.get_position() * fig.get_size_inches()
width, height = ur - ll
return height / width
plt.figure()
axradio = plt.axes([0.3, 0.3, 0.6, 0.2])
radios = RadioButtons(axradio, buttonlist)
r = 0.2
for circ in radios.circles:
circ.width = r * get_aspect_ratio(axradio)
circ.height = r

https://stackoverflow.com/questions/64265736
复制相似问题