python - Matplotlib - 2 Figures in Subplot - 1 is Animation

标签 python animation matplotlib

我想在子图中绘制两个图形:

fig = plt.figure()
ax1 = fig.add_subplot(1,2,1)
ax2 = fig.add_subplot(1,2,2)

假设 ax1 将填充一个添加点的动画(散点图)。然后 Ax2 将这些点分箱到网格并显示密度。

我可以在子图 1 中显示动画,并在完成后将密度图像添加到子图 2 吗?

最佳答案

这应该是可能的。请看example .您也可以查看上一个问题:

Simple animation of 2D coordinates using matplotlib and pyplot

下面是一个示例实现。在第一个停止渲染之前,第二个图是隐藏的:

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

def update_line(num, data, line, img):
    line.set_data(data[...,:num])
    if num == 24:
        img.set_visible(True)
    return line, img

fig1 = plt.figure()

data = np.random.rand(2, 25)
ax1=plt.subplot(211)
l, = plt.plot([], [], 'rx')
plt.xlim(0, 1)
plt.ylim(0, 1)
plt.xlabel('x')
plt.title('test')
ax2=plt.subplot(212)
nhist, xedges, yedges = np.histogram2d(data[0,:], data[1,:])
img = plt.imshow(nhist, aspect='auto', origin='lower')
img.set_visible(False)
line_ani = animation.FuncAnimation(fig1, update_line, 25, 
                                   fargs=(data, l, img),
                                   interval=50, blit=True)
line_ani.repeat = False
plt.show()

关于python - Matplotlib - 2 Figures in Subplot - 1 is Animation,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13366057/

相关文章:

ajax - 人们从哪里获得旋转加载图像?

css - 悬停时取消 CSS3 动画

android - LinearLayout animate() 剪辑 View ,clipChildren 不起作用

Python - 当行索引和列都是类别时数据帧值的散点图

python - 查找两个列表的索引

Python 请求模块。 SSL 证书验证错误

python - matplotlib 两种不同颜色的同一个注释

python - 如何在 matplotlib "histogram"图表上添加(或注释)值标签(或频率)

python - 将 PyTorch 模型转换为 tf.keras 时无法加载 .pb

python - Python multiprocessing.Process 子类的属性