连续绘制数据块
Plot blocks of data in succession
我有一个 运行 时间的数据集,我将其细分为六个月(一月至六月)。我想绘制散点图的动画,在 x 轴上显示距离,在 y 轴上显示时间。
没有任何动画我有:
plt.figure(figsize = (8,8))
plt.scatter(data = strava_df, x = 'Distance', y = 'Elapsed Time', c = col_list, alpha = 0.7)
plt.xlabel('Distance (km)')
plt.ylabel('Elapsed Time (min)')
plt.title('Running Distance vs. Time')
plt.show()
这给了我:
我想要的是绘制第一个月数据的动画,然后延迟第二个月,依此类推。
from matplotlib.animation import FuncAnimation
fig = plt.figure(figsize=(10,10))
ax = plt.axes(xlim=(2,15), ylim=(10, 80))
x = []
y = []
scat = plt.scatter(x, y)
def animate(i):
for m in range(0,6):
x.append(strava_df.loc[strava_df['Month'] == m,strava_df['Distance']])
y.append(strava_df.loc[strava_df['Month'] == m,strava_df['Elapsed Time']])
FuncAnimation(fig, animate, frames=12, interval=6, repeat=False)
plt.show()
这是我想出的方法,但它不起作用。有什么建议吗?
animate
函数应该更新通过调用 scat = ax.scatter(...)
创建的 matplotlib 对象以及 return 该对象作为元组。可以使用 nx2
xy 值数组调用 scat.set_offsets()
来更新位置。可以使用 scat.set_color()
和颜色列表或数组来更新颜色。
假设 col_list
是颜色名称或 rgb 值的列表,代码可能如下所示:
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation
import pandas as pd
import numpy as np
strava_df = pd.DataFrame({'Month': np.random.randint(0, 6, 120),
'Distance': np.random.uniform(2, 13, 120),
'Color': np.random.choice(['blue', 'red', 'orange', 'cyan'], 120)
})
strava_df['Elapsed Time'] = strava_df['Distance'] * 5 + np.random.uniform(0, 5, 120)
fig = plt.figure(figsize=(10, 10))
ax = plt.axes(xlim=(2, 15), ylim=(10, 80))
scat = ax.scatter([], [], s=20)
def animate(i):
x = np.array([])
y = np.array([])
c = np.array([])
for m in range(0, i + 1):
x = np.concatenate([x, strava_df.loc[strava_df['Month'] == m, 'Distance']])
y = np.concatenate([y, strava_df.loc[strava_df['Month'] == m, 'Elapsed Time']])
c = np.concatenate([c, strava_df.loc[strava_df['Month'] == m, 'Color']])
scat.set_offsets(np.array([x, y]).T)
scat.set_color(c)
return scat,
anim = FuncAnimation(fig, animate, frames=12, interval=6, repeat=False)
plt.show()
我有一个 运行 时间的数据集,我将其细分为六个月(一月至六月)。我想绘制散点图的动画,在 x 轴上显示距离,在 y 轴上显示时间。
没有任何动画我有:
plt.figure(figsize = (8,8))
plt.scatter(data = strava_df, x = 'Distance', y = 'Elapsed Time', c = col_list, alpha = 0.7)
plt.xlabel('Distance (km)')
plt.ylabel('Elapsed Time (min)')
plt.title('Running Distance vs. Time')
plt.show()
这给了我:
我想要的是绘制第一个月数据的动画,然后延迟第二个月,依此类推。
from matplotlib.animation import FuncAnimation
fig = plt.figure(figsize=(10,10))
ax = plt.axes(xlim=(2,15), ylim=(10, 80))
x = []
y = []
scat = plt.scatter(x, y)
def animate(i):
for m in range(0,6):
x.append(strava_df.loc[strava_df['Month'] == m,strava_df['Distance']])
y.append(strava_df.loc[strava_df['Month'] == m,strava_df['Elapsed Time']])
FuncAnimation(fig, animate, frames=12, interval=6, repeat=False)
plt.show()
这是我想出的方法,但它不起作用。有什么建议吗?
animate
函数应该更新通过调用 scat = ax.scatter(...)
创建的 matplotlib 对象以及 return 该对象作为元组。可以使用 nx2
xy 值数组调用 scat.set_offsets()
来更新位置。可以使用 scat.set_color()
和颜色列表或数组来更新颜色。
假设 col_list
是颜色名称或 rgb 值的列表,代码可能如下所示:
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation
import pandas as pd
import numpy as np
strava_df = pd.DataFrame({'Month': np.random.randint(0, 6, 120),
'Distance': np.random.uniform(2, 13, 120),
'Color': np.random.choice(['blue', 'red', 'orange', 'cyan'], 120)
})
strava_df['Elapsed Time'] = strava_df['Distance'] * 5 + np.random.uniform(0, 5, 120)
fig = plt.figure(figsize=(10, 10))
ax = plt.axes(xlim=(2, 15), ylim=(10, 80))
scat = ax.scatter([], [], s=20)
def animate(i):
x = np.array([])
y = np.array([])
c = np.array([])
for m in range(0, i + 1):
x = np.concatenate([x, strava_df.loc[strava_df['Month'] == m, 'Distance']])
y = np.concatenate([y, strava_df.loc[strava_df['Month'] == m, 'Elapsed Time']])
c = np.concatenate([c, strava_df.loc[strava_df['Month'] == m, 'Color']])
scat.set_offsets(np.array([x, y]).T)
scat.set_color(c)
return scat,
anim = FuncAnimation(fig, animate, frames=12, interval=6, repeat=False)
plt.show()