我正在尝试创建一个垂直条等于分数长度的图,并使用一个单选按钮,用户可以选择他/她想要查看的等级分数,如下所示: 按钮1:显示级别1中John和Daniel的分数。 按钮2:在2级显示John和Daniel的分数。 按钮3:显示3级中约翰和丹尼尔的分数。 ... 等等。 如果你运行代码,设计的想法应该是清楚的。
我的一般问题是如何将radiobutton连接到项目的值,在这个例子中称为“level1”,“level2”和“level3”(第19,20和21行)。 我所有的scabelon都是来自Matplotlib的换色器(禁用)。
我尝试了很多googeling,但只能提出如何设计radiobutton本身的答案,而不是如何将它连接到我的输入。
我希望问题很清楚。谢谢
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.widgets import Slider, Button, RadioButtons
john_level1 = 10
john_level2 = 2
john_level3 = 18
daniel_level1 = 11
daniel_level2 = 0
daniel_level3 = 6
fig, ax = plt.subplots()
plt.subplots_adjust(left=0.35, bottom=0.25)
people = ("John", "Daniel")
y_pos = np.arange(len(people))
level1 = john_level1, daniel_level1
level2 = john_level2, daniel_level2
level3 = john_level3, daniel_level3
plt.axis([0, 12, -1, 2])
axcolor = 'lightgoldenrodyellow'
plt.barh(y_pos, level1, align='center', alpha=0.5)
plt.yticks(y_pos, people)
plt.title('Individuel Points', fontsize=14, fontweight='bold')
#################
# RADIO BUTTONS # Originally a color-changer from Matplotlib
#################
rax = plt.axes([0.015, 0.45, 0.25, 0.25], axisbg=axcolor)
radio = RadioButtons(rax, ('Score at Level 1', 'Score at Level 2', 'Score at Level 3'), active=0)
def raidfunc(label):
# l.set_color(label)
fig.canvas.draw_idle()
radio.on_clicked(raidfunc)
plt.show()
答案 0 :(得分:0)
好吧,所以我想我得上班了。试试这个:
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.widgets import Slider, Button, RadioButtons
john_level1 = 10
john_level2 = 2
john_level3 = 18
daniel_level1 = 11
daniel_level2 = 0
daniel_level3 = 6
# plt.subplots_adjust(left=0.35, bottom=0.25)
people = ("John", "Daniel")
y_pos = np.arange(len(people))
level1 = john_level1, daniel_level1
level2 = john_level2, daniel_level2
level3 = john_level3, daniel_level3
fig = plt.figure()
ax = fig.add_subplot(111)
ax.axis([0, 12, -1, 2])
axcolor = 'lightgoldenrodyellow'
ax.barh(y_pos, level1, align='center', alpha=0.5)
ax.set_yticks(y_pos, people)
ax.set_title('Individual Points', fontsize=14, fontweight='bold')
#################
# RADIO BUTTONS # Originally a color-changer from Matplotlib
#################
rax = fig.add_axes([0.015, 0.75, 0.25, 0.25], axisbg=axcolor)
radio = RadioButtons(rax, ('Score at Level 1', 'Score at Level 2', 'Score at Level 3'), active=0)
def raidfunc(label):
if (label == 'Score at Level 1'):
ax.clear()
ax.axis([0, 12, -1, 2])
ax.barh(y_pos, level1, align='center', alpha=0.5)
fig.canvas.draw()
if (label == 'Score at Level 2'):
ax.clear()
ax.axis([0, 12, -1, 2])
ax.barh(y_pos, level2, align='center', alpha=0.5)
fig.canvas.draw()
if (label == 'Score at Level 3'):
ax.clear()
ax.axis([0, 12, -1, 2])
ax.barh(y_pos, level3, align='center', alpha=0.5)
fig.canvas.draw()
radio.on_clicked(raidfunc)
plt.show()
我改变了两件事,基本上是:
对于涉及类似于set_ydata的函数,应该有一个更优雅的解决方案,但是这个特定的一个似乎不适用于barh。要点是你应该能够在没有重新绘制的情况下做到这一点,这更快,但这似乎有效。至少对我而言。
我希望这有帮助!