簡體   English   中英

通過matplotlib圖表向后和向前滾動

[英]Scroll backwards and forwards through matplotlib plots

我的代碼使用matplotib從數據中生成了許多圖表,我希望能夠在現場演示中向前和向后滾動它們(可能是通過按向前和向后鍵或使用鼠標)。 目前,我必須將每個圖像單獨保存為圖像,然后使用單獨的圖像查看器滾動它們。 有沒有辦法完全從python中做到這一點?

實現這一目標的一種簡單方法是在列表中存儲x和y數組的元組,然后使用處理程序事件來選擇要繪制的下一個(x,y)對:

import numpy as np
import matplotlib.pyplot as plt

# define your x and y arrays to be plotted
t = np.linspace(start=0, stop=2*np.pi, num=100)
y1 = np.cos(t)
y2 = np.sin(t)
y3 = np.tan(t)
plots = [(t,y1), (t,y2), (t,y3)]

# now the real code :) 
curr_pos = 0

def key_event(e):
    global curr_pos

    if e.key == "right":
        curr_pos = curr_pos + 1
    elif e.key == "left":
        curr_pos = curr_pos - 1
    else:
        return
    curr_pos = curr_pos % len(plots)

    ax.cla()
    ax.plot(plots[curr_pos][0], plots[curr_pos][1])
    fig.canvas.draw()

fig = plt.figure()
fig.canvas.mpl_connect('key_press_event', key_event)
ax = fig.add_subplot(111)
ax.plot(t,y1)
plt.show()

在此代碼中,我選擇rightleft箭頭進行迭代,但您可以更改它們。

暫無
暫無

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

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