簡體   English   中英

帶滑塊的Matplotlib動態圖

[英]Matplotlib dynamic plot with a slider

我不太習慣使用matplotlib動態圖,因此創建所需的圖有些困難。 我正在嘗試繪制相同大小(800k點)的N個時間軸(不要太多,少說少於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(timelines[k])就足夠了。

我通過每80000點設置一個刻度來進行更多格式化。

最后,我得到了滑塊。 顯然,更新功能不起作用。 我不知道正確的語法,也不知道實現此目的的正確方法。

謝謝 :)

編輯:通過放置參數ax = axtimelines = timelines似乎開始工作,但是我不喜歡該函數的外觀。 我很確定存在更好的方法。

編輯:最新腳本...和輸出:

產量

該問題僅應在IPython(或使用IPython的Spyder)中發生。 問題在於plt.show()將不會阻塞,並且函數slide_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圖形支持”,禁用“ Activate support”並啟動新的IPython控制台以使其生效。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM