【问题标题】:Updating legend entry using imshow in Python3在 Python3 中使用 imshow 更新图例条目
【发布时间】:2018-03-26 22:20:30
【问题描述】:

我正在尝试添加一个图例来覆盖显示随机数动画数组的 imshow() 图。我希望更新图例以显示我们正在查看的步骤。

我尝试按照步骤here 进行操作,该步骤展示了如何使用 FuncAnimation 为 subplots() 创建动画图例。我相信显示动画数组的唯一方法是使用 ArtistAnimation() 和 imshow(),但其中一个或两个导致我无法遵循链接的解决方案。

我在工作代码下方附加了生成动画随机数组,图例解决方案(来自链接)双注释掉。

任何帮助或补救建议将不胜感激。

谢谢, C

import matplotlib.animation as animation
from matplotlib import colors
import matplotlib.pyplot as plt
import numpy as np

N=20
steps = 100
interval_pause = 100
repeat_pause = 1000

cmap = colors.ListedColormap(['white', 'black'])
bounds=[-1,0,1]
norm = colors.BoundaryNorm(bounds, cmap.N)
fig = plt.figure()
ax = plt.gca()
ax.axes.xaxis.set_ticklabels([])
ax.axes.yaxis.set_ticklabels([])
ax.axes.xaxis.set_ticks([])
ax.axes.yaxis.set_ticks([])
#plt.colorbar(img, cmap=cmap, norm=norm, boundaries=bounds, ticks=[-1,0,1])

array = 2*(np.random.rand(N,N,steps)-0.5)
state = np.zeros(steps)
ims = []    
##leg = ax.legend(loc='upper left',prop={'size':12})

for step in range(0,steps):
    state = array[:,:,step]
    im = plt.imshow(state,interpolation='nearest',cmap=cmap,norm=norm, animated=True)
    ##lab = 'step = '+str(step)
    ##leg.texts.set_text(lab)
    ims.append([im])##+leg])

ani = animation.ArtistAnimation(fig,ims,interval=interval_pause,repeat_delay=repeat_pause)
#ani.save('animate_evolution '+str(timer())+'.mp4')
plt.show()

【问题讨论】:

    标签: python python-3.x animation matplotlib imshow


    【解决方案1】:

    如您链接到的问题所示,使用FuncAnimation 更容易。这允许简单地更新单个图例和 imshow 图,而不是创建其中的几个。

    因为不清楚图例应该为 imshow 图显示什么,所以我只创建了一个蓝色矩形。你当然可以用你喜欢的任何东西替换它。

    import matplotlib.animation as animation
    from matplotlib import colors
    import matplotlib.pyplot as plt
    import numpy as np
    
    N=20
    steps = 100
    interval_pause = 100
    repeat_pause = 1000
    
    cmap = colors.ListedColormap(['white', 'black'])
    bounds=[-1,0,1]
    norm = colors.BoundaryNorm(bounds, cmap.N)
    fig = plt.figure()
    ax = plt.gca()
    ax.axes.xaxis.set_ticklabels([])
    ax.axes.yaxis.set_ticklabels([])
    ax.axes.xaxis.set_ticks([])
    ax.axes.yaxis.set_ticks([])
    
    array = 2*(np.random.rand(N,N,steps)-0.5)
    
    leg = ax.legend([plt.Rectangle((0,0),1,1)],["step0"], loc='upper left',prop={'size':12})
    
    img = ax.imshow(array[:,:,0],interpolation='nearest',cmap=cmap,norm=norm, animated=True)
    fig.colorbar(img, cmap=cmap, norm=norm, boundaries=bounds, ticks=[-1,0,1])
    
    def update(step):
        state = array[:,:,step]
        img.set_data(state)
        lab = 'step = '+str(step)
        leg.texts[0].set_text(lab)
    
    ani = animation.FuncAnimation(fig,update,frames = steps, 
                                  interval=interval_pause,repeat_delay=repeat_pause)
    plt.show()
    

    【讨论】:

    • 非常感谢,这正是我所追求的!出于好奇(并为第二轮提问道歉),传说在这种情况下是否有效,因为我们将它与 plt.Rectangle 相关联,还是有其他东西? imshow() 中的 ["step0"] 参数的目的是什么?是否可以不显示图标,而仅将图例保留为显示“Step = ”?再次感谢,C。
    • 好吧,图例通常用于显示绘图中的某些元素以及文本。如果图中没有该元素,则需要创建它。与文本相同(“step0”)。可能您不想实际使用图例,而是创建一些 plt.text
    猜你喜欢
    • 1970-01-01
    • 2018-02-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多