Python 带滑块的Matplotlib动态打印

Python 带滑块的Matplotlib动态打印,python,matplotlib,slider,Python,Matplotlib,Slider,我不太习惯于matplotlib动态绘图,因此我在创建所需绘图时遇到一些困难。我正在尝试绘制N条同样大尺寸(800k点)的时间线(不太多,少说少于5条)。我想用一个滑块来表示这一点 import numpy as np from matplotlib import pyplot as plt from matplotlib.widgets import Slider plt.ioff() timelines = [np.random.randint(-2, 5, size = 800000)

我不太习惯于
matplotlib
动态绘图,因此我在创建所需绘图时遇到一些困难。我正在尝试绘制N条同样大尺寸(800k点)的时间线(不太多,少说少于5条)。我想用一个滑块来表示这一点

import numpy as np
from matplotlib import pyplot as plt
from matplotlib.widgets import Slider

plt.ioff()

timelines = [np.random.randint(-2, 5, size = 800000),
             np.random.randint(-2, 5, size = 800000), 
             np.random.randint(-2, 5, size = 800000)]
timelines_labels = ["label1", "label2", "label3"]

def slide_plot(timelines, timelines_labels):

    f, ax = plt.subplots(len(timelines), 1, sharex = True, figsize = (20, 10))

    def update(pos, ax = ax, timelines = timelines):
        for k, a in enumerate(ax):
            a.axis([pos,pos+80,-max(timelines[k])-1, max(timelines[k])+1])
        f.canvas.draw_idle()

    f.subplots_adjust(bottom=0.25)
    plt.setp([a.get_xticklabels() for a in f.axes[:-1]], visible=False)
    t = np.arange(0.0, len(timelines[0]), 1)

    for k, a in enumerate(ax):
        a.plot(t, timelines[k], lw = 0.55, label=timelines_labels[k])
        a.legend(loc="upper right")
        a.axis([0, 160000, -max(timelines[k])-1, max(timelines[k])+1])

    ax[-1].set_xticks(np.arange(0.0, len(timelines[0])+80000, 80000))

    axpos = plt.axes([0.2, 0.1, 0.65, 0.03])
    spos = Slider(axpos, 'Time (ms)', valmin =0, valmax=800000, valinit=0)
    spos.on_changed(update)
    plt.show()

slide_plot(timelines, timelines_labels)
如您所见,由于我的绘图共享X轴,因此我将从所有轴(底部轴除外)中取出xticks标签(稍后将对xticks执行相同操作)

然后我创建了变量t,它只是时间,我觉得它没用,而且
ax[k].plot(timeline[k])
就足够了

我通过每80000点设置一个勾号来进行更多的格式化

最后,我得到了滑块。显然,update函数不起作用。我不知道正确的语法,也不知道实现这一点的正确方法

谢谢:)

编辑:通过放置参数
ax=ax
timeline=timeline
它似乎开始工作,但是我不喜欢函数的外观。我很肯定有更好的办法

编辑:最新的脚本。。。以及输出:


问题只应出现在IPython(或使用IPython的Spyder)中。问题是
plt.show()
将不会阻塞,并且函数
slide\u plot
将返回。一旦它返回,所有对滑块的引用以及回调都将消失。 (中的代码不使用函数,因此不会出现此问题。)

解决方案是让函数返回对滑块的引用并存储它

import numpy as np
from matplotlib import pyplot as plt
from matplotlib.widgets import Slider

timelines = [np.random.randint(-2, 5, size = 800000),
             np.random.randint(-2, 5, size = 800000), 
             np.random.randint(-2, 5, size = 800000)]
timelines_labels = ["label1", "label2", "label3"]

def slide_plot(timelines, timelines_labels):

    f, ax = plt.subplots(len(timelines), 1, sharex = True)

    def update(pos):
        for k, a in enumerate(ax):
            a.axis([pos,pos+25,-max(timelines[k])-1, max(timelines[k])+1])
        f.canvas.draw_idle()

    f.subplots_adjust(bottom=0.25)
    plt.setp([a.get_xticklabels() for a in f.axes[:-1]], visible=False)
    t = np.arange(0.0, len(timelines[0]), 1)

    for k, a in enumerate(ax):
        a.plot(t, timelines[k], lw = 0.55, label=timelines_labels[k])
        a.legend(loc="upper right")
        a.axis([0, 25, -max(timelines[k])-1, max(timelines[k])+1])

    ax[-1].set_xticks(np.arange(0.0, len(timelines[0]) / 8000, 10))

    axpos = plt.axes([0.2, 0.1, 0.65, 0.03])
    spos = Slider(axpos, 'Time (ms)', 0.1, 90.0)
    spos.on_changed(update)
    plt.show()
    return spos

slider = slide_plot(timelines, timelines_labels)

或者,您可以将Spyder配置为不使用Preferences/IPython/graphics下的“matplotlib图形支持”,禁用“激活支持”并启动新的IPython控制台以使其生效。

您的缩进似乎有问题,请验证并修复它。此外,如果缩进是正确的,您永远不会调用
slide\u plot
@ReblochonMasque No。事实上我没有接到电话。。。我只是用随机数构建了一个虚拟的例子,在编辑过程中稍加修改,更新不会崩溃。但是,滚动条不起作用,窗口等于完整的点集:/n如果需要,可以直接使用
def update(pos):
并删除行
pos=spos.val
。但是,当前的代码已经可以工作了。因此,你在哪里以及如何运行这个问题?@ImportanceOfBeingErnest有趣。我已经开始尝试这篇文章:一旦取出
axcolors
行,它就可以工作了。我正在使用spyder,我首先在Ipytohn Constrole中设置了
%matplotlib
,以打开带有绘图的新窗口。文章中的例子是可行的,但是,我们看到了我的场景,滚动条不起作用。非常感谢,我永远不会猜到它是x')