我正在嘗試為我用 GridSpec 設定的 4 個子圖設定影片,在其中我想表示我用亂數設定的 4 個分布,我設定子圖、分布和影片函式的代碼如下:
import matplotlib
import matplotlib.animation as animation
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
#Create the figure and set the grid:
fig = plt.figure()
gspec = matplotlib.gridspec.GridSpec(6,6)
top_left = plt.subplot(gspec[0:2,0:3])
top_right = plt.subplot(gspec[0:2,3:], sharey = top_left)
bottom_left = plt.subplot(gspec[3:5,0:3], sharey = top_left)
bottom_right = plt.subplot(gspec[3:5,3:], sharey = top_left)
#Create the random variables:
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)
#Create the function:
n = 10000-100
def anim(curr, subplot = None, dist = None):
if curr >= 100:
if curr == n 100:
a.event_source.stop()
subplot.cla()
bins = np.arange(p1,p2,step)
subplot.hist(dist[:curr], bins = bins)
subplot.gca().set_ylabel('Frequency')
subplot.gca().set_xlabel('Value')
subplot.annotate('n={}'.format(curr 100))
#Create the necessary lists for looping:
subplots = [top_left, top_right, bottom_left, bottom_right]
bins = [np.arange(-6,2,0.5),np.arange(-1,15,0.5), np.arange(5,20,0.5), np.arange(13.5,20.5,0.5)]
dists = [x2,x2,x3,x4]
然后,我嘗試使用animation.FuncAnimation具有以下功能的函式進行回圈:
for i in range(3):
a = animation.FuncAnimation(fig, anim(subplot = subplots[i], dist = dists[i]))
但我收到以下錯誤:
-------------------------------------------------- ------------------------- TypeError Traceback (most recent call last) in () 2 3 for i in range(3): ---- > 4 a = animation.FuncAnimation(fig, anim(subplot = subplots[i], dist = diss[i]))
型別錯誤:anim() 缺少 1 個必需的位置引數:'curr'
Why is it asking me to specify curr? I thought the FuncAnimation function was able to understand itself that that's the argument to loop through. I think I am not understanding well the functioning of the animation module
NOTE: I have to say that I tried a similar code to this one, but just for one single plot, without any grid, and without looping. In that case, I didn't have to specify subplot nor dist arguments within anim function because it wasn't necessary to include them when defining the anim function, since as I only had to pass one plot I just specify those values within the function, in that case, I didn't get any error, but now that I have to specify more arguments I am getting it.
uj5u.com熱心網友回復:
你必須像這樣在 FUncAnimation 中呼叫沒有引數的函式:
a = animation.FuncAnimation(fig, anim)
所以像這樣改變你的代碼才能做到:
subplots = [top_left, top_right, bottom_left, bottom_right]
bins = [np.arange(-6,2,0.5),np.arange(-1,15,0.5), np.arange(5,20,0.5), np.arange(13.5,20.5,0.5)]
dists = [x2,x2,x3,x4]
n = 10000-100
def anim(curr):
for i in range(3):
if curr >= 100:
if curr == n 100:
a.event_source.stop()
subplots[i].cla()
subplots[i].hist(dists[i][:curr], bins = bins[i])
subplots[i].gca().set_ylabel('Frequency')
subplots[i].gca().set_xlabel('Value')
subplots[i].annotate('n={}'.format(curr 100))
a = animation.FuncAnimation(fig, anim)
此外,您必須更改 anim 函式,根據檔案 anim 應該是一個回傳藝術家物件序列的函式。在您的情況下,anim 不會回傳任何內容。所以你必須修復它
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/315328.html
