Matplotlib动画子图(2个历史图和2个散点图)

Matplotlib动画子图(2个历史图和2个散点图),matplotlib,subplot,Matplotlib,Subplot,如何设置以下4个子情节的动画?到目前为止,我已经设法使动画根本不停止,或使帧出现,但不点/条的情节。我试图遵循这个例子,但“子类化”远远超出了我的技能 import numpy as np import matplotlib.pyplot as plt import matplotlib.animation as animation # generate 4 random variables x = np.random.normal(-2.5, 1, 10000) y = np.rando

如何设置以下4个子情节的动画?到目前为止,我已经设法使动画根本不停止,或使帧出现,但不点/条的情节。我试图遵循这个例子,但“子类化”远远超出了我的技能

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

# generate 4 random variables  
x = np.random.normal(-2.5, 1, 10000)
y = np.random.gamma(2, 1.5, 10000)
a = np.random.exponential(2, 10000)+7
b = np.random.uniform(14,20, 10000)

n_bins = 100
n = 100
fig, ((ax1, ax2), (ax3, ax4)) = plt.subplots(2,2, sharex='col')

# plot
ax1.hist(x, bins=n_bins, facecolor='c')
ax1.set_title('normal')
ax1.spines['top'].set_visible(False)
ax1.spines['right'].set_visible(False)
ax1.spines['bottom'].set_visible(False)
ax1.spines['left'].set_visible(False)
ax1.tick_params(bottom=False, left=False)

ax2.hist(y, bins=n_bins, facecolor='c')
ax2.set_title('exponential')
ax2.spines['top'].set_visible(False)
ax2.spines['right'].set_visible(False)
ax2.spines['bottom'].set_visible(False)
ax2.spines['left'].set_visible(False)
ax2.tick_params(bottom=False, left=False)

ax3.scatter(x, a, facecolor='c')
ax3.set_title('gamma')
ax3.spines['top'].set_visible(False)
ax3.spines['right'].set_visible(False)
ax3.spines['bottom'].set_visible(False)
ax3.spines['left'].set_visible(False)
ax3.tick_params(bottom=False, left=False) 

ax4.scatter(y, b, facecolor='c')
ax4.set_title('uniform')
ax4.spines['top'].set_visible(False)
ax4.spines['right'].set_visible(False)
ax4.spines['bottom'].set_visible(False)
ax4.spines['left'].set_visible(False)
ax4.tick_params(bottom=False, left=False)


plt.show()

在这行中

fig, ((ax1, ax2), (ax3, ax4)) = plt.subplots(2,2, sharex='col')
ax1、ax2、ax3、ax4,有图像空间 您需要创建一个映像并放入ax1/2/3/4


我举了一个例子,说明了在你对你想要完成的事情发表评论后,我理解了,要得到一个比我在这里提供的内容更加完美的动画,需要计划一些数学

将numpy导入为np
将matplotlib.pyplot作为plt导入
将matplotlib.animation导入为动画
#某些参数可能会发生变化
vary=np.linspace(10120100,dtype=int)
n_bins=100
n=100
图,((ax1,ax2),(ax3,ax4))=plt.子批(2,2,sharex='col')
def plot_prop():
#密谋
ax1.set_ylim(0350)
ax1.设置标题(“正常”)
ax1.脊椎['top'].设置为可见(False)
ax1.脊椎['right'].set_可见(False)
ax1.脊椎['bottom'].set_可见(False)
ax1.脊椎['left'].设置为可见(False)
ax1.勾选参数(底部=假,左侧=假)
ax2.set_ylim(0450)
ax2.集合标题(“指数”)
ax2.脊椎['top'].设置为可见(假)
ax2.脊椎[“右”]。设置为可见(假)
ax2.脊椎['bottom'].set_可见(假)
ax2.脊椎['left'].set_可见(False)
ax2.勾选参数(底部=假,左侧=假)
ax3.set_xlim(-7,2)
ax3.set_ylim(5,25)
ax3.设置标题(“gamma”)
ax3.脊椎['top'].设置为可见(假)
ax3.脊椎[“右”]。设置为可见(假)
ax3.脊椎['bottom'].set_可见(False)
ax3.脊椎['left'].set_可见(False)
ax3.勾选参数(底部=假,左侧=假)
ax4.set_xlim(0,20)
ax4.设置标题(“统一”)
ax4.脊椎['top'].设置为可见(假)
ax4.脊椎[“右”]。设置为可见(假)
ax4.脊椎['bottom'].set_可见(False)
ax4.脊椎['left'].set_可见(False)
ax4.勾选参数(底部=假,左侧=假)
对于枚举中的i,p(变化):
#生成4个随机变量
x=np.随机.正常(-2.5,1,p*100)
y=np.随机伽马(2,1.5,p*100)
a=np.随机指数(2,p*100)+7
b=np.随机均匀(14,20,p*100)
plot_prop()
ax1.hist(x,bin=n_bin,facecolor='c')
ax2.hist(y,bin=n_bin,facecolor='c')
ax3.散射(x,a,facecolor='c')
ax4.散射(y、b、facecolor='c')
plt.savefig('../imgs/img'+str(i)+'.png')
ax1.cla()
ax2.cla()
ax3.cla()
ax4.cla()
图=plt.图()
ims=[]
对于范围(100)内的i:
im=plt.imshow(plt.imread('../imgs/img'+str(i)+'.png'),动画=True)
ims.append([im])
ani=动画。ArtistAnimation(图,ims,间隔=200,blit=True)
ani.save('../imgs/animation.mp4',writer=animation.FFMpegFileWriter(),dpi=300)

这是我得到的动画的一个例子

对不起,我不理解你的解决方案。我不想在空间里放东西,我已经有了情节。我需要给它们设置动画。你想在这里设置什么动画?您希望改变哪个变量?开始时定义的4个变量。我想展示他们在每个情节中的积累。随着时间的推移,条状和点状物不断积累。这不可能吗?谢谢你的贡献。代码在VSC中不起任何作用,链接也不起作用,它说“很抱歉,我们无法显示这样的laurge文件”。Github不播放mp4文件,因此您需要下载,但您不必下载。如果您为自己运行代码,并且为了使代码正常运行,您需要更改
plt.savefig(您要保存的位置)
plt.imread(从您想要阅读的地方)