绘制具有不同标签的时间戳的时间线
Plot timelines of timestamps with different labels
我有一些来自不同来源的带时间戳的数据,我想通过在平行时间线上绘制点来可视化这些数据。
I want something that looks like this
来自如下所示的数据:
id | type | timestamp
----------------------
1 | A | 2022-03-11 09:01:00
2 | B | 2022-03-12 13:13:00
3 | C | 2022-03-12 20:05:00
4 | B | 2022-03-12 01:24:00
5 | A | 2022-03-12 13:03:00
6 | B | 2022-03-13 11:45:00
最好的方法是什么?
根据图片,以下是实现方法。我已经获取了你上面问题中的数据,图表 X-axis 具有等间隔分布的日期时间,而 Y-axis 具有类型。
代码
import pandas as pd
import matplotlib.pyplot as plt
from datetime import datetime
df = pd.DataFrame([['A', '2022-03-11 09:01:00'], ['B', '2022-03-12 13:13:00'], ['C', '2022-03-12 20:05:00'],
['B', '2022-03-12 01:24:00'], ['A', '2022-03-12 13:03:00'], ['B', '2022-03-13 11:45:00']],
columns=['type', 'timestamp'])
df['timestamp'] = pd.to_datetime(df['timestamp'])
fig,ax = plt.subplots()
ax.plot_date(df["timestamp"].values, df["type"].values)
xmin, xmax = ax.get_xlim()
ax.set_xticks(np.round(np.linspace(xmin, xmax, 3), 2))
输出
我有一些来自不同来源的带时间戳的数据,我想通过在平行时间线上绘制点来可视化这些数据。
I want something that looks like this
来自如下所示的数据:
id | type | timestamp
----------------------
1 | A | 2022-03-11 09:01:00
2 | B | 2022-03-12 13:13:00
3 | C | 2022-03-12 20:05:00
4 | B | 2022-03-12 01:24:00
5 | A | 2022-03-12 13:03:00
6 | B | 2022-03-13 11:45:00
最好的方法是什么?
根据图片,以下是实现方法。我已经获取了你上面问题中的数据,图表 X-axis 具有等间隔分布的日期时间,而 Y-axis 具有类型。
代码
import pandas as pd
import matplotlib.pyplot as plt
from datetime import datetime
df = pd.DataFrame([['A', '2022-03-11 09:01:00'], ['B', '2022-03-12 13:13:00'], ['C', '2022-03-12 20:05:00'],
['B', '2022-03-12 01:24:00'], ['A', '2022-03-12 13:03:00'], ['B', '2022-03-13 11:45:00']],
columns=['type', 'timestamp'])
df['timestamp'] = pd.to_datetime(df['timestamp'])
fig,ax = plt.subplots()
ax.plot_date(df["timestamp"].values, df["type"].values)
xmin, xmax = ax.get_xlim()
ax.set_xticks(np.round(np.linspace(xmin, xmax, 3), 2))
输出