Skip to content

Instantly share code, notes, and snippets.

@sinhrks
Created March 21, 2014 07:57
Show Gist options
  • Save sinhrks/9681610 to your computer and use it in GitHub Desktop.
Save sinhrks/9681610 to your computer and use it in GitHub Desktop.
Pandas dataframe with legends
from pandas import DataFrame, Series
from numpy.random import randn, rand
df1 = DataFrame(randn(6, 3), index=range(6),
columns=['a', 'b', 'c'])
df2 = DataFrame(randn(6, 3), index=range(6),
columns=['d', 'e', 'f'])
df3 = DataFrame(randn(6, 3), index=range(6),
columns=['x', 'y', 'z'])
import matplotlib.pyplot as plt
fig, axes = plt.subplots(1, 5, figsize=(14, 3))
plt.subplots_adjust(top=0.9, bottom=0.1, left=0.05, right=0.97, hspace=0.5)
for i, (legend, df) in enumerate(zip([True, False, 'reverse'], [df1, df2, df3])):
df.plot(ax=axes[0], legend=legend, title='line')
df.plot(kind='bar', ax=axes[1], legend=legend, title='bar')
df.plot(kind='barh', ax=axes[2], legend=legend, title='barh')
df.plot(kind='kde', ax=axes[3], legend=legend, title='kde')
df.plot(kind='scatter', ax=axes[4], x=df.columns[0], y=df.columns[1], label=i,
legend=legend, title='scatter')
plt.show()
fig, axes = plt.subplots(1, 6, figsize=(14, 3))
plt.subplots_adjust(top=0.9, bottom=0.1, left=0.05, right=0.97, hspace=0.5)
df1.plot(ax=axes[0], secondary_y ='a')
df1.plot(kind='bar', ax=axes[0])
df2.plot(ax=axes[0], title='line')
df1.plot(ax=axes[1], secondary_y ='a')
df1.plot(kind='bar', ax=axes[1], secondary_y='x')
df2.plot(ax=axes[1], secondary_y='d', title='line')
df1.plot(ax=axes[2], kind='kde')
df2.plot(ax=axes[2], kind='kde', legend=False)
df1.plot(ax=axes[3], kind='kde')
df2.plot(ax=axes[3], kind='kde', legend=False)
df3.plot(ax=axes[3], kind='kde', legend=True)
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