如何调整 Matplotlib RadioButtons 的大小?
Matplotlib 是一个出色的Python可视化库,用于数组的 2D 绘图。 Matplotlib 是一个基于 NumPy 数组的多平台数据可视化库,旨在与更广泛的 SciPy 堆栈一起使用。
单选按钮是可视化中最常用的小部件。单选按钮让我们在可视化中的多个选项之间进行选择。在这种情况下,这些按钮让用户可以选择三种不同颜色中的一种,以便在图中显示正弦波。
这是单选按钮的简单可视化。
Python3
# import modules
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.widgets import RadioButtons
# creating an array starting from
# 0 to 1 with step size 0.01
t = np.arange(0.0, 1.0, 0.01)
# the values of sin values of t
s0 = np.sin(2*np.pi*t)
# depict visualization
fig, ax = plt.subplots()
l, = ax.plot(t, s0, lw=2, color='red')
plt.subplots_adjust(left=0.3)
# adjust radio buttons
axcolor = 'lightgoldenrodyellow'
rax = plt.axes([0.05, 0.4, 0.15, 0.30],
facecolor=axcolor)
radio = RadioButtons(rax, ('red', 'blue', 'green'))
plt.show()
Python3
# import module
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.widgets import RadioButtons
# creating an array starting from
# 0 to 1 with step size 0.01
t = np.arange(0.0, 1.0, 0.01)
# the values of sin values of t
s0 = np.sin(2*np.pi*t)
# depict visualiiation
fig, ax = plt.subplots()
l, = ax.plot(t, s0, lw=2, color='red')
plt.subplots_adjust(left=0.3)
# adjust radiobuttons
axcolor = 'lightgoldenrodyellow'
rax = plt.axes([0.05, 0.4, 0.15, 0.30],
facecolor=axcolor)
radio = RadioButtons(rax, ('red', 'blue', 'green'))
# adjust radius here. The default is 0.05
for circle in radio.circles:
circle.set_radius(0.09)
Python3
# import module
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.widgets import RadioButtons
# creating an array starting from
# 0 to 1 with step size 0.01
t = np.arange(0.0, 1.0, 0.01)
# the values of sin values of t
s0 = np.sin(2*np.pi*t)
# depict visualization
fig, ax = plt.subplots()
l, = ax.plot(t, s0, lw=2, color='red')
plt.subplots_adjust(left=0.3)
# adjust radiobuttons
axcolor = 'lightgoldenrodyellow'
rax = plt.axes([0.05, 0.4, 0.15, 0.30],
facecolor=axcolor)
radio = RadioButtons(rax, ('red', 'blue', 'green'))
# adjust radius here. The default is 0.05
for circle in radio.circles:
circle.set_radius(0.02)
输出:
在上面的代码中, plt.axes()将轴添加到当前图形并使其成为当前轴。第一个参数是box = [left, bottom, width, height] 的4 元浮点数。第二个参数是**kwargs 。此方法还采用返回的轴类的关键字参数。它可以是 facecolor、figure 等。在RadioButtons()方法中,第一个参数是上面创建的轴,第二个参数是我们想要的标签元组。
调整单选按钮中圆的半径
圆的默认大小为 0.05。我们添加如下所示的简单线条来更改半径。如果圆的半径大于 0.05,则说 0.09。
蟒蛇3
# import module
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.widgets import RadioButtons
# creating an array starting from
# 0 to 1 with step size 0.01
t = np.arange(0.0, 1.0, 0.01)
# the values of sin values of t
s0 = np.sin(2*np.pi*t)
# depict visualiiation
fig, ax = plt.subplots()
l, = ax.plot(t, s0, lw=2, color='red')
plt.subplots_adjust(left=0.3)
# adjust radiobuttons
axcolor = 'lightgoldenrodyellow'
rax = plt.axes([0.05, 0.4, 0.15, 0.30],
facecolor=axcolor)
radio = RadioButtons(rax, ('red', 'blue', 'green'))
# adjust radius here. The default is 0.05
for circle in radio.circles:
circle.set_radius(0.09)
输出:
这是另一个例子,其中圆的半径小于 0.05 比如说 0.02
蟒蛇3
# import module
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.widgets import RadioButtons
# creating an array starting from
# 0 to 1 with step size 0.01
t = np.arange(0.0, 1.0, 0.01)
# the values of sin values of t
s0 = np.sin(2*np.pi*t)
# depict visualization
fig, ax = plt.subplots()
l, = ax.plot(t, s0, lw=2, color='red')
plt.subplots_adjust(left=0.3)
# adjust radiobuttons
axcolor = 'lightgoldenrodyellow'
rax = plt.axes([0.05, 0.4, 0.15, 0.30],
facecolor=axcolor)
radio = RadioButtons(rax, ('red', 'blue', 'green'))
# adjust radius here. The default is 0.05
for circle in radio.circles:
circle.set_radius(0.02)
输出: