Skip to content

Instantly share code, notes, and snippets.

@sinhrks
Created March 17, 2014 20:23
Show Gist options
  • Save sinhrks/9607567 to your computer and use it in GitHub Desktop.
Save sinhrks/9607567 to your computer and use it in GitHub Desktop.
Pandas dataframe line and area plots
import string
from pandas import DataFrame
import numpy as np
from numpy.random import randn, rand
import pandas.tools.plotting as plotting
from matplotlib.pylab import close
mix_s_df = DataFrame(randn(6, 3),
index=range(6),
columns=['x', 'y', 'z'])
s_df = DataFrame(rand(6, 3),
index=range(6),
columns=['x', 'y', 'z'])
neg_s_df = - s_df
import pandas.util.testing as tm
mix_t_df = DataFrame(randn(6, 3),
index=tm.makeDateIndex(k=6),
columns=['x', 'y', 'z'])
t_df = DataFrame(rand(6, 3),
index=tm.makeDateIndex(k=6),
columns=['x', 'y', 'z'])
neg_t_df = - t_df
import matplotlib.pyplot as plt
for m_df, df, neg_df in [(mix_s_df, s_df, neg_s_df), (mix_t_df, t_df, neg_t_df)]:
fig, axes = plt.subplots(6, 3, figsize=(14, 8))
plt.subplots_adjust(top=0.97, bottom=0.05, left=0.15, right=0.97, hspace=0.5)
df.plot(kind='line', ax=axes[0, 0], legend=False, title='stacked=False')
df.plot(kind='line', ax=axes[0, 1], stacked=True, legend=False, title='stacked=True')
df.plot(kind='line', ax=axes[0, 2], stacked=True, logy=True, legend=False, title='logy=True, stacked=True')
fig.text(0.07, 0.91, 'All Positive Data', ha='center')
neg_df.plot(kind='line', ax=axes[1, 0], legend=False, title='stacked=False')
neg_df.plot(kind='line', ax=axes[1, 1], stacked=True, legend=False, title='stacked=True')
axes[1, 2].text(0.5, 0.5, 'Unable to define for negative values', ha='center', va='center')
axes[1, 2].set_title('logy=True, stacked=True')
fig.text(0.07, 0.74, 'All Negative Data', ha='center')
m_df.plot(kind='line', ax=axes[2, 0], legend=False, title='stacked=False')
axes[2, 1].text(0.5, 0.5, 'Unable to define stacking \n of positive/negative mixted', ha='center', va='center')
axes[2, 2].text(0.5, 0.5, 'Unable to define for negative values', ha='center', va='center')
axes[2, 2].set_title('logy=True, stacked=True')
fig.text(0.07, 0.58, 'Positive-Negative \n Mixed Data', ha='center')
df.plot(kind='area', ax=axes[3, 0], stacked=False, legend=False, title='stacked=False')
df.plot(kind='area', ax=axes[3, 1], stacked=True, legend=False, title='stacked=True')
axes[3, 2].text(0.5, 0.5, 'Not supported because \n unable to fill from 0', ha='center', va='center')
axes[3, 2].set_title('logy=True, stacked=True')
fig.text(0.07, 0.41, 'All Positive Data', ha='center')
neg_df.plot(kind='area', ax=axes[4, 0], stacked=False, legend=False, title='stacked=False')
neg_df.plot(kind='area', ax=axes[4, 1], stacked=True, legend=False, title='stacked=True')
axes[4, 2].text(0.5, 0.5, 'Unable to define for negative values', ha='center', va='center')
axes[4, 2].set_title('logy=True, stacked=True')
fig.text(0.07, 0.26, 'All Negative Data', ha='center')
m_df.plot(kind='area', ax=axes[5, 0], stacked=False, legend=False, title='stacked=False')
axes[5, 1].text(0.5, 0.5, 'Unable to define stacking \n of positive/negative mixed', ha='center', va='center')
axes[5, 2].text(0.5, 0.5, 'Unable to define for negative values', ha='center', va='center')
axes[5, 2].set_title('logy=True, stacked=True')
fig.text(0.07, 0.10, 'Positive-Negative \n Mixed Data', ha='center')
plt.show()
@sinhrks
Copy link
Author

sinhrks commented Mar 21, 2014

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