【问题标题】:How to scale the x-axis (in datetime format) of dataframes graphs to the same scale ? Python Pandas如何将数据框图的 x 轴(日期时间格式)缩放到相同的比例?蟒蛇熊猫
【发布时间】: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


【解决方案1】:

你可以take两个Axes的查看间隔,把较小的放大来匹配较大的,然后set围绕其中心的新间隔:

fig, axes = plt.subplots(nrows=2, figsize=(10, 8), layout='constrained')

ax1 = df1.plot(x='Timestamp', y='Value', ax=axes[0])
xlocator = mdates.SecondLocator(interval = 15)
ax1.xaxis.set_major_locator(xlocator)
ax1.xaxis.set_major_formatter(mdates.DateFormatter('%d/%m/%y %H:%M:%S'))

ax2 = df2.plot(x='Timestamp', y='Value', ax=axes[1])
xlocator = mdates.SecondLocator(interval = 15)
ax2.xaxis.set_major_locator(xlocator)
ax2.xaxis.set_major_formatter(mdates.DateFormatter('%d/%m/%y %H:%M:%S'))

vi1 = ax1.xaxis.get_view_interval()
vi2 = ax2.xaxis.get_view_interval()
if vi1.ptp() > vi2.ptp():
    ax2.set_xlim(vi2.mean() - vi1.ptp() / 2, vi2.mean() + vi1.ptp() / 2)
else:
    ax1.set_xlim(vi1.mean() - vi2.ptp() / 2, vi1.mean() + vi2.ptp() / 2)

您也可以使用get_view_interval() 而不是get_view_interval(),但前者的优点是将区间作为一个numpy 数组返回,这使得新区间的计算更容易一些。


如果您不想居中但两个数据都从左边缘开始,您可以简单地使用:
if vi1.ptp() > vi2.ptp():
    ax2.set_xlim(vi2[0], vi2[0] + vi1.ptp())
else:
    ax1.set_xlim(vi1[0], vi1[0] + vi2.ptp())

【讨论】:

    猜你喜欢
    • 2020-12-16
    • 2018-12-13
    • 1970-01-01
    • 2021-02-18
    • 1970-01-01
    • 1970-01-01
    • 2021-11-25
    • 1970-01-01
    • 2019-09-12
    相关资源
    最近更新 更多