带有值图、Pandas 和 MatPlotLib 的月、年

Sri*_*mar 3 python matplotlib pandas

我有带月、年和值的 DataFrame,我想做一个时间序列图。

样本:

month   year    Value
12      2016    0.006437804129357764
1       2017    0.013850880792606646
2       2017    0.013330349031207292
3       2017    0.07663058273768052
4       2017    0.7822831457266424
5       2017    0.8089573099244689
6       2017    1.1634845000200715
Run Code Online (Sandbox Code Playgroud)

我试图用 X 轴中的年和月、年和月以及 Y 轴中的值来绘制此值数据。

Joe*_*Joe 9

一种方法是这样的:

import pandas as pd
import matplotlib.pyplot as plt

df['date'] = df['month'].map(str)+ '-' +df['year'].map(str)
df['date'] = pd.to_datetime(df['date'], format='%m-%Y').dt.strftime('%m-%Y')
fig, ax = plt.subplots()
plt.plot_date(df['date'], df['Value'])
plt.show()
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明