使用单选按钮根据字典的项目更改图表的情节
changing the plot of a graph based on the items of a dictionary using radiobuttons
我有一本包含许多项目的字典。我想创建单个图形,其中单选按钮的数量等于字典中的项目数。单选按钮选项将是字典的键,每次您 select 编辑不同的单选按钮时,相关值都会相应地绘制出来。例如:
from matplotlib.widgets import Slider, Button, RadioButtons
data = {'A' : [1,2,4,6,7,8,10],'B' : [1,2,4,6,4,3,7], 'C' : [3,2,1,6,2,3,4]}
axcolor = 'lightgoldenrodyellow'
rax = plt.axes([0.025, 0.5, 0.15, 0.15], facecolor=axcolor)
radio = RadioButtons(rax, data.keys(), active=0)
def chooseGraph(graph):
[plt.plot(data[names]) for names in data.keys()]
radio.on_clicked(chooseGraph)
plt.show()
当我执行这段代码时,我得到了单选按钮,但没有图表。当我 select 一个按钮时,所有 3 个数据集都绘制在单选按钮的顶部,更改按钮只会更改颜色。
提前感谢您的帮助!
您需要创建一个轴来绘制数据,否则它会选择当前活动的轴,即带有单选按钮的轴。
import matplotlib.pyplot as plt
from matplotlib.widgets import RadioButtons
data = {'A' : [1,2,4,6,7,8,10],'B' : [1,2,4,6,4,3,7], 'C' : [3,2,1,6,2,3,4]}
fig, ax = plt.subplots()
ax.plot(data["A"])
rax = fig.add_axes([0.025, 0.5, 0.15, 0.15])
radio = RadioButtons(rax, sorted(data.keys()), active=0)
def chooseGraph(graph):
ax.clear()
ax.plot(data[graph])
fig.canvas.draw_idle()
radio.on_clicked(chooseGraph)
plt.show()
我有一本包含许多项目的字典。我想创建单个图形,其中单选按钮的数量等于字典中的项目数。单选按钮选项将是字典的键,每次您 select 编辑不同的单选按钮时,相关值都会相应地绘制出来。例如:
from matplotlib.widgets import Slider, Button, RadioButtons
data = {'A' : [1,2,4,6,7,8,10],'B' : [1,2,4,6,4,3,7], 'C' : [3,2,1,6,2,3,4]}
axcolor = 'lightgoldenrodyellow'
rax = plt.axes([0.025, 0.5, 0.15, 0.15], facecolor=axcolor)
radio = RadioButtons(rax, data.keys(), active=0)
def chooseGraph(graph):
[plt.plot(data[names]) for names in data.keys()]
radio.on_clicked(chooseGraph)
plt.show()
当我执行这段代码时,我得到了单选按钮,但没有图表。当我 select 一个按钮时,所有 3 个数据集都绘制在单选按钮的顶部,更改按钮只会更改颜色。
提前感谢您的帮助!
您需要创建一个轴来绘制数据,否则它会选择当前活动的轴,即带有单选按钮的轴。
import matplotlib.pyplot as plt
from matplotlib.widgets import RadioButtons
data = {'A' : [1,2,4,6,7,8,10],'B' : [1,2,4,6,4,3,7], 'C' : [3,2,1,6,2,3,4]}
fig, ax = plt.subplots()
ax.plot(data["A"])
rax = fig.add_axes([0.025, 0.5, 0.15, 0.15])
radio = RadioButtons(rax, sorted(data.keys()), active=0)
def chooseGraph(graph):
ax.clear()
ax.plot(data[graph])
fig.canvas.draw_idle()
radio.on_clicked(chooseGraph)
plt.show()