matplotlib双条形图与熊猫系列

a1234

我有以下内容:

indyes = tuple(yesSeries.index)
indno = tuple(nodSeries.index)
width = 3

p1 = plt.bar(indyes, yesSeries, label="Example one", color='SkyBlue')
p2 = plt.bar(indno, nodSeries, label="Example two", color='IndianRed')
plt.legend()
plt.xlabel('bar number')
plt.ylabel('bar height')

plt.title('Epic Graph\nAnother Line! Whoa')

plt.show()

它将我的图绘制为堆积的条形图: 在此处输入图片说明

当我尝试添加+ width到第二个条形图中时,indno + width它们没有堆叠在一起,而是并排出现以下错误:TypeError: can only concatenate tuple (not "int") to tuple

我已将“熊猫系列”设置为一个元组,以提取日期和计数信息。

如何获得两个平行的垂直条形图?

塞德里克·佐波罗

我相信你在找什么可以做创建一个DataFrame从你的Series,然后使用该plot.bar功能。

在下面的代码中,我将生成数据并根据需要创建图形。

import matplotlib.pyplot as plt
import pandas as pd
times = pd.date_range('2018-09-01', periods=7, freq='5D')
yesSeries = pd.Series([1800,2000,3000,1000,2000,1500,1700], index=times)
nodSeries = pd.Series([200,500,700,600,300,50,0], index=times)

df = pd.DataFrame({"Example one":yesSeries,"Example two":nodSeries})
ax = df.plot.bar(color=["SkyBlue","IndianRed"], rot=0, title="Epic Graph\nAnother Line! Whoa")
ax.set_xlabel("date")
ax.set_ylabel("counts")
ax.xaxis.set_major_formatter(plt.FixedFormatter(times.strftime("%b %d %Y")))
plt.show()

结果如下图所示。

条形图

如果日期看起来很混乱,则可以在plt.show()代码之前的下面插入

plt.gcf().autofmt_xdate()

这将导致下图。

带日期的条形图与autofmt

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章