python - 实时更新 pyplot 图表

标签 python matplotlib time

我正在尝试绘制二维数据网格并将它们映射到颜色。然后我想更新这些值并用新值更新图表。目前该图仅显示最终结果,并未显示该图应经历的所有中间阶段。

我的代码::

import matplotlib.pyplot as pyplot
import matplotlib as mpl
import numpy as np
import time
import matplotlib.animation as animation



thing=0
NUM_COL=10
NUM_ROW=10

zvals=np.full((NUM_ROW,NUM_COL),-5.0)

def update_graph(zvals):
    zvals+=1
    pyplot.clf()
    img = pyplot.imshow(zvals,interpolation='nearest',
                    cmap = cmap,norm=norm)
    time.sleep(1)
    pyplot.draw()

# make a color map of fixed colors
cmap = mpl.colors.ListedColormap(['blue','black','red'])
bounds=[-6,-2,2,6]
norm = mpl.colors.BoundaryNorm(bounds, cmap.N)

# tell imshow about color map so that only set colors are used

img = pyplot.imshow(zvals,interpolation='nearest',
                    cmap = cmap,norm=norm)

# make a color bar
pyplot.colorbar(img,cmap=cmap,norm=norm,boundaries=bounds,ticks=[-5,0,5])



pyplot.draw()

for i in range(5):
    update_graph(zvals)

pyplot.show()

最佳答案

所以我不确定这是否是一个答案,我之前只使用过更新绘图一次。但这是实现您想要的目标的一种方法。

import matplotlib.animation as animation
import matplotlib.pyplot as plt
import matplotlib as mpl
import numpy as np

NUM_COL = 10
NUM_ROW = 10

zvals = np.full((NUM_ROW,NUM_COL),-5.0)
cmap = mpl.colors.ListedColormap(['blue','black','red'])
bounds = [-6,-2,2,6]
norm = mpl.colors.BoundaryNorm(bounds, cmap.N)

fig = plt.figure() # Create the figure
img = plt.imshow(zvals,interpolation='nearest', cmap=cmap,norm=norm) # display the first image
plt.colorbar(img,cmap=cmap,norm=norm,boundaries=bounds,ticks=[-5,0,5]) # create your colour bar

# If we dont have this, then animation.FuncAnimation will call update_graph upon initialization
def init():
    pass

# animation.FuncAnimation will use this function to update the plot. This is where we update what we want displayed
def update_graph(frame):
    global zvals # zvals is a global variable
    zvals+=1 
    img.set_data(zvals) # This sets the data to the new, updated values
    print("Frame Update {}".format(frame)) # this is for debugging to help you see whats going on
    return img

# This is what will run the animations
anim = animation.FuncAnimation(fig, update_graph, init_func = init,
                                                  interval  = 1000, # update every 1000ms
                                                  frames  = 8, # Update 8 times
                                                  repeat=False) # After 8 times, don't repeat the animation
plt.show() # show our plot

关于python - 实时更新 pyplot 图表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44686023/

相关文章:

python - OrderedDictionary.popitem() 无法迭代所有值?

python - 如何拆分堆栈中的行并为 Pandas 中的每个行执行 `select`?

python - 将参数从类传递给方法 pd.read_sql?

python - IPython 键盘中断 CTRL + C 不一致

python - 如何旋转 x 轴标签

python - Kivy 网格布局固定某些列的列宽

python-2.7 - 用fig.show()内联绘制一个IPython Notebook图形?

Java Swing : Set starting time on timer and loop it

algorithm - 为什么以下两个重复查找算法的时间复杂度不同?

javascript - 比较 javascript 中的两个时间,格式为 'hh:mm tt'