从Pandas数据框中的值创建动态箭头图。

4

你好,我正在尝试从数据框中制作一个箭头图的动画。

我在Pandas DataFrame中存储了如下数据:

    QuivXLoc    QuivYLoc    QuivXVal    QuivYVal    QuivColorVal    QuivPlotNum
0   -70.22     -127.241     1.624       -0.879      1.846623        1
1   -61.74     -127.241     -0.973      -0.027      0.973375        1
2   -65.98     -121.835     0.046       2.416       2.416438        1
3   -74.46     -121.835     -0.151      2.673       2.677262        1
4   -78.70     -116.429     1.073       -0.954      1.435773        2

我目前是这样绘制它的,它可以完美地生成每个序列号的单独图表。
for seq in quidf['QuivPlotNum'].unique():
    temp=quidf[quidf['QuivPlotNum']==seq]  ## make subset to plot
    plt.quiver(temp['QuivXLoc'], temp['QuivYLoc'], temp['QuivXVal'], temp['QuivYVal'],        # data
           temp['QuivColorVal'],                   # colour the arrows based on this array
           cmap=cm.jet,     # colour map
           headlength=3)        # length of the arrows

这里有一些格式化图表的附加代码我没有列出。

我想做的是基于迭代我的数据框中的序列数字来对序列进行动画化。我看到的所有Quiver动画示例都涉及通过增加某个标量来缩放先前的函数。

我想生成类似quiver动画的示例,但我尝试了很多次,无法弄清楚如何更改update_quiver使其适用于我的应用程序: 在Python中绘制动态箭头


你是指将箭头动画化,使其逐渐增长到最终长度吗?你尝试过什么? - DisappointedByUnaccountableMod
是的,就像这个例子中的动画展示了随着帧数变化的箭头图。但它是基于标量更新的。我想根据存储在DataFrame中的数据绘制图表。https://dev59.com/a2Ik5IYBdhLWcg3wYtP7 - Justin Gabitzsch
1个回答

6
使用 matplotlib.animation 模块及其 FuncAnimation 类:
import matplotlib.pyplot as plt
import numpy as np
from matplotlib.animation import FuncAnimation
import pandas as pd

# read in the date and group it by the frame number
data = pd.read_csv('data2.csv', index_col=0)
grouped = data.groupby('QuivPlotNum')

# set up the figure
fig = plt.figure()
ax = fig.add_subplot(1, 1, 1)
ax.set_xlim(-200, 200)
ax.set_ylim(-200, 200)

# create empty plot for the update function to manipulate
plot = ax.quiver([], [], [], [], [], cmap='jet', headlength=3)

# create an iterator over the group, next() will return a tuple
# of QuivPlotNum, DataFrame
iterator = iter(grouped)

def update(i):
    # get next thing in the iterator
    key, data = next(iterator)
    # set new x, y coordinates for the plot
    plot.set_offsets(np.column_stack([data.QuivXLoc, data.QuivYLoc]))
    # update vector and color values
    plot.set_UVC(data.QuivXVal, data.QuivYVal, data.QuivColorVal)

# create the animation, update every 1000 ms
ani = FuncAnimation(fig, update, interval=1000)

# show it
plt.show()

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接