How to set Matplotlib RadioButton radius using set_radius?

205 views Asked by At

I try to set RadioButtons circle radius. According to the below MWE, the buttons disappeared. However, removing the circ.set_radius(10) recovers the buttons. Using circ.height and circ.width recovers buttons and if done properly they are perfectly round. Any idea what causes inability to use 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()

Just to add: I use Python 3.6.8 (32bit edition) on Windows. Matplotlib 3.3.2.

1

There are 1 answers

2
scleronomic On BEST ANSWER

A few comments. If you create new axes, the default limits are (0, 1) for x and y. So if you create a circle with radius=10, you just can't see the circle. Try to set the radius to a smaller value (ie 0.1)

The other thing is that most of the time the aspect ratio of the x and the y axis are is not equal, this means that a circle looks like an ellipse. You have different options here, one is to use the keyword aspect='equal' or 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)

aspect='auto'

Another option is to use this answer and get the aspect ratio of the axis. With this you can adjust width and height as you did, but this way it is less guessing what the correct ratio is. The advantage of this method is, that you are more flexible with respect to the width and height of the axis, .

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

get_aspect()