我设法处理了绘图数据以渲染相应的时间序列绘图。但是我对当前的输出并不满意,因为要理解新生成的图并不容易。
我当前的数据和输出:
这是我的数据,如下所示:
更新
这是在绘图数据上方成形的我的素描代码:
df=df.groupby(['date'])['qty1'].sum().reset_index()
df['year'] = pd.DatetimeIndex(df['date']).year
df['month'] = pd.DatetimeIndex(df['date']).month
plot_data=df.groupby(['year', 'month'])['qty1'].sum().unstack().fillna(0)
plot_data.plot(kind='line')
根据这些数据,我得到了这个图:
但这不是我期望的。
所需的情节:
这是我真正想要的情节:
我没有得到这个情节。我怎么能得到这个?有什么主意吗?
答案 0 :(得分:0)
这是您要寻找的吗?
import pandas as pd
import matplotlib.pyplot as plt
import calendar
%matplotlib inline
df = pd.DataFrame(dic) #dic is the dictionary you provided in the github link
df.columns = [str(i) for i in range(1,13)]
df = df.T
df.columns = ['2014','2015','2016','2017','2018']
df['Avg'] = df.mean(axis =1)
fig,ax = plt.subplots(figsize = (15,7))
plt.plot(df.index,df['2016'], marker='s',color = 'green', linewidth = 1, label="2016")
plt.plot(df.index,df['2017'],"bo-", linewidth = 1.5, label="2017")
plt.plot(df.index,df['2018'], marker='s', ms =10, color = 'red', linewidth = 3, label="2018")
plt.plot(df.index,df['Avg'], "--", color = 'grey', linewidth = 8, label="5-Yr-Avg")
plt.xlabel('\n Months\n', fontsize = 25, color = 'lightslategrey')
plt.legend(frameon = False, loc = "lower center", ncol=len(df.columns), fontsize = 14)
plt.grid(axis='y')
ax.set_xticklabels([calendar.month_abbr[i] for i in range(1,13)])
plt.tick_params( left = False, labelsize= 13)
ax.spines['left'].set_visible(False)
ax.spines['top'].set_visible(False)
ax.spines['right'].set_visible(False)
plt.show()