matplotlib.widgets.RadioButtons#

class matplotlib.widgets.RadioButtons(ax, labels, active=0, activecolor=None, *, layout=None, useblit=True, label_props=None, radio_props=None)[source]#

Bases: _Buttons

A GUI neutral radio button.

For the buttons to remain responsive you must keep a reference to this object.

Connect to the RadioButtons with the on_clicked method.

Attributes:
axAxes

The parent Axes for the widget.

activecolorcolor

The color of the selected button.

labelslist of Text

The button labels.

value_selectedstr

The label text of the currently selected button.

index_selectedint

The index of the selected button.

Add radio buttons to an Axes.

Parameters:
axAxes

The Axes to add the buttons to.

labelslist of str

The button labels.

activeint

The index of the initially selected button.

activecolorcolor

The color of the selected button. The default is 'blue' if not specified here or in radio_props.

layoutNone or "vertical" or "horizontal" or (int, int), default: None

The layout of the radio buttons. Options are:

  • None: Use legacy vertical layout (default).

  • "vertical": Arrange buttons in a single column with dynamic positioning based on text widths.

  • "horizontal": Arrange buttons in a single row with dynamic positioning based on text widths.

  • (rows, cols) tuple: Arrange buttons in a grid with the specified number of rows and columns. Buttons are placed left-to-right, top-to-bottom with dynamic positioning.

The layout options "vertical", "horizontal" and (rows, cols) create mtext.Text objects to determine exact text sizes, and then they are added to the Axes. This is usually okay, but may cause side-effects and has a slight performance impact. Therefore the default None value avoids this.

Added in version 3.11.

useblitbool, default: True

Use blitting for faster drawing if supported by the backend. See the tutorial Faster rendering by using blitting for details.

Added in version 3.7.

label_propsdict of lists, optional

Dictionary of Text properties to be used for the labels. Each dictionary value should be a list of at least a single element. If the list is of length M, its values are cycled such that the Nth label gets the (N mod M) property.

Added in version 3.7.

radio_propsdict, optional

Dictionary of scatter Collection properties to be used for the radio buttons. Defaults to (label font size / 2)**2 size, black edgecolor, and activecolor facecolor (when active).

Note

If a facecolor is supplied in radio_props, it will override activecolor. This may be used to provide an active color per button.

Added in version 3.7.

property activecolor#
clear()[source]#

Reset the active button to the initially active one.

set_active(index)[source]#

Select button with number index.

Callbacks will be triggered if eventson is True.

Parameters:
indexint

The index of the button to activate.

Raises:
ValueError

If the index is invalid.

set_radio_props(props)[source]#

Set properties of the Text labels.

Added in version 3.7.

Parameters:
propsdict

Dictionary of Collection properties to be used for the radio buttons.

Examples using matplotlib.widgets.RadioButtons#

Radio Buttons

Radio Buttons

Radio Buttons Grid

Radio Buttons Grid