graph - Pyplot-正值​​和负值的条形图

标签 graph matplotlib charts plot

我希望负条朝下,正条朝上,x轴(0线)正好在它们之间通过。我试过了

chart = fig.bar(x, negative_data, width=35, color='r')
ax2 = plt.gca().twinx()
ax2.bar(x, positive_data, width=35, color='b')


但是,相反,我合并了红色和白色的条,两个条都朝下。似乎数组negative_data / positive_data仅指定条形的高度,但是如何指定方向?我需要一些东西来指定每个酒吧的顶部的坐标。

另外,当用户调整图形大小时,如何使宽度合理,可能是动态的?

这是有问题的宽度的示例:

x = [250, 1500, 2750, 4250, 6000, 8500, 13200]
negative_data = [0, 0, 0, 0, 0, 0, 0]
positive_data = [3, 0, 0, 0, 1, 0, 0]


我怎样才能使这些图看起来好看?

最佳答案

您无需添加双轴,可以在同一轴上绘制两个条形图,如下所示:

x = range(7)
negative_data = [-1,-4,-3,-2,-6,-2,-8]
positive_data = [4,2,3,1,4,6,7,]

fig = plt.figure()
ax = plt.subplot(111)
ax.bar(x, negative_data, width=1, color='r')
ax.bar(x, positive_data, width=1, color='b')




宽度为1的条形图将填充轴,并在调整大小时随图形缩放。

关于graph - Pyplot-正值​​和负值的条形图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25550308/

相关文章:

深度优先搜索中的Python "RuntimeError: maximum recursion depth exceeded"

python-3.x - 分别设置两个直方图的颜色有问题

python - Matplotlib:将图形另存为 iPython 笔记本中的文件

reactjs - 在 react-chartjs-2 中将背景颜色更改为渐变

位图创建时的 Android OutOfMemory 错误

c++ - 使用c++、linux绘制 vector 图

c - 在邻接表中有自己的节点

python - Matplotlib 绘图上的双标记

javascript - 为 AmCharts 4 标签添加样式

charts - 谷歌图表包装图例标签