Matplotlib – 单选按钮
单选按钮让用户只能在多个选项中选择一个选项。这些按钮以两个或更多为一组排列,并带有圆点列表。要使单选按钮保持响应,您必须保留对此对象的引用。我们使用 on_clicked 方法连接 RadioButtons 以使其响应。
句法:
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.
下面是描述如何使用 matplotlib 库创建和使用单选按钮的各种示例。
示例 1:
Python3
# import required modules as numpy,
# matplotlib and radiobutton widget
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.widgets import RadioButtons
# x and y-coordinates for graph creation
x = np.linspace(0, 2*np.pi, 200)
y = np.cos(x**2)
# Creating subplot and adjusting subplot
fig, 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 values
rax = plt.axes([0.1, 0.15, 0.2, 0.2])
radio_button = RadioButtons(rax, ('yellow',
'red',
'blue',
'green'))
# function performed on switching the
# radiobuttons
def colorfunc(label):
l.set_color(label)
plt.draw()
radio_button.on_clicked(colorfunc)
plt.show()
Python3
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.widgets import RadioButtons
# plotting between the interval -π and π
x = np.linspace(-np.pi, np.pi)
# trigonometric functions to plot
p = 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 buttons
def 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 grid
ax.grid()
plt.show()
Python3
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.widgets import RadioButtons
# plotting between the interval -π and π
x = np.linspace(-np.pi, np.pi)
# trigonometric functions to plot
p = 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 buttons
def 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 grid
ax.grid()
# x and y-coordinates for graph creation
x = np.linspace(0, 2*np.pi, 200)
y = np.cos(x**2)
# sub-plot for radio button with
# left, bottom, width, height values
rax2 = plt.axes([0.05, 0.15, 0.15, 0.2])
radio_button = RadioButtons(rax2, ('red',
'blue',
'green'))
# function performed on switching radiobuttons
def colorfunc(label2):
l.set_color(label2)
plt.draw()
radio_button.on_clicked(colorfunc)
plt.show()
输出:
示例 2:
蟒蛇3
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.widgets import RadioButtons
# plotting between the interval -π and π
x = np.linspace(-np.pi, np.pi)
# trigonometric functions to plot
p = 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 buttons
def 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 grid
ax.grid()
plt.show()
输出:
示例 3:
蟒蛇3
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.widgets import RadioButtons
# plotting between the interval -π and π
x = np.linspace(-np.pi, np.pi)
# trigonometric functions to plot
p = 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 buttons
def 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 grid
ax.grid()
# x and y-coordinates for graph creation
x = np.linspace(0, 2*np.pi, 200)
y = np.cos(x**2)
# sub-plot for radio button with
# left, bottom, width, height values
rax2 = plt.axes([0.05, 0.15, 0.15, 0.2])
radio_button = RadioButtons(rax2, ('red',
'blue',
'green'))
# function performed on switching radiobuttons
def colorfunc(label2):
l.set_color(label2)
plt.draw()
radio_button.on_clicked(colorfunc)
plt.show()
输出: