pandas 如何在matplotlib中的熊猫条形图上添加一条线?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23909249/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to add a line on a pandas bar plot in matplotlib?
提问by lsheng
Hi I have managed to add a line in a bar plot however the position is not right. I want to make the points right in the middle of each bar. Could anyone help?
嗨,我设法在条形图中添加了一条线,但是位置不对。我想在每个小节的中间点。有人可以帮忙吗?
>>> df
price cost net
0 22.5 -20.737486 1.364360
1 35.5 -19.285862 16.695847
2 13.5 -20.456378 -9.016052
3 5.0 -19.643776 -17.539636
4 13.5 -27.015138 -15.964597
5 5.0 -24.267836 -22.618819
6 18.0 -21.096404 -7.357684
7 5.0 -24.691966 -24.116106
8 5.0 -25.755958 -22.080329
9 25.0 -26.352161 -2.781588
fig = plt.figure()
df[['price','cost']].plot(kind = 'bar',stacked = True,color = ['grey','navy'])
df['net'].plot('o',color = 'orange',linewidth=2.0,use_index = True)


回答by joris
Update: this will be fixed in the upcoming 0.14 release (and your code above will just work), for older pandas releases my answer below can be used as a workaround.
更新:这将在即将发布的 0.14 版本中修复(并且您上面的代码将正常工作),对于较旧的 Pandas 版本,我下面的回答可以用作解决方法。
The problem you encounter is that the xaxis labels you see on the bar chart do not correspond exactly with the actual underlying coordinates that matplotlib uses.
Eg with the default barplot in matplotlib, the first rectangle (first bar with label 0) will be plotted on x-coordinates of 0 to 0.8 (bar width of 0.8). So if you want to plot a point or line in the middleof this, this should have x-coordinate of 0.4, and not0!
您遇到的问题是您在条形图上看到的 xaxis 标签与 matplotlib 使用的实际底层坐标不完全对应。
例如,使用barmatplotlib 中的默认绘图,第一个矩形(带有标签 0 的第一个条形)将绘制在 0 到 0.8(条形宽度为 0.8)的 x 坐标上。所以如果你想在中间绘制一个点或线,它的 x 坐标应该是 0.4,而不是0!
To solve this in your case, you can do:
要在您的情况下解决此问题,您可以执行以下操作:
In [3]: ax = df[['price','cost']].plot(kind = 'bar',stacked = True,color = ['grey','navy'])
In [4]: ax.get_children()[3]
Out[4]: <matplotlib.patches.Rectangle at 0x16f2aba8>
In [5]: ax.get_children()[3].get_width()
Out[5]: 0.5
In [6]: ax.get_children()[3].get_bbox()
Out[6]: Bbox('array([[ 0.25, 0. ],\n [ 0.75, 22.5 ]])')
In [7]: plt.plot(df.index+0.5, df['net'],color = 'orange',linewidth=2.0)
I do the ax.get_children()[3].get_width()and .get_bbox()to inspect the actual width and coordinates of the bars in your plot, as pandas doesn't seem to use the default values of matplotlib (the value of 0.5 actually comes from 0.25 (offset from y-axis to start first bar) + 0.5/2 (half of the width)).
我这样做ax.get_children()[3].get_width()并.get_bbox()检查了图中条的实际宽度和坐标,因为Pandas似乎没有使用 matplotlib 的默认值(0.5 的值实际上来自 0.25(从 y 轴偏移到开始第一个条) ) + 0.5/2(宽度的一半))。
So what I actually did was changing df['net'].plot(use_index = True)to plt.plot(df.index + 0.5, df['net']).
所以我实际上所做的是更改df['net'].plot(use_index = True)为plt.plot(df.index + 0.5, df['net']).
This gives me:
这给了我:



