Matplotlib - Radio Buttons - GeeksforGeeks

03 Jul, 2021
Radio buttons let the user choose only one option between multiple options. These buttons are arranged in groups of two or more with a list of circular dots.  For the radio buttons to remain responsive you must keep a reference to this object.  We connect the RadioButtons with the on_clicked method to make it responsive.
Syntax:
matplotlib.widgets.RadioButtons(ax, labels, active=0, activecolor=’blue’)
Parameters:
ax: The axes to which the radio buttons add.
labels: button labels(list of str).
active: Index of the initially selected button.
activecolor: Color of the selected button.
Below are various examples that depict how to create and use radio buttons using matplotlib library.
Example 1:
Python3
# import required modules as numpy,# matplotlib and radiobutton widgetimport numpy as npimport matplotlib.pyplot as pltfrom matplotlib.widgets import RadioButtons # x and y-coordinates for graph creationx = np.linspace(0, 2*np.pi, 200)y = np.cos(x**2) # Creating subplot and adjusting subplotfig, ax = plt.subplots()l, = ax.plot(x, y, color='yellow')plt.subplots_adjust(left=0.4)ax.set_title('Plot with RadioButtons',             fontsize=18) # sub-plot for radio button with# left, bottom, width, height valuesrax = plt.axes([0.1, 0.15, 0.2, 0.2])radio_button = RadioButtons(rax, ('yellow',                                  'red',                                  'blue',                                  'green')) # function performed on switching the# radiobuttonsdef colorfunc(label):    l.set_color(label)    plt.draw()  radio_button.on_clicked(colorfunc) plt.show()
Output:
Example 2:
Python3
import numpy as npimport matplotlib.pyplot as pltfrom matplotlib.widgets import RadioButtons # plotting between the interval -π and πx = np.linspace(-np.pi, np.pi) # trigonometric functions to plotp = 2*np.sin(x)q = np.sin(x)r = np.cos(x)s = 2*np.cos(x) fig, ax = plt.subplots()l, = ax.plot(x, p, lw=3, color='green')plt.subplots_adjust(left=0.3) rax = plt.axes([0.05, 0.7, 0.15, 0.2])radio = RadioButtons(rax, ('2sin(x)',                           'sin(x)',                           'cos(x)',                           '2cos(x)')) # function performed on clicking the radio buttonsdef sinefunc(label):    sindict = {'2sin(x)': p,               'sin(x)': q,               'cos(x)': r,               '2cos(x)': s}    data = sindict[label]    l.set_ydata(data)    plt.draw()  radio.on_clicked(sinefunc) # plot gridax.grid()plt.show()
Output:
Example 3: 
Python3
import numpy as npimport matplotlib.pyplot as pltfrom matplotlib.widgets import RadioButtons # plotting between the interval -π and πx = np.linspace(-np.pi, np.pi) # trigonometric functions to plotp = 2*np.sin(x)q = np.sin(x)r = np.cos(x)s = 2*np.cos(x)fig, ax = plt.subplots() l, = ax.plot(x, p, lw=3, color='red')plt.subplots_adjust(left=0.3) rax = plt.axes([0.05, 0.7, 0.15, 0.2])radio = RadioButtons(rax, ('2sin(x)',                           'sin(x)',                           'cos(x)',                           '2cos(x)')) # function performed on clicking the radio buttonsdef sinefunc(label):    sindict = {'2sin(x)': p,               'sin(x)': q,               'cos(x)': r,               '2cos(x)': s}    data = sindict[label]    l.set_ydata(data)    plt.draw()  radio.on_clicked(sinefunc) # plot gridax.grid() # x and y-coordinates for graph creationx = np.linspace(0, 2*np.pi, 200)y = np.cos(x**2) # sub-plot for radio button with# left, bottom, width, height valuesrax2 = plt.axes([0.05, 0.15, 0.15, 0.2])radio_button = RadioButtons(rax2, ('red',                                   'blue',                                   'green')) # function performed on switching radiobuttonsdef colorfunc(label2):    l.set_color(label2)    plt.draw()  radio_button.on_clicked(colorfunc) plt.show()
Output:
akshaysingh98088
Picked
Python-matplotlib
Python
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
How to Install PIP on Windows ?
How to drop one or multiple columns in Pandas Dataframe
How To Convert Python Dictionary To JSON?
Check if element exists in list in Python
Defaultdict in Python
Python | Get unique values from a list
Python | os.path.join() method
Selecting rows in pandas DataFrame based on conditions
Create a directory in Python
Python | Pandas dataframe.groupby()