1

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 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

2 Comments

So as I understand, the set_radius has units of axes hosting the RadioButtons and the only reason I couldn't see the buttons was their way too large radius in these units. Good to know. Thank you :)
Yes, you can play around with axlegend and set_xlim(-11, 11)to better understand what is going on.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.