Skip to content

Instantly share code, notes, and snippets.

@sinhrks
Created April 8, 2014 13:19
Show Gist options
  • Save sinhrks/10122968 to your computer and use it in GitHub Desktop.
Save sinhrks/10122968 to your computer and use it in GitHub Desktop.
Pandas plotting with errorbars
# -*- coding: utf-8 -*-
import string
from pandas import DataFrame, Series
import datetime
import numpy as np
from numpy.random import randn, rand
import pandas.tools.plotting as plotting
from pandas.compat import u, string_types
from matplotlib.pylab import close
import matplotlib.pyplot as plt
# errors
err_df = DataFrame(randn(6, 3) / 3, index=range(6), columns=['x', 'y', 'z'])
err_dict = {'x': randn(6) / 3, 'y': randn(6) / 3, 'z': randn(6) / 3}
err_s = Series(randn(6) / 3, index=range(6))
err_str = 'z'
err_list = randn(6) / 3
err_other = 0.2
errs = [err_df, err_dict, err_s, err_list, err_other]
import pandas.util.testing as tm
df = DataFrame(rand(6, 3), index=range(6), columns=['x', 'y', 'z'])
s = Series(rand(6), index=range(6), name='x')
fig, axes = plt.subplots(1, 4, figsize=(14, 4))
df.plot(kind='scatter', x='x', y='y', ax=axes[0], title='no errorbar')
df.plot(kind='scatter', x='x', y='y', xerr=err_df, ax=axes[1], title='xerr')
df.plot(kind='scatter', x='x', y='y', yerr=err_df, ax=axes[2], title='yerr')
df.plot(kind='scatter', x='x', y='y', xerr=err_df, yerr=err_df, ax=axes[3], title='both')
plt.show()
def plot(df, s, errs):
fig, axes = plt.subplots(4, 6, figsize=(14, 8))
plt.subplots_adjust(top=0.97, bottom=0.05, left=0.15, right=0.97, hspace=0.5)
for i, e in enumerate(errs):
df.plot(kind='line', ax=axes[0, i], legend=False, title=type(e), xerr=e, yerr=e)
df.plot(kind='bar', ax=axes[1, i], legend=False, title=type(e), xerr=e, yerr=e)
s.plot(kind='line', ax=axes[2, i], legend=False, title=type(e), xerr=e, yerr=e)
s.plot(kind='bar', ax=axes[3, i], legend=False, title=type(e), xerr=e, yerr=e)
df.plot(kind='line', ax=axes[0, 5], legend=False, title='str', yerr=err_str)
df.plot(kind='bar', ax=axes[1, 5], legend=False, title='str', yerr=err_str)
plt.show()
plot(df, s, errs)
@sinhrks
Copy link
Author

sinhrks commented Apr 8, 2014

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment