【问题标题】:Python Matplotlib: change the displayed figure interactively using a widgetPython Matplotlib:使用小部件以交互方式更改显示的图形
【发布时间】:2015-01-27 22:14:59
【问题描述】:

我有一个数据列表,例如 {A_1, A_2, A_3, ...},其中每个元素又是一个大数据列表,例如 A_i = {p_i_1, p_i_2, ...}。

我想使用 Matplotlib 制作每个 A_i 的列表图,比如 plot_A_i,然后具有更改 plot_A_i 之间显示图的功能。因为每个 A_i 都很大,我不想每次都重新绘制它,而是先绘制所有图,然后使用 Matplotlib 的某种小部件更改显示的图。我想到的是类似于 Mathematica 的“操纵”。我该怎么做?

【问题讨论】:

    标签: python matplotlib


    【解决方案1】:

    这是我使用 Axes.set_visible() 的方法。任何评论和/或建议都将受到欢迎!

    import numpy as np
    import matplotlib.pyplot as plt
    from matplotlib.widgets import Slider
    
    def f(t):
        return np.exp(-t) * np.cos(2*np.pi*t)
    
    class PlotList:
        def __init__(self, plots):
            self.cur_val = 0
            self.plots = plots
            self.plots[self.cur_val].set_visible(True)
        def set_visible(self, val):
            new_val = int(val)
            if(self.cur_val != new_val):
                self.plots[self.cur_val].set_visible(False)
                self.plots[new_val].set_visible(True)
                self.cur_val = new_val
            fig.canvas.draw_idle()
    
    t1 = np.arange(0.0, 5.0, 0.1)
    t2 = np.arange(0.0, 5.0, 0.02)
    
    
    fig = plt.figure()
    plot_axes_rect = [0.125, 0.15, .8, 0.75]
    plots = [fig.add_axes(plot_axes_rect, label=1, visible=False),
             fig.add_axes(plot_axes_rect, label=2, visible=False)]
    plots[0].plot(t1, f(t1), 'bo')
    plots[1].plot(t2, f(t2), 'k')
    
    pl = PlotList(plots)
    
    theta_axes = fig.add_axes([0.125, 0.05, .8, 0.05])
    theta_slider = Slider(theta_axes, 'theta', 0, 2,
                          valinit=pl.cur_val, valfmt='%d')
    
    theta_slider.on_changed(pl.set_visible)
    
    plt.show()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-04-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-09-03
      • 2015-12-19
      • 2019-10-12
      相关资源
      最近更新 更多