我無法弄清楚為什么在更新滑塊時繪圖不重繪 。我正在使用 Jupiter 筆記本,并選擇帶有“nbAgg”引數的后端。
初始化代碼:
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation
from matplotlib import use as m_use
from matplotlib.widgets import Slider
m_use('nbAgg')
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)
fig, ((ax1, ax2), (ax3, ax4)) = plt.subplots(2, 2,figsize=(7, 5))
plt.subplots_adjust(left=0.1,right=.8)
有一個影片哪個午餐這個功能:
def updateData(curr,divider=7):
global ax1, ax2, ax3, ax4
for ax in (ax1, ax2, ax3, ax4):
ax.cla()
if curr <= 679 :
my_b = int(np.around(curr/divider) 3)
else : my_b = 100
multi = 100
ax1.hist(x1[:curr*multi],bins=my_b)
ax2.hist(x2[:curr*multi],bins=my_b)
ax3.hist(x3[:curr*multi],bins=my_b)
ax4.hist(x4[:curr*multi],bins=my_b)
fig.suptitle('Frame {} on 100'.format((curr 1)))
return None
影片:
simulation = animation.FuncAnimation(fig=fig, func=updateData, frames=10,
blit=False, interval=1, repeat=False)
這是卡住我的滑塊:
slider_ax = plt.axes([.9, 0.45, 0.01, 0.3])
slider = Slider(ax=slider_ax,label='Divider \nfor bins',valmin=1,valmax=15, valinit=7, orientation='vertical',valfmt='%.0f',track_color='black',facecolor='red',
handle_style={'facecolor':'k','edgecolor':'#86a2cf','size':20})
def update():
anim_2 = animation.FuncAnimation(fig=fig, func=updateData, frames=20,
blit=False, interval=1, repeat=False)
以下功能無法按預期作業:
slider.on_changed(update)
simulation = animation.FuncAnimation(fig=fig, func=updateData, frames=10,
blit=False, interval=1, repeat=False)
plt.show()
uj5u.com熱心網友回復:
您可以嘗試以下方法:
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation
from matplotlib.widgets import Slider
from ipywidgets import interact
import ipywidgets as widget
%matplotlib widget
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)
(fig, ((ax1, ax2), (ax3, ax4))) = plt.subplots(2, 2, figsize=(7, 5))
plt.subplots_adjust(left=0.1, right=.8)
divider = 7
def updateData(curr):
global ax1, ax2, ax3, ax4, divider
for ax in (ax1, ax2, ax3, ax4):
ax.cla()
if (curr <= 679):
my_b = int(np.around(curr / divider) 3)
else:
my_b = 100
multi = 100
ax1.hist(x1[:curr * multi], bins=my_b)
ax2.hist(x2[:curr * multi], bins=my_b)
ax3.hist(x3[:curr * multi], bins=my_b)
ax4.hist(x4[:curr * multi], bins=my_b)
fig.suptitle('Frame {} on 100'.format(curr 1))
plt.tight_layout()
def update(val):
global divider
divider = val
anim_2 = animation.FuncAnimation(fig=fig, func=updateData, frames=20, blit=False, interval=1, repeat=False)
fig.canvas.draw()
slider = interact(update, val=widget.IntSlider(min=1, max=15, value=7))
simulation = animation.FuncAnimation(fig=fig, func=updateData, frames=10, blit=False, interval=1,repeat=False)
display(slider)
有幾點需要注意:
要使用 matplotlib 在 Jupyter Notebook 中繪制互動式圖形,您需要安裝
ipympl之后才能使用魔法函式在 Jupyter Notebook 中繪制互動式 Matplotlib 圖形%matplotlib widget。要在影片存盤在變數中后更新圖形,您需要呼叫
fig.canvas.draw()以重繪圖形。您的實作沒有使用(更新的)滑塊值作為函式的引數,
update因為update不需要任何引數即可作業。但是,小部件通常用于呼叫帶有引數的函式。例如,檢查互動如何在 Jupyter Notebook 中提供介面來呼叫具有不同引數值的相同函式。鑒于此,我修改了
update使用滑塊值來更新全域變數值的函式divider。
這意味著,當updateData使用FuncAnimationwithframes作為整數呼叫函式時,updateData將使用來自 的值重復呼叫range(frames)。這意味著,在每次呼叫updateDatabyFuncAnimation時,curr都會從 中獲取一個值range(frames)。
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/488241.html
標籤:Python matplotlib 动画 相互作用
上一篇:如何控制花卉影片?
