要在Matplotlib中繪制時序數據并處理時間格式,可以按照以下步驟進行:
import matplotlib.pyplot as plt
import pandas as pd
data = {'date': ['2021-01-01', '2021-01-02', '2021-01-03', '2021-01-04', '2021-01-05'],
'value': [10, 15, 20, 25, 30]}
df = pd.DataFrame(data)
df['date'] = pd.to_datetime(df['date'])
plt.plot(df['date'], df['value'])
plt.gca().xaxis.set_major_formatter(mdates.DateFormatter('%Y-%m-%d'))
plt.gca().xaxis.set_major_locator(mdates.DayLocator())
plt.gcf().autofmt_xdate()
plt.show()