15

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

2 Answers 2

23

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)
Sign up to request clarification or add additional context in comments.

Comments

1
import pandas as pd
from datetime import date
import matplotlib.pyplot as plt

# sample dataframe
data = {'date': [date(2015, 11, 2), date(2015, 11, 3), date(2015, 11, 4), date(2015, 11, 5)],
        'gas': [6.805351, 4.751319, 2.835403, 7.291005], 'std': [7.447903, 1.847106, 0.9273, 2.250171]}
trip = pd.DataFrmae(data)

# plot the dataframe with error bars
ax = trip.plot(kind='bar', x='date', y='gas', yerr='std', rot=0)

enter image description here

fig, ax = plt.subplots()
ax.bar(trip['date'], trip['gas'], yerr=trip['std'])
fig, ax = plt.subplots()
ax.bar('date', 'gas', yerr='std', data=trip)

trip

         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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.