在动画图上动态显示最后一个数据值
Dynamically showing the last data value on an animated plot
在这个动画情节之上,,为了让它更形象,我希望值显示在点的顶部,但只针对最后的数据,随着动画的进行。
下面的代码显示了所有的注释,因为动画添加了数据,所以最后很乱...谁能帮我解决这个问题?
我试图在注释的执行下面添加 plt.pause()
和 remove()
,但结果注释总是在数据点之前....我不知道为什么.. ..
import numpy as np
from matplotlib.animation import FuncAnimation
from matplotlib import pyplot as plt
def collatz(k):
seq = [k]
while seq[-1] > 1:
if k % 2 == 0:
seq.append(k/2)
else:
seq.append(3*k+1)
k = seq[-1]
return seq
y= collatz(22)
x = list(range(len(y)))
fig = plt.figure()
plt.xlim(1,len(y))
plt.ylim(1,max(y))
draw, = plt.plot([],[], marker='o', markersize='4', color='magenta')
def update(idx):
draw.set_data(x[:idx], y[:idx])
plt.gca()
ann = plt.annotate(f'{y[idx]:.1f}', (x[idx],y[idx]), textcoords="offset points", xytext=(0,0.5), ha="center")
#plt.pause(0.5)
#ann.remove()
return draw,
a = FuncAnimation(fig, update, frames=len(x), interval=30, repeat=False)
plt.show()
您应该将文本放置为 matplotlib.text
,而不是注释,return 以及 draw
:
from matplotlib.animation import FuncAnimation
from matplotlib import pyplot as plt
def collatz(k):
seq = [k]
while seq[-1] > 1:
if k%2 == 0:
seq.append(k/2)
else:
seq.append(3*k + 1)
k = seq[-1]
return seq
y = collatz(22)
x = list(range(len(y)))
fig = plt.figure()
plt.xlim(1, len(y))
plt.ylim(1, max(y))
draw, = plt.plot([], [], marker = 'o', markersize = '4', color = 'magenta')
ax = plt.gca()
text = ax.text(0.5, 0.5, '')
def update(idx):
draw.set_data(x[:idx + 1], y[:idx + 1])
text.set_text(f'{y[idx]:.1f}')
text.set_position((x[idx], y[idx]))
return draw, text,
a = FuncAnimation(fig, update, frames = len(x), interval = 30, repeat = False)
plt.show()
在这个动画情节之上,
下面的代码显示了所有的注释,因为动画添加了数据,所以最后很乱...谁能帮我解决这个问题?
我试图在注释的执行下面添加 plt.pause()
和 remove()
,但结果注释总是在数据点之前....我不知道为什么.. ..
import numpy as np
from matplotlib.animation import FuncAnimation
from matplotlib import pyplot as plt
def collatz(k):
seq = [k]
while seq[-1] > 1:
if k % 2 == 0:
seq.append(k/2)
else:
seq.append(3*k+1)
k = seq[-1]
return seq
y= collatz(22)
x = list(range(len(y)))
fig = plt.figure()
plt.xlim(1,len(y))
plt.ylim(1,max(y))
draw, = plt.plot([],[], marker='o', markersize='4', color='magenta')
def update(idx):
draw.set_data(x[:idx], y[:idx])
plt.gca()
ann = plt.annotate(f'{y[idx]:.1f}', (x[idx],y[idx]), textcoords="offset points", xytext=(0,0.5), ha="center")
#plt.pause(0.5)
#ann.remove()
return draw,
a = FuncAnimation(fig, update, frames=len(x), interval=30, repeat=False)
plt.show()
您应该将文本放置为 matplotlib.text
,而不是注释,return 以及 draw
:
from matplotlib.animation import FuncAnimation
from matplotlib import pyplot as plt
def collatz(k):
seq = [k]
while seq[-1] > 1:
if k%2 == 0:
seq.append(k/2)
else:
seq.append(3*k + 1)
k = seq[-1]
return seq
y = collatz(22)
x = list(range(len(y)))
fig = plt.figure()
plt.xlim(1, len(y))
plt.ylim(1, max(y))
draw, = plt.plot([], [], marker = 'o', markersize = '4', color = 'magenta')
ax = plt.gca()
text = ax.text(0.5, 0.5, '')
def update(idx):
draw.set_data(x[:idx + 1], y[:idx + 1])
text.set_text(f'{y[idx]:.1f}')
text.set_position((x[idx], y[idx]))
return draw, text,
a = FuncAnimation(fig, update, frames = len(x), interval = 30, repeat = False)
plt.show()