简体   繁体   English

月份,年份和价值图,熊猫和MatPlotLib

[英]Month,Year with Value Plot,Pandas and MatPlotLib

i have DataFrame with Month,Year and Value and i want to do a TimeSeries Plot. 我有带月,年和值的数据框,我想做一个时间序列图。

Sample: 样品:

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

im trying to plot this Value data with Year and Month, Year and Month in X-Axis and Value in Y-Axis. 我试图用年和月,年和月在X轴上,值在Y轴上绘制此Value数据。

One way is this: 一种方法是:

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

在此处输入图片说明

You need to set a DateTime index for pandas to properly plot the axis. 您需要为熊猫设置DateTime索引才能正确绘制轴。 A one line modification for your dataframe (assuming you don't need year and month anymore as columns and that first day of each month is correct) would do: 对数据框进行一行修改(假设您不再需要yearmonth作为列,并且month第一天都是正确的),则可以执行以下操作:

df.set_index(pd.to_datetime({
    'day': 1,
    'month': df.pop('month'),
    'year': df.pop('year')
}), inplace=True)

df.Value.plot()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM