Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/313.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 在XLabel上使用总线条和日期打印堆叠的条形图_Python_Pandas_Matplotlib - Fatal编程技术网

Python 在XLabel上使用总线条和日期打印堆叠的条形图

Python 在XLabel上使用总线条和日期打印堆叠的条形图,python,pandas,matplotlib,Python,Pandas,Matplotlib,我正在使用pandas绘图生成一个堆叠条形图,该条形图的行为与matplotlib的不同,但日期的格式总是不好,我无法更改它。 我还想在图表上画一条“总计”线。但当我尝试添加它时,前面的条会被删除。 我想制作一个类似下面的图表(由excel生成)。黑线是条数的总和 我在网上看过一些解决方案,但它们只有在没有很多条的情况下才看起来不错,所以标签之间有一些空间 这里是我能做的最好的,下面是我使用的代码 将熊猫作为pd导入 将matplotlib.pyplot作为plt导入 将matplotlib

我正在使用pandas绘图生成一个堆叠条形图,该条形图的行为与matplotlib的不同,但日期的格式总是不好,我无法更改它。 我还想在图表上画一条“总计”线。但当我尝试添加它时,前面的条会被删除。 我想制作一个类似下面的图表(由excel生成)。黑线是条数的总和

我在网上看过一些解决方案,但它们只有在没有很多条的情况下才看起来不错,所以标签之间有一些空间

这里是我能做的最好的,下面是我使用的代码

将熊猫作为pd导入
将matplotlib.pyplot作为plt导入
将matplotlib.ticker导入为plticker
#数据(不是图表中的完整系列)
日期=['2016-10-31','2016-11-30','2016-12-31','2017-01-31','2017-02-28','2017-03-31',
'2017-04-30', '2017-05-31', '2017-06-30', '2017-07-31', '2017-08-31', '2017-09-30',
'2017-10-31', '2017-11-30', '2017-12-31', '2018-01-31', '2018-02-28', '2018-03-31',
'2018-04-30', '2018-05-31', '2018-06-30', '2018-07-31', '2018-08-31', '2018-09-30',
'2018-10-31', '2018-11-30', '2018-12-31', '2019-01-31', '2019-02-28', '2019-03-31']
变量={'quantum ex sa':[6.878011,6.557054,3.229360,3.739318,1.006442,-0.117945,
-1.854614, -2.882032, -1.305225, 0.280100, 0.524068, 1.847649,
5.315940, 4.746596, 6.650303, 6.809901, 8.135243, 8.127328,
9.202209, 8.146417, 6.600906, 6.231881, 5.265775, 3.971435,
2.896829, 4.307549, 4.695687, 4.696656, 3.747793, 3.366878],
"南非价格":[-11.618681,9.062433,6.228452,2.944336,0.513788,4.068517,,
6.973203, 8.667524, 10.091766, 10.927501, 11.124805, 11.368854,
11.582204, 10.818471, 10.132152, 8.638781, 6.984159, 5.161404,
3.944813, 3.723371, 3.808564, 4.576303, 5.170760, 5.237303,
5.121998, 5.502981, 5.159970, 4.772495, 4.140812, 3.568077]}
df=pd.DataFrame(index=pd.to_datetime(日期),data=variables)
#策划
ax=df.plot(种类='bar',堆叠=True,宽度=1)
#df['Total']=df.sum(轴=1)
#df['Total'].绘图(ax=ax)
ax.axhline(0,线宽=1)
ax.yaxis.set\u major\u格式化程序(plticker.PercentFormatter())
plt.紧_布局()
plt.show()
编辑 这对我来说是最好的。这比使用pandas
df.plot(kind='bar',stacked=True)
更有效,因为它可以更好地格式化x轴上的日期标签,还可以为条形图设置任意数量的序列

对于计数,枚举中的列(df.列):
old=df.iloc[:,:count].sum(轴=1)
底部_系列=((旧>=0)==(df[col]>=0))*old
ax.bar(df.index,df[col],label=col,bottom=bottom\u系列,width=31)
df['Total']=df.sum(轴=1)
ax.plot(df.index,df['Total',color='black',label='Total')
这就是您想要的:

fig, ax = plt.subplots(1,1, figsize=(16,9))
# PLOTTING
ax.bar(df.index, df['price ex sa'], bottom=df['quantum ex sa'],width=31, label='price ex sa')
ax.bar(df.index, df['quantum ex sa'], width=31, label='quantum ex sa')

total = df.sum(axis=1)
ax.plot(total.index, total, color='r', linewidth=3, label='total')

ax.legend()
plt.show()

编辑:在使用datetime打印时似乎有一个bug(功能)。我尝试将索引转换为字符串,但效果良好:

df.index=df.index.strftime('%Y-%m')

ax = df.plot(kind='bar', stacked=True, width=1)
df['Total'] = df.sum(axis=1)
df['Total'].plot(ax=ax, label='total')
ax.legend()


编辑2:我想我知道发生了什么。问题是

ax = df.plot(kind='bar', stacked=True)
ax
的x轴返回/设置为
范围(len(df))
,该范围由
df.index
中的相应值标记,而不是
df.index
本身。这就是为什么如果我们在同一个
ax
上绘制第二个系列,它不会显示出来(因为xaxis的比例不同)。所以我试着:

# PLOTTING
colums = df.columns

ax = df.plot(kind='bar', stacked=True, width=1, figsize=(10, 6))
ax.plot(range(len(df)), df.sum(1), label='Total')
ax.legend()
plt.show()
它的工作原理与预期相符


对于这种情况,只有两个系列,这是一种解决方案。有没有一种方法可以推广,这样我就可以拥有任意数量的酒吧系列?@GustavoAmarante请参见编辑。我可能会有更好的把戏。是的,我也有。但是你的第一个回答给了我一个想法。让我试试,如果ti有效的话,我会把它贴在这里。我最终选择了另一个解决方案,但它是从你的第一个答案中得到启发的。谢谢你的帮助。