In [ ]:
%matplotlib notebook

In [ ]:
import numpy as np
import matplotlib.pyplot as plt

In [ ]:
data = np.arange(10)
data

In [ ]:
plt.plot(data)

9.1.1 Figures and Subplots


In [ ]:
fig = plt.figure()
ax1 = fig.add_subplot(2,2,1)
ax2 = fig.add_subplot(2,2,2)
ax3 = fig.add_subplot(2,2,3)
plt.plot(np.random.randn(50).cumsum(), 'k--')
_ = ax1.hist(np.random.randn(50), bins=20, color='k', alpha=0.3)
ax2.scatter(np.arange(30), np.arange(30)+3*np.random.randn(30))

In [ ]:
type(fig)

In [ ]:
fig, axes = plt.subplots(2,3)
axes

In [ ]:
axes[0][1]

In [ ]:
fig, axes = plt.subplots(2, 2, sharex=True, sharey=True)
for i in range(2):
    for j in range(2):
        axes[i, j].hist(np.random.randn(500), bins=50, color='orange', alpha=0.5)
plt.subplots_adjust(wspace=0.2, hspace=1.0)

9.1.2 Colors, Markers, and Line Styles


In [ ]:
from numpy.random import randn

In [ ]:
fig = plt.figure()
plt.plot(randn(30).cumsum(), 'ko--')
plt.plot(randn(30).cumsum(), color='orange', linestyle='dashed', marker='o')

In [ ]:
data = randn(30).cumsum()
fig = plt.figure()
plt.plot(data, 'g--', label='Default')
plt.plot(data, color='orange', drawstyle='steps-post', marker='o', label='steps-post')
plt.legend(loc='best')

9.1.3 Ticks, Labels and Legends


In [ ]:
fig = plt.figure()
ax = fig.add_subplot(1,1,1)
ax.plot(randn(1000).cumsum())
ax.set_xticks([0, 250, 500, 750, 1000])
ax.set_xticklabels(['one', 'two', 'three', 'four', 'five'], rotation=30, fontsize='small')
ax.set_title('My first matplotlib plot')
ax.set_xlabel('Stage')

9.1.3.4 Adding legend


In [ ]:
fig = plt.figure()
ax = fig.add_subplot(1,1,1)
ax.plot(randn(1000).cumsum(), color='orange', label='one')
ax.plot(randn(1000).cumsum(), 'r--', label='two')
ax.plot(randn(1000).cumsum(), 'g.', label='three')
ax.legend(loc='best')

9.1.4 Annotations and Drawing on a Subplot


In [ ]:
from datetime import datetime

In [ ]:
import pandas as pd

In [ ]:
data = pd.read_csv('./datasets/spx.csv', index_col=0, parse_dates=True)
spx = data['SPX']

crisis_data = [
    (datetime(2007, 10, 11), 'Peek of bull market'),
    (datetime(2008, 3, 12), 'Bear Stearns Fails'),
    (datetime(2008, 9, 15), 'Lehman Bankruptcy'),
]

In [ ]:
fig = plt.figure()
ax = fig.add_subplot(1, 1, 1)

spx.plot(ax=ax, style='k-')

for date, label in crisis_data:
    ax.annotate(label, xy=(date, spx.asof(date)+75),
                xytext=(date, spx.asof(date)+225),
                arrowprops=dict(facecolor='black', headwidth=4, width=2,
                                headlength=4),
                horizontalalignment='left', verticalalignment='top'
               )

ax.set_xlim(['1/1/2007', '1/1/2009'])
ax.set_ylim([600, 1800])
ax.set_title('Important dates in the 2008-2009 financial crisis')

Adding shapes


In [ ]:
fig = plt.figure()
ax = fig.add_subplot(1, 1, 1)

rect = plt.Rectangle((0.2, 0.75), 0.4, 0.15, color='k', alpha=0.3)
circ = plt.Circle((0.7, 0.2), 0.15, color='b', alpha=0.3)
pgon = plt.Polygon([[0.15,0.15], [0.35,0.4], [0.2,0.6]], color='g', alpha=0.5)
ax.add_patch(rect)
ax.add_patch(circ)
ax.add_patch(pgon)

In [ ]:
plt.savefig('shape.pdf', dpi=400, bbox_inches='tight')

9.2.1 Line Plots


In [ ]:
import pandas as pd

In [ ]:
s = pd.Series(np.random.rand(10).cumsum(), index=np.arange(0, 100, 10))

In [ ]:
fig = plt.figure()
s.plot(color='r', linestyle='--', label='Thor')
plt.legend(loc='best')

In [ ]:
df = pd.DataFrame(np.random.rand(10, 4).cumsum(0), columns=['Captain America', 'Iron Man', 'Thor', 'Black Widow'], index=np.arange(0, 100, 10))
df

In [ ]:
fig, ax = plt.subplots(2,2)
df.plot(ax=ax, subplots=True)

9.2.2 Bar Plots


In [ ]:
fig, axes = plt.subplots(2, 1)
data = pd.Series(np.random.rand(16), index=list('abcdefghijklmnop'))
data.plot.bar(ax=axes[0], color='k', alpha=0.7)
data.plot.barh(ax=axes[1], color='k', alpha=0.7)

In [ ]:
df = pd.DataFrame(np.random.rand(6, 4), index=['one', 'two', 'three', 'four', 'five', 'six'], 
                 columns=pd.Index(['A', 'B', 'C', 'D'], name='Genus'))
df

In [ ]:
df.plot.bar()

In [ ]:
df.plot.barh(stacked=True, alpha=0.5)

In [ ]:
tips = pd.read_csv('datasets/tips.csv')
tips

In [ ]:
party_counts = pd.crosstab(tips['day'], tips['size'])
party_counts

In [ ]:
party_counts = party_counts.loc[:, 2:5]

In [ ]:
party_counts

In [ ]:
party_pacts = party_counts.div(party_counts.sum(axis=1), axis=0)
party_pacts

In [ ]:
party_pacts.plot.bar()

Use seaborn


In [ ]:
import seaborn as sns

In [ ]:
tips.head()

In [ ]:
tips['tip_pct'] = tips['tip'] / (tips['total_bill'] - tips['tip'])
tips.head()

In [ ]:
fig = plt.figure()
sns.barplot(x='tip_pct', y='day', data=tips, orient='h')

In [ ]:
fig = plt.figure()
sns.barplot(x='tip_pct', y='day', hue='time', data=tips, orient='h')

9.2.3 Histograms and Density Plots


In [ ]:
sns.set(style='darkgrid')

In [ ]:
fig = plt.figure()
_ = tips['tip_pct'].plot.hist(bins=50)

In [ ]:
fig = plt.figure()
_ = tips['tip_pct'].plot.density(color='r')

In [ ]:
fig = plt.figure()
comp1 = np.random.normal(0, 1, size=200)
comp2 = np.random.normal(10, 2, size=200)
values = pd.Series(np.concatenate([comp1, comp2]))
sns.distplot(values, bins=100, color='orange')