How do I get narrow width and a large height on a

2019-07-03 22:15发布

问题:

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.