How do I get narrow width and a large height on a RadioButtons widget, and still have round radio buttons that don't overlap?

荒凉一梦 提交于 2019-12-10 19:58:43

问题


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()

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()

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.


回答1:


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()

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.



来源:https://stackoverflow.com/questions/36520864/how-do-i-get-narrow-width-and-a-large-height-on-a-radiobuttons-widget-and-still

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!