我想依次绘制一系列的x,y坐标,同时清楚地标记指定的坐标。“市场”似乎允许用户在matplotlib图中这样做,但是,当我在动画中提供这个属性时,我会收到错误'ValueError:markevery是可迭代的,但不是numpy幻想索引的有效形式‘。有什么想法吗?
我的实际“mark_on”数组会长得多,所以我认为在这里使用行集是不合理的。
frames = 100
def update_pos(num,data,line):
line.set_data(data[...,:num])
return line,
def traj_ani(data):
fig_traj = plt.figure()
l,= plt.plot([],[],'b', markevery = mark_on, marker = '*')
plt.xlim(-90,90)
plt.ylim(-90,90)
pos_ani = animation.FuncAnimation(fig_traj, update_pos, frames = np.shape(data)[1], fargs = (data,l),
interval = 20, blit = True)
pos_ani.save('AgentTrajectory.mp4')
data = pd.read_csv('xy_pos.csv', header = None, skiprows = [0])
data = np.asarray(data)
mark_on = [20, 50, 100, 300, 600]
traj_ani(data)谢谢!
下面是一个完整的、实用的动画示例:
import matplotlib
import matplotlib.pyplot as plt
import matplotlib.animation as animation
import csv
import pandas as pd
import numpy as np
Writer = animation.writers['ffmpeg']
writer = Writer(fps=2000, metadata=dict(artist='Me'), bitrate=1800)
def update_pos(num,data,line):
line.set_data(data[...,:num])
return line,
def traj_ani(data):
fig_traj = plt.figure()
l,= plt.plot([],[],'b')
plt.xlim(0,1)
plt.ylim(0,1)
pos_ani = animation.FuncAnimation(fig_traj, update_pos, frames = 25, fargs = (data,l),
interval = 200, blit = True)
pos_ani.save('AgentTrajectory.mp4')
data = np.random.rand(2,25)
traj_ani(data)在我的完整代码中,我想指定某些帧,它的x坐标应该用一个特殊字符或者用不同的颜色来标记。
发布于 2017-03-30 07:09:33
为市场设置一个独立的列表似乎是有问题的,其中包含的索引没有出现在所绘制的数组中。例如,如果所绘制的数组有3个元素,但为市场设置的列表包含一个索引5,则会发生ValueError。
解决方案需要是在每次迭代中设置市场列表,并确保它只包含有效的indize。
import matplotlib.pyplot as plt
import matplotlib.animation as animation
import numpy as np
mark_on = np.array([2,5,6,13,17,24])
def update_pos(num,data,line):
line.set_data(data[...,:num])
mark = mark_on[mark_on < num]
line.set_markevery(list(mark))
return line,
def traj_ani(data):
fig_traj = plt.figure()
l,= plt.plot([],[],'b', markevery = [], marker = '*', mfc="red", mec="red", ms=15)
plt.xlim(0,1)
plt.ylim(0,1)
pos_ani = animation.FuncAnimation(fig_traj, update_pos, frames = 25, fargs = (data,l),
interval = 200, blit = True)
plt.show()
data = np.random.rand(2,25)
traj_ani(data)https://stackoverflow.com/questions/43100009
复制相似问题