【问题标题】:Setting Radiobutton to swich between lists设置单选按钮以在列表之间切换
【发布时间】:2023-03-20 01:24:02
【问题描述】:

我正在尝试创建一个垂直条等于分数长度的图,并带有一个单选按钮,使用户能够选择他/她想要查看的级别分数,例如: 按钮 1:显示 John 和 Daniel 在关卡 1 中的得分。 按钮 2:显示 John 和 Daniel 在关卡 2 中的得分。 按钮 3:显示 John 和 Daniel 在关卡 3 中的得分。 ... 等等。 如果你运行代码,设计的想法应该是清晰的。

我的一般问题是如何将单选按钮连接到项目的值,在本例中称为“level1”、“level2”和“level3”(第 19、20 和 21 行)。 作为一个 scablon,我所拥有的只是一个来自 Matplotlib 的换色器(已禁用)。

我尝试了很多谷歌搜索,但只能想出如何设计单选按钮本身的答案,而不是如何将其连接到我的输入。

我希望问题很清楚。谢谢

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()

【问题讨论】:

    标签: python matplotlib widget radio-button


    【解决方案1】:

    好的,所以我想我已经成功了。试试这个:

    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()
    

    我改变了两件事,基本上:

    1. 使用 fig、ax 和 add_subplot 语法来简化绘图操作
    2. 有了这个,我可以清除包含您的数据的子图(不会破坏整个图),重新绘制坐标轴(否则它会变得一团糟并用您的数据填充图。我猜你不想要那个)和重新绘制数据

    应该有一个更优雅的解决方案,涉及类似于 set_ydata 的函数,但那个特定的函数似乎不适用于 barh。要点是你应该能够在不重新绘制的情况下做到这一点,这更快,但这似乎有效。至少对我来说。

    我希望这会有所帮助!

    另外,灵感:How to update a plot in matplotlib?

    【讨论】:

    • 它有效,这正是我想要的。谢谢一百万。
    • 没问题!乐于助人:)
    猜你喜欢
    • 1970-01-01
    • 2017-09-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-04
    相关资源
    最近更新 更多