matplotlib datetime false interpreted

问题描述 投票:0回答:0

我使用 matplotlib 在子图中绘制线条。 xaxis 是格式为“%y-%m-%d %HH:%MM:%SS”的时间戳。 timestamp 的范围在 2323-01-01 00:00:00 到 2023-03-31 22:59:59 之间。但是 matplotlib 可以将其解释为一整年(下图)。你知道怎么修吗?提前致谢。

import pandas as pd
import matplotlib.pyplot as plt
from matplotlib.dates import DateFormatter
import matplotlib.dates as mdates

df = pd.read_csv('Data.csv', index_col=0)
df['Timestamp'] = pd.to_datetime(df['Timestamp'])
df.set_index('Timestamp', inplace=True)
fig, (ax1, ax2) = plt.subplots(2, 1, figsize=(20,10), sharex=True)
ax1.plot(df.index.values, df['Value_1'])
ax1.set(ylabel='Value_1', title='Diagramm')
ax2.plot(df.index.values, df['Value_2'])
ax2.set(xlabel='Timestamp')
date_form = DateFormatter('%d.%m')
ax2.xaxis.set_major_formatter(date_form)
ax2.xaxis.set_major_locator(mdates.WeekdayLocator(interval=4))
plt.show()

matplotlib time-series
© www.soinside.com 2019 - 2024. All rights reserved.