【问题标题】:How do you add a colormap to a matplotlib Animation?如何将颜色图添加到 matplotlib 动画?
【发布时间】:2018-06-25 06:24:21
【问题描述】:

假设我尝试使用其 X 和 Y 轴位置来可视化对象位置,并使用其他变量“Z”作为颜色图。下面这个简化的例子说明了我目前是如何做到这一点的。

import numpy as np
from matplotlib import pyplot as plt

dataX = np.linspace(-50,50,1000)
dataY = np.linspace(-50,50,1000)
dataZ = np.linspace(-50,50,1000)

plt.scatter(dataX, dataY, c=dataZ, cmap='winter', edgecolors='none')
plt.colorbar()
plt.show()

结果:

我想为此添加实时动画,而不仅仅是显示静态图像,但我正在努力为其添加颜色图。下面的代码显示了我是如何做到的没有颜色图。

import matplotlib.pyplot as plt
import matplotlib.animation as animation
import numpy as np
import Tkinter
import tkMessageBox

def restart():
    root = Tkinter.Tk()
    root.withdraw()
    result = tkMessageBox.askyesno("Restart", "Would you like to restart the animation?")
    if result:
        ani.frame_seq = ani.new_frame_seq() 
        ani.event_source.start()
    else:
        plt.close()

dataX = np.linspace(-50,50,1000)
dataY = np.linspace(-50,50,1000)
dataZ = np.linspace(-50,50,1000)


def function(num, dataX,dataY, line):
    line.set_data(dataX[..., :num],dataY[..., :num])
    if num == dataX.size :
        restart()
    return line,

fig = plt.figure()

l, = plt.plot([], [], 'ro', markeredgewidth=0.0)
limitsX = [min(dataX)-100,max(dataX)+100]
limitsY = [min(dataY)-100, max(dataY)+100]
plt.xlim(limitsX[0],limitsX[1] )
plt.ylim(limitsY[0],limitsY[1] )
plt.xlabel('x')
plt.ylabel('y')
plt.title('test')

ani = animation.FuncAnimation(fig, function, (dataX.size+1), fargs=(dataX,dataY,l),
                              interval=10, blit = True, repeat = False)

plt.show()

重新提出问题:如何向我的动画添加颜色图?

【问题讨论】:

    标签: python animation matplotlib


    【解决方案1】:

    这个答案大致基于my answer to a similar problem,尽管我觉得处理PathCollection(由scatter()返回)的具体情况需要一个新的答案。

    这是我解决问题的方法。诀窍是在图中的第二个轴上生成一个单独的静态颜色条。然后,在更新 PathCollection 属性时,使用该颜色条和标准化来更新点的颜色。

    dataX = np.linspace(-50,50,1000)
    dataY = np.linspace(-50,50,1000)
    dataZ = np.linspace(-50,50,1000)
    
    
    def animate(num):
        data = np.hstack((dataX[:num,np.newaxis], dataY[:num, np.newaxis]))
        art.set_offsets(data)
        art.set_color(cmap(norm(dataZ[:num]))) # update colors using the colorbar and its normalization defined below
        return art,
    
    
    fig,[ax,cax] = plt.subplots(1,2, gridspec_kw={"width_ratios":[50,1]})
    # Set the colormap and norm to correspond to the data for which
    # the colorbar will be used.
    cmap = matplotlib.cm.winter
    norm = matplotlib.colors.Normalize(vmin=-50, vmax=50)
    
    cb1 = matplotlib.colorbar.ColorbarBase(cax, cmap=cmap,
                                    norm=norm,
                                    orientation='vertical')
    
    ax.set_xlim(-60,60)
    ax.set_ylim(-60,60)
    ax.set_xlabel('x')
    ax.set_ylabel('y')
    ax.set_title('test')
    art = ax.scatter([],[],c=[])
    
    ani = animation.FuncAnimation(fig, animate,interval=2, blit=True, repeat=True)
    
    plt.show()
    

    【讨论】:

    • 谢谢!你让它看起来比我想象的要简单得多。
    • 嘿,这可以在 2D 地图中使用相同的概念吗?我有粒子(1000 个)在 241 个时间步长内移动,我想使用它们的质量作为颜色为轨迹设置动画。
    猜你喜欢
    • 2019-08-02
    • 1970-01-01
    • 1970-01-01
    • 2016-09-16
    • 2014-01-12
    • 2020-08-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多