gpt4 book ai didi

python - 注释箭头 Prop 中间的动画列表

转载 作者:太空宇宙 更新时间:2023-11-04 04:48:13 25 4
gpt4 key购买 nike

我的目标是在注释函数中间为 df 设置动画。我可以让箭头进行动画处理,并且 df 的第一个值出现但不使用更新的坐标进行动画处理。为此,我将 label.set_text 更改为 (Number[i+1]) 但这只会在第一帧的正确位置显示数字。由于未调用新坐标,因此位置不会更新。我尝试运行此代码来更新坐标,但它没有显示任何内容?

import matplotlib.pyplot as plt
import matplotlib.animation as animation
import numpy as np
import random
from functools import partial
import pandas as pd

one_sample = partial(random.sample, range(100), 10)
a_data = [one_sample() for _ in range(1000)]
b_data = [one_sample() for _ in range(1000)]

df = pd.DataFrame(np.random.randint(0,100,size=(100, 1)), columns=list('A'))

fig, ax = plt.subplots(figsize = (8,6))
ax.set_xlim(0,100)
ax.set_ylim(0,100)

arrow = ax.annotate('', xy = (a_data[0][0], b_data[0][0]), xytext = (a_data[0][1],b_data[0][1]), arrowprops = {'arrowstyle': "<->", 'color':'black'}, ha = 'center')

Number = df[A']
label = plt.text(a_data[0][0], b_data[0][0], Number, fontsize = 8, ha = 'center')

def animate(i) :
arrow_start = (a_data[0+i][0], b_data[0+i][0])
arrow_end = (a_data[0+i][1], b_data[0+i][1])
arrow.set_position(arrow_start)
arrow.xy = arrow_end
label.set_text(a_data[0+i][0], b_data[0+i][0])

ani = animation.FuncAnimation(fig, animate,
interval = 500, blit = False)

plt.draw()

最佳答案

虽然您可以使用 plt.text 来显示标签,但您并不需要它。 ax.annotate 可以生成标签和箭头。您可以将标签字符串指定为 ax.annotate 的第一个参数,

arrow = ax.annotate(Number[0], xy=(a_data[0][0], b_data[0][0]), ...

您可以通过调用 arrow.set_text 来更改标签:

arrow.set_text(Number[i])

import matplotlib.pyplot as plt
import matplotlib.animation as animation
import numpy as np
import random
from functools import partial
import pandas as pd

one_sample = partial(random.sample, range(100), 10)
a_data = [one_sample() for _ in range(1000)]
b_data = [one_sample() for _ in range(1000)]

df = pd.DataFrame(np.random.randint(0, 100, size=(100, 1)), columns=list('A'))

fig, ax = plt.subplots(figsize=(8, 6))
ax.set_xlim(0, 100)
ax.set_ylim(0, 100)

Number = df['A']
arrow = ax.annotate(Number[0], xy=(a_data[0][0], b_data[0][0]),
xytext=(a_data[0][1], b_data[0][1]),
arrowprops={'arrowstyle': "<->", 'color': 'black'}, ha='center')

def animate(i):
arrow_start = (a_data[0 + i][0], b_data[0 + i][0])
arrow_end = (a_data[0 + i][1], b_data[0 + i][1])
arrow.set_position(arrow_start)
arrow.xy = arrow_end
arrow.set_text(Number[i])
return [arrow]

ani = animation.FuncAnimation(fig, animate, interval=500, blit=True)
plt.show()

enter image description here


要将标签放在箭头中间,我相信您需要使用 plt.text(或再次调用 ax.annotate)。要移动 plt.text 生成的标签,请调用 label.set_position:

import matplotlib.pyplot as plt
import matplotlib.animation as animation
import numpy as np
import random
import math
from functools import partial
import pandas as pd

one_sample = partial(random.sample, range(100), 10)
a_data = [one_sample() for _ in range(1000)]
b_data = [one_sample() for _ in range(1000)]
df = pd.DataFrame(np.random.randint(0, 100, size=(100, 1)), columns=list('A'))
Number = df['A']

data = np.stack([a_data, b_data], axis=2)
# a_data and b_data contain more data than we are actually using,
# so let's crop `data` to make the following code simpler:
data = data[:, :2, :]
middle = data.mean(axis=1)
# find the direction perpendicular to the arrow
perp_dir = (data[:, 0] - data[:, 1]).astype('float')
perp_dir = np.array((-perp_dir[:, 1], perp_dir[:, 0]))
perp_dir /= np.sqrt((perp_dir**2).sum(axis=0))
perp_dir = perp_dir.T
# shift middle by a little bit in the perpendicular direction
offset = 3.0
middle += offset * perp_dir

fig, ax = plt.subplots(figsize=(8, 6))
ax.set_xlim(0, 100)
ax.set_ylim(0, 100)

arrow = ax.annotate('', xy=data[0, 0],
xytext=data[0, 1],
arrowprops={'arrowstyle': "<->", 'color': 'black'},
ha='center')

label = plt.text(middle[0, 0], middle[0, 1], Number[0], fontsize = 8,
ha = 'center')

def animate(i):
arrow_start = data[i, 0]
arrow_end = data[i, 1]
arrow.set_position(arrow_start)
arrow.xy = arrow_end
label.set_text(Number[i])
label.set_position(middle[i])
return [arrow, label]

ani = animation.FuncAnimation(fig, animate, interval=500, blit=True)
plt.show()

enter image description here

关于python - 注释箭头 Prop 中间的动画列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48999869/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com