【问题标题】:How to switch between diagrams with a button in Matplotlib如何在 Matplotlib 中使用按钮在图表之间切换
【发布时间】:2018-11-10 17:47:33
【问题描述】:

有没有简单的方法,通过一个按钮在两个或多个图表之间切换?例如,我希望能够通过一个按钮在这两个图表之间切换,而不是一个接一个地显示它们。

import matplotlib.pyplot as plt

x_values = [1, 2, 3, 4, 5]
y_values = [1, 4, 9, 16, 25]
plt.bar(x_values, y_values)
plt.show()

x_values = [5, 4, 3, 2, 1]
y_values = [1, 4, 9, 16, 25]
plt.bar(x_values, y_values)
plt.show()

我知道有这个问题的例子,但我根本无法让它工作......

这里是链接,他们展示了如何做到这一点,但我不明白如何做到这一点......

链接:https://matplotlib.org/gallery/widgets/buttons.html

【问题讨论】:

    标签: python matplotlib button diagram


    【解决方案1】:

    在这里,我修改了您提供的链接中的代码,以便它根据当前绘制的值使用不同的值集。

    import matplotlib.pyplot as plt
    from matplotlib.widgets import Button
    
    x1_values = [1, 2, 3, 4, 5]
    y1_values = [1, 4, 9, 16, 25]
    l, = plt.plot(x1_values, y1_values)
    
    
    class Index(object):
        def __init__(self):
            self.current = 1
    
            self.x1 = [5, 4, 3, 2, 1]
            self.y1 = [1, 4, 9, 16, 25]
    
            self.x2 = [1, 2, 3, 4, 5]
            self.y2 = [1, 4, 9, 16, 25]
    
        def plot(self, x):
            self.current += 1
    
            if self.current%2:
                self.values1()
            else:
                self.values2()
    
        def values1(self):
            l.set_xdata(self.x1)
            l.set_ydata(self.y1)
            plt.draw()
    
        def values2(self):
            l.set_xdata(self.x2)
            l.set_ydata(self.y2)
            plt.draw()
    
    callback = Index()
    axnext = plt.axes([0.81, 0.05, 0.1, 0.075])
    bprev = Button(axnext, 'Switch')
    bprev.on_clicked(callback.plot)
    
    plt.show()
    

    【讨论】:

    • 非常感谢,更好地理解和尝试!
    【解决方案2】:

    对于那些想要在条形图或绘图图之间切换的人(适用于两种类型的图表)。我也认为这样做更干净。

    import matplotlib.pyplot as plt
    from matplotlib.widgets import Button
    
    
    class Index(object):
    
        def start(self, event=None):
            ax.clear()
            x_values = [1, 2, 3, 4, 5]
            y_values = [3, 5, 2, 19, 1]
            ax.bar(x_values, y_values)
            plt.draw()
    
        def next(self, event):
            ax.clear()
            x_values = [1, 2, 3, 4, 5]
            y_values = [20, 15, 10, 5, 1]
            ax.bar(x_values, y_values)
            plt.draw()
    
        def prev(self, event):
            ax.clear()
            x_values = [1, 2, 3, 4, 5]
            y_values = [1, 5, 10, 15, 20]
            ax.bar(x_values, y_values)
            plt.draw()
    
    ax  = plt.gca()
    callback = Index()
    callback.start()
    
    axprev = plt.axes([0.59, 0.002, 0.1, 0.075])
    bprev = Button(axprev, 'Previous')
    bprev.on_clicked(callback.prev)
    axstart = plt.axes([0.7, 0.002, 0.1, 0.075])
    bstart = Button(axstart, 'Start')
    bstart.on_clicked(callback.start)
    axnext = plt.axes([0.81, 0.002, 0.1, 0.075])
    bnext = Button(axnext, 'Next')
    bnext.on_clicked(callback.next)
    
    plt.show()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-03-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-09-12
      相关资源
      最近更新 更多