标签:python numpy matplotlib animation random
好吧,我想制作一个可以显示4个不同发行版的动画,但是当我使用gridspec制作子图时,它不起作用,代码如下:
import matplotlib.animation as animation
import matplotlib.pyplot as plt
import numpy as np
%matplotlib notebook
x1 = np.random.normal(-2.5, 1, 10000)
x2 = np.random.gamma(2, 1.5, 10000)
x3 = np.random.exponential(2, 10000)+7
x4 = np.random.uniform(14,20, 10000)
x = [x1, x2, x3, x4]
bins1 = np.arange(-7.5, 2.5, 0.2)
bins2 = np.arange(0, 10, 0.2)
bins3 = np.arange(7, 17, 0.2)
bins4 = np.arange(12, 22, 0.2)
bins = [bins1, bins2, bins3, bins4]
axis1 = [-7.5, 2.5, 0, 0.6]
axis2 = [0, 10, 0, 0.6]
axis3 = [7, 17, 0, 0.6]
axis4 = [12, 22, 0, 0.6]
axis = [axis1, axis2, axis3, axis4]
import matplotlib.gridspec as gridspec
gspec = gridspec.GridSpec(4, 4)
plt.figure()
ax1 = plt.subplot(gspec[0:2, 0:2])
ax2 = plt.subplot(gspec[0:2, 2:])
ax3 = plt.subplot(gspec[2:, 2:])
ax4 = plt.subplot(gspec[2:, 0:2])
ax = [ax1, ax2, ax3, ax4]
for a in ax:
a.spines['right'].set_visible(False)
a.spines['top'].set_visible(False)
gspec.update(wspace = .6, hspace = .6)
def update(curr):
if curr == 500:
a.event_source.stop()
for i in range(len(ax)):
ax[i].cla()
ax[i].hist(x[i][:curr], normed = True, bins = bins[i])
ax[i].axis(axis[i])
ax[i].set_title('n={}'.format(curr))
ax[i].set_ylabel('Normed Frequency')
plt.tight_layout()
fig = plt.gcf()
a = animation.FuncAnimation(fig, update, interval = 10)
在这种情况下,动画不起作用,但有趣的是如果我使用
fig, ((ax1,ax2),(ax3, ax4)) = plt.subplots(2, 2, sharey = True)
ax = [ax1, ax2, ax3, ax4]
制作ax1,ax2,ax3,ax4并且不改变其他任何东西,代码运行良好,那么在使用gridspec时是否需要了解一些细节?
解决方法:
GridSpec参数wspace和hspace与plt.tight_layout不兼容.您可以使用tight_layout或指定间距.
如果您想使用GridSpec,您有两个选择:
>删除plt.tight_layout行
>删除行gspec.update(wspace = .6,hspace = .6)
原因
fig, ((ax1,ax2),(ax3, ax4)) = plt.subplots(2, 2, sharey = True)
ax = [ax1, ax2, ax3, ax4]
因此,您不需要设置任何wspace或hspace.
标签:python,numpy,matplotlib,animation,random 来源: https://codeday.me/bug/20191003/1846519.html
本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享; 2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关; 3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关; 4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除; 5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。