首页 > 解决方案 > 月、年与价值图、Pandas 和 MatPlotLib

问题描述

我有带有月、年和值的 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

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

标签: pythonpandasmatplotlib

解决方案


一种方法是:

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()

在此处输入图像描述


推荐阅读