Search code examples
pythonpandasmatplotliberrorbar

Plotting error bars using pandas dataframe


I'm sure this is relatively easy, but I can't seem to make it work. I would like to plot this df, with date as the x-axis, gas as the y-axis and std as errorbars using the matplotlib module. I can get it to work using the pandas wrapper, but then I have no idea how to style the errorbars.

Using pandas matplotlib wrapper

I can plot the error bars using the matplotlib pandas wrapper trip.plot(yerr='std', ax=ax, marker ='D') But then i'm not sure how to access the error bars to style them like one could in matplotlib using plt.errorbar()

Using Matplotlib

fig, ax = plt.subplots()
ax.bar(trip.index, trip.gas, yerr=trip.std)

or

plt.errorbar(trip.index, trip.gas, yerr=trip.std)

The above code throws this error TypeError: unsupported operand type(s) for -: 'float' and 'instancemethod'

So basically, what I would like help with is plotting the errorbars using standard matplotlib module rather than the pandas wrapper.

DF ==

        date       gas       std
0 2015-11-02  6.805351  7.447903
1 2015-11-03  4.751319  1.847106
2 2015-11-04  2.835403  0.927300
3 2015-11-05  7.291005  2.250171

Solution

  • std is a method on a dataframe, ex df.std().

    Use

    plt.errorbar(trip.index, trip['gas'], yerr=trip['std'])
    

    or if you have mpl1.5.0+

    plt.errorbar(trip.index, 'gas', yerr='std', data=trip)