Python 如何使用动画使用matplotlib更新打印标题?
下面是我的代码。为什么标题不是每勾一次就更新?我读了这篇文章:但它没有用Python 如何使用动画使用matplotlib更新打印标题?,python,matplotlib,Python,Matplotlib,下面是我的代码。为什么标题不是每勾一次就更新?我读了这篇文章:但它没有用 #! /usr/bin/python3 import matplotlib import numpy as np import matplotlib.pyplot as plt import matplotlib.animation as animation import time import random as rr plt.rc('grid', color='#397939', linewidth=1, lines
#! /usr/bin/python3
import matplotlib
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation
import time
import random as rr
plt.rc('grid', color='#397939', linewidth=1, linestyle='-')
plt.rc('xtick', labelsize=10)
plt.rc('ytick', labelsize=5)
width, height = matplotlib.rcParams['figure.figsize']
size = min(width, height)
fig = plt.figure(figsize=(size, size))
ax = fig.add_axes([0.1, 0.1, 0.8, 0.8], polar=True, facecolor='#cfd98c')
ax.set_rmax(20.0)
plt.grid(True)
plt.title("")
def data_gen(t=0):
tw = 0
phase = 0
ctn = 0
while True:
ctn += 1
if ctn == 1000:
phase=round(rr.uniform(0,180),0)
tw = round(rr.uniform(0,20),0)
ctn = 0
yield tw, phase
def update(data):
tw, phase = data
print(data)
ax.set_title("|TW| = {}, Angle: {}°".format(tw, phase))
arr1 = plt.arrow(phase, 0, 0, tw, alpha = 0.5, width = 0.080,
edgecolor = 'red', facecolor = 'red', lw = 2, zorder = 5)
return arr1,
ani = animation.FuncAnimation(fig, update, data_gen, interval=100, blit=True, repeat=False)
plt.show()
编辑。1
在@eyllanesc answer之后,我编辑了这段代码:
def data_gen(t=0):
tw = 10
phase = 0
ctn = 0
while True:
if phase < 2*180:
phase += 1
else:
phase=0
yield tw, phase
def update(data):
tw, phase = data
angolo = phase /180 * np.pi
print("|TW| = {}, Angle = {}°".format(tw,phase))
ax.set_title("|TW| = {}, Angle: {}°".format(tw, phase))
arr1 = plt.arrow(angolo, 0, 0, tw, alpha = 0.5, width = 0.080, edgecolor = 'red', facecolor = 'red', lw = 2, zorder = 5)
plt.draw()
return arr1,
def数据生成(t=0):
tw=10
相位=0
ctn=0
尽管如此:
如果相位<2*180:
相位+=1
其他:
相位=0
产量tw,相位
def更新(数据):
tw,相位=数据
angolo=相位/180*np.pi
打印(“|TW |={},角度={}°”。格式(TW,相位))
ax.set_title(“|TW |={},角度:{}°”。格式(TW,相位))
arr1=plt.箭头(angolo,0,0,tw,alpha=0.5,宽度=0.080,边色='red',面色='red',lw=2,zorder=5)
plt.draw()
返回arr1,
现在文本工作正常,但箭头更新不是“流动的”(每次更新都会出现和消失)。在
函数动画中使用blit=True
时会出现问题。这将存储背景并仅更新更新更新函数返回的艺术家。但是,恢复的背景将覆盖标题,因为它位于轴之外
可能的解决办法是:
将标题放在轴内
不要使用blitting
可能使用ArtistAnimation
而不是functanimation
也可以。但我还没有测试过
请注意,在更新功能中使用plt.draw
或类似功能(如其他答案中所建议的)是没有意义的,因为它会破坏使用blitting的所有建议,并使动画比不使用blitting时更慢
1.将标题放在轴内(blit=True
)
可以使用位于轴内的title=ax.text(..)
代替轴外的标题。可以为每次迭代更新此文本。title.set_text(“…”
),然后必须由更新函数返回(return arr1,title,
)
更好地解释自己,你的问题是什么?我需要在每一帧更新中,情节的标题也会更新。如果箭头已更新但标题未更新,我是否正确?请查看我的答案
import matplotlib
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation
plt.rc('grid', color='#397939', linewidth=1, linestyle='-')
plt.rc('xtick', labelsize=10)
plt.rc('ytick', labelsize=5)
width, height = matplotlib.rcParams['figure.figsize']
size = min(width, height)
fig = plt.figure(figsize=(size, size))
ax = fig.add_axes([0.1, 0.1, 0.8, 0.8], polar=True, facecolor='#cfd98c')
ax.set_rmax(20.0)
plt.grid(True)
title = ax.text(0.5,0.85, "", bbox={'facecolor':'w', 'alpha':0.5, 'pad':5},
transform=ax.transAxes, ha="center")
def data_gen(t=0):
tw = 10
phase = 0
while True:
if phase < 2*180:
phase += 2
else:
phase=0
yield tw, phase
def update(data):
tw, phase = data
title.set_text(u"|TW| = {}, Angle: {}°".format(tw, phase))
arr1 = ax.arrow(np.deg2rad(phase), 0, 0, tw, alpha = 0.5, width = 0.080,
edgecolor = 'red', facecolor = 'red', lw = 2, zorder = 5)
return arr1,title,
ani = animation.FuncAnimation(fig, update, data_gen, interval=100, blit=True, repeat=False)
plt.show()
import matplotlib
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation
plt.rc('grid', color='#397939', linewidth=1, linestyle='-')
plt.rc('xtick', labelsize=10)
plt.rc('ytick', labelsize=5)
width, height = matplotlib.rcParams['figure.figsize']
size = min(width, height)
fig = plt.figure(figsize=(size, size))
ax = fig.add_axes([0.1, 0.1, 0.8, 0.8], polar=True, facecolor='#cfd98c')
ax.set_rmax(20.0)
plt.grid(True)
def data_gen(t=0):
tw = 10
phase = 0
while True:
if phase < 2*180:
phase += 2
else:
phase=0
yield tw, phase
arr1 = [None]
def update(data):
tw, phase = data
ax.set_title(u"|TW| = {}, Angle: {}°".format(tw, phase))
if arr1[0]: arr1[0].remove()
arr1[0] = ax.arrow(np.deg2rad(phase), 0, 0, tw, alpha = 0.5, width = 0.080,
edgecolor = 'red', facecolor = 'red', lw = 2, zorder = 5)
ani = animation.FuncAnimation(fig, update, data_gen, interval=100, blit=False, repeat=False)
plt.show()