Pandas Bar Plot Error Bar

10,747

Solution 1

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).

Using pandas, you just need to dig the correct line in the children of plot and change its marker:

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()

The result looks like: Resulting graph

Solution 2

Just don't set lolim = True and you are good to go, an example with sample data:

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()

Share:
10,747
nonickname
Author by

nonickname

Updated on August 21, 2022

Comments

  • nonickname
    nonickname over 1 year

    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.

    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.