I used this question to make an animated scatterplot like this:
import matplotlib.pyplot as plt
import pandas as pd
from matplotlib.collections import PathCollection
df = pd.DataFrame({"x": [1, 2, 6, 4, 5, 6], "y": [1, 4, 36, 16, 25, 36]})
plt.ion()
fig: plt.Figure = plt.figure
ax = fig.subplots()
path_collection: PathCollection = ax.scatter(df.loc[0:2, "x"], df.loc[0:2, "y"])
# Note: I don't use pandas built in DataFrame.plot.scatter function so I can get the PathCollection object to later change the scatterpoints.
fig.canvas.draw()
path_collection.set_offsets([[row.x, row.y] for index, row in df.loc[3:].iterrows()])
# Due to the format of offset (array-like (N,2)) this seems to be the best way to provide the data.
fig.canvas.draw()
This works perfectly but I'd like to have times on the x-axis so I tried changing the above code to this:
import matplotlib.pyplot as plt
import pandas as pd
from matplotlib.collections import PathCollection
df = pd.DataFrame({'time': [pd.Timestamp('2021-02-04 00:00:01'),
pd.Timestamp('2021-02-04 00:00:02'),
pd.Timestamp('2021-02-04 00:00:10'),
pd.Timestamp('2021-02-04 00:00:05'),
pd.Timestamp('2021-02-04 00:00:06'),
pd.Timestamp('2021-02-04 00:00:08')],
'y': [5, 6, 10, 8, 9, 10]})
fig: plt.Figure = plt.figure()
ax = fig.subplots()
sc: PathCollection = ax.scatter(df.loc[0:2, "time"], df.loc[0:2, "y"])
fig.canvas.draw()
sc.set_offsets([[row.time, row.y] for index, row in df.loc[3:].iterrows()])
fig.canvas.draw()
The second to last line throws this error:
TypeError: float() argument must be a string or a number, not 'Timestamp'
. This seems to be caused by the fact that PathCollection
stores it _offsets
as a numpy array which cannot contain a Timestamp
.
So I was wondering, is there a workaround to animate scatterpoints with a time axis?
Thanks in advance.
question from:
https://stackoverflow.com/questions/66048025/matplotlib-animate-scatterplot-with-time-axis 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…