Pandas 条形图误差条
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/45204202/
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
Pandas Bar Plot Error Bar
提问by nonickname
so I am plotting error bar of pandas dataframe. Now the error bar has a weird arrow at the top, but what I want is a horizontal line. For example, a figure like this:
But now my error bar ends with arrow instead of a horinzontal line.
所以我正在绘制Pandas数据框的误差条。现在误差条顶部有一个奇怪的箭头,但我想要的是一条水平线。例如,像这样的图:
但现在我的误差条以箭头而不是水平线结束。
Here is the code i used to generate it:
这是我用来生成它的代码:
plot = meansum.plot(kind='bar',yerr=stdsum,colormap='OrRd_r',edgecolor='black',grid=False,figsize=(8,2),ax=ax,position=0.45,error_kw=dict(ecolor='black',elinewidth=0.5,lolims=True,marker='o'),width=0.8)
So what should I change to make the error become the one I want. Thx.
那么我应该改变什么才能使错误成为我想要的错误。谢谢。
回答by Vinícius Aguiar
Just don't set lolim = True
and you are good to go, an example with sample data:
只是不要设置lolim = True
,您就可以开始了,示例数据示例:
import pandas as pd
import matplotlib.pyplot as plt
fig, ax = plt.subplots()
df = pd.DataFrame({"val":[1,2,3,4],"error":[.4,.3,.6,.9]})
meansum = df["val"]
stdsum = df["error"]
plot = meansum.plot(kind='bar',yerr=stdsum,colormap='OrRd_r',edgecolor='black',grid=False,figsize=(8,2),ax=ax,position=0.45,error_kw=dict(ecolor='black',elinewidth=0.5),width=0.8)
plt.show()
回答by H. Rev.
Using plt.errorbar
from matplotlib makes it easier as it returns several objects including the caplines
which contain the marker you want to change (the arrow which is automatically used when lolims
is set to True
, see docs).
使用plt.errorbar
从matplotlib使得它更容易,因为它返回几个对象,包括caplines
含有改变你想要的标记(时自动使用箭头lolims
设置为True
,见文档)。
Using pandas
, you just need to dig the correct line in the children of plot
and change its marker:
使用pandas
,您只需要在 的子项中挖掘正确的行plot
并更改其标记:
import pandas as pd
import matplotlib.pyplot as plt
fig, ax = plt.subplots()
df = pd.DataFrame({"val":[1,2,3,4],"error":[.4,.3,.6,.9]})
meansum = df["val"]
stdsum = df["error"]
plot = meansum.plot(kind='bar',yerr=stdsum,colormap='OrRd_r',edgecolor='black',grid=False,figsize=8,2),ax=ax,position=0.45,error_kw=dict(ecolor='black',elinewidth=0.5, lolims=True),width=0.8)
for ch in plot.get_children():
if str(ch).startswith('Line2D'): # this is silly, but it appears that the first Line in the children are the caplines...
ch.set_marker('_')
ch.set_markersize(10) # to change its size
break
plt.show()