如何使用 set_radius 设置 Matplotlib RadioButton 半径?
Posted
技术标签:
【中文标题】如何使用 set_radius 设置 Matplotlib RadioButton 半径?【英文标题】:How to set Matplotlib RadioButton radius using set_radius? 【发布时间】:2021-01-23 16:34:21 【问题描述】:我尝试设置 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.
【问题讨论】:
【参考方案1】:几厘米。
如果您创建新坐标区,则 x 和 y 的默认限制为 (0, 1)。
所以如果你创建一个半径=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 答案并获取轴的纵横比。有了这个,你可以像你一样调整宽度和高度,但这样就更少猜测正确的比例是多少。这种方法的优点是,您在轴的宽度和高度方面更加灵活。
def get_aspect_ratio(ax=None):
"""https://***.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
【讨论】:
据我了解,set_radius
具有承载 RadioButtons 的轴单位,我看不到按钮的唯一原因是它们在这些单位中的半径太大。很高兴知道。谢谢你:)
是的,您可以使用axlegend
和set_xlim(-11, 11)
来更好地了解发生了什么。以上是关于如何使用 set_radius 设置 Matplotlib RadioButton 半径?的主要内容,如果未能解决你的问题,请参考以下文章