【发布时间】:2022-01-25 02:43:35
【问题描述】:
我在一个图形上有几个数据框图形。 X 轴是时间戳,格式为:dd/mm/yy HH:MM:SS
问题是时间轴不在同一个刻度上,我不能把它们放在同一个刻度上。我试过了,但它不起作用:
df1:
Timestamp,Value
2018-11-13 00:26:43.267725,68.9999980926514
2018-11-13 00:26:52.194564,488.389312744141
2018-11-13 00:26:52.479555,549.0
2018-11-13 00:27:11.812900,535.6854553222661
2018-11-13 00:27:12.080380,549.0
2018-11-13 00:27:12.348114,509.51171875
2018-11-13 00:27:20.346217,47.54024255275726
2018-11-13 00:28:39.572289,68.9999980926514
2018-11-13 00:28:46.264423,86.6078643798828
2018-11-13 00:28:50.782171,549.0
2018-11-13 00:29:12.807073,68.9999980926514
df2:
Timestamp,Value
2018-12-10 20:22:30.088260,120.8003616333008
2018-12-10 20:22:31.893382,549.0
2018-12-10 20:22:49.872620,478.66650390625
2018-12-10 20:22:50.129706,427.010375976562
2018-12-10 20:22:50.437430,353.003936767578
2018-12-10 20:22:50.762730,277.003540039062
2018-12-10 20:22:51.081120,232.50846862793
2018-12-10 20:22:51.338931,198.633895874023
2018-12-10 20:22:51.677225,164.06259918212902
2018-12-10 20:22:52.002505,147.7807312011719
cols = 1
rows = 2
nb_figs = rows
# create the figure with multiple axes
fig, axes = plt.subplots(nrows=rows, ncols=cols, figsize=(10, 13))
ax = df1.plot(x='Timestamp', y='Value', ax=axes[0])
xlocator = mdates.SecondLocator(interval = 15)
ax.xaxis.set_major_locator(xlocator)
ax.xaxis.set_major_formatter(mdates.DateFormatter('%d/%m/%y %H:%M:%S'))
ax = df2.plot(x='Timestamp', y='Value', ax=axes[1])
xlocator = mdates.SecondLocator(interval = 15)
ax.xaxis.set_major_locator(xlocator)
ax.xaxis.set_major_formatter(mdates.DateFormatter('%d/%m/%y %H:%M:%S'))
结果是:
我希望这两个图跨越相同的持续时间(比如 20 分钟),不一定显示相同的时间间隔?我错过了一个参数吗?还是我需要其他方法?
【问题讨论】:
-
如何创建情节?尝试使用
fig, axs = plt.subplots(2, 1, sharex=True, sharey=True) -
但不可能分享 X,因为我们不在同一日期和时间
-
你能创建一个可重现的例子吗?
-
是的,我刚刚向您提供了数据
标签: python pandas datetime matplotlib