Graphics

See more elementary examples at http://people.duke.edu/~ccc14/cfar-data-2016/index.html#day-2

Matplotlib

Setting up numeric environment


In [1]:
import numpy as np
import pandas as pd
np.random.seed(123)

Load images directly into notebook


In [2]:
from IPython.display import Image

Setting up graphics environment


In [3]:
%matplotlib inline
import matplotlib as mpl
import matplotlib.pyplot as plt

Basic plots


In [4]:
n = 100
t = pd.date_range('23-Jan-2017', periods=n)
x = np.cumsum(np.random.randn(n))
y = np.cumsum(np.random.randn(n))

In [5]:
fig, ax = plt.subplots(figsize=(8,6))
fig.autofmt_xdate()
ax.plot(t, x, marker='o', 
        linestyle='dashed', linewidth=0.5, color='red', 
        markersize=5, markerfacecolor='blue', markeredgecolor='red',
        label='ACME')
ax.plot(t, y, marker='o', 
        linestyle='solid', linewidth=0.5, color='blue', 
        markersize=5, markerfacecolor='yellow', markeredgecolor='blue',
        label='EMCA')
ax.set_axis_bgcolor('lightgrey')
ax.set_xlabel('Date', fontsize=14)
ax.set_ylabel('Stock Price', fontsize=14)
ax.margins(y=0.05)
ax.legend(loc='lower right', fontsize=14)
ax.set_title('ACME vs EMCA')
fig.savefig('acme_vs_ecma.png', dpi=150)
pass



In [6]:
Image('acme_vs_ecma.png', width=600)


Out[6]:

Using styles


In [7]:
mpl.style.available


Out[7]:
['classic',
 'grayscale',
 'seaborn-pastel',
 'seaborn-darkgrid',
 'seaborn-muted',
 'seaborn-whitegrid',
 'seaborn-colorblind',
 'seaborn-bright',
 'dark_background',
 'seaborn-talk',
 'seaborn-white',
 'seaborn-dark',
 'seaborn-paper',
 'ggplot',
 'seaborn-ticks',
 'fivethirtyeight',
 'bmh',
 'seaborn-notebook',
 'seaborn-deep',
 'seaborn-dark-palette',
 'seaborn-poster']

In [8]:
with mpl.style.context('fivethirtyeight'):
    fig, ax = plt.subplots(figsize=(8,6))
    fig.autofmt_xdate()
    ax.plot(t, x, marker='o', 
            linestyle='dashed', linewidth=0.5, color='red', 
            markersize=5, markerfacecolor='blue', markeredgecolor='red',
            label='ACME')
    ax.plot(t, y, marker='o', 
            linestyle='solid', linewidth=0.5, color='blue', 
            markersize=5, markerfacecolor='yellow', markeredgecolor='blue',
            label='EMCA')
    ax.set_axis_bgcolor('lightgrey')
    ax.set_xlabel('Date', fontsize=14)
    ax.set_ylabel('Stock Price', fontsize=14)
    ax.margins(y=0.05)
    ax.legend(loc='lower right', fontsize=14)
    ax.set_title('ACME vs EMCA')
    fig.savefig('fivethirtyeight.png')


The special, whimsical XKCD style

Only available within pyplot, and not via matplotlib.style.


In [9]:
with plt.xkcd():
    fig, ax = plt.subplots(figsize=(8,6))
    fig.autofmt_xdate()
    ax.plot(t, x, marker='o', 
            linestyle='dashed', linewidth=0.5, color='red', 
            markersize=5, markerfacecolor='blue', markeredgecolor='red',
            label='ACME')
    ax.plot(t, y, marker='o', 
            linestyle='solid', linewidth=0.5, color='blue', 
            markersize=5, markerfacecolor='yellow', markeredgecolor='blue',
            label='EMCA')
    ax.set_axis_bgcolor('lightgrey')
    ax.set_xlabel('Date', fontsize=14)
    ax.set_ylabel('Stock Price', fontsize=14)
    ax.margins(y=0.05)
    ax.legend(loc='lower right', fontsize=14)
    ax.set_title('ACME vs EMCA')
    plt.savefig('xkcd.png')


/opt/conda/lib/python3.5/site-packages/matplotlib/font_manager.py:1288: UserWarning: findfont: Font family ['Humor Sans', 'Comic Sans MS'] not found. Falling back to Bitstream Vera Sans
  (prop.get_family(), self.defaultFamily[fontext]))

Other plots


In [10]:
n, bins, patches = plt.hist(x, histtype='bar', linewidth=2, edgecolor='white', alpha=0.5)
plt.savefig('bar.png')



In [11]:
s = c = np.random.uniform(10, 300, len(x))
patches = plt.scatter(x, y, s=s, c=c, alpha=0.5, cmap='jet')
plt.savefig('scatter.png')



In [12]:
ax = plt.subplot()
ax.set_aspect('equal')
X, Y = np.meshgrid(range(-50, 50), range(-50, 50))
U = X - Y
V = X + Y
ax.streamplot(X,Y, U, V)
plt.savefig('stream.png')



In [13]:
n = 50
theta = np.random.uniform(0, 2*np.pi, n)
r = np.random.random(n)
ax = plt.subplot(projection='polar')
ax.scatter(theta, r, s=100*r)
ax.bar(theta, r, width=0.2, alpha=0.5)
plt.savefig('polar.png')


Annotations


In [14]:
n = 10
s = np.random.uniform(100, 500, n)
t = pd.date_range(start='23-Jan-2017', periods=n)
y = np.cumsum(np.random.randn(n))

In [15]:
np.random.seed(123)
fig, ax = plt.subplots()
fig.autofmt_xdate()
plt.plot(t, y, marker='o')
plt.text('26-Jan-2017', 0.65, r'$\alpha$', ha='center', fontsize=20)
plt.text(0.9, 0.9, r'$\beta$', transform=ax.transAxes, fontsize=20,
        bbox=dict(facecolor='yellow', alpha=0.5))
plt.arrow(0.9, 0.9, -0.15, 0.02, head_width=0.05, head_length=0.05, 
          fc='k', ec='k', transform=ax.transAxes)
plt.margins(x=0.05)
plt.savefig('annotate.png')


Multiple plots


In [16]:
n = 10
s = np.random.uniform(100, 500, n)
ys = np.cumsum(np.random.normal(0, 1, (6, n)), axis=1)

In [17]:
fig, axes = plt.subplots(2, 3, figsize=(10,6), sharey=True)
for ax, y in zip(axes.ravel(), ys):
    ax.plot(y, '-o')
    ax.margins(x=0.05)
plt.tight_layout()
plt.savefig('regular.png')



In [18]:
gs = mpl.gridspec.GridSpec(3,3)
ax1 = plt.subplot(gs[0, :])
ax2 = plt.subplot(gs[1, :2])
ax3 = plt.subplot(gs[1,2])
ax4 = plt.subplot(gs[2,0])
ax5 = plt.subplot(gs[2,1])
ax6 = plt.subplot(gs[2,2])
axes = [ax1, ax2, ax3, ax4, ax5, ax6]

for ax, y in zip(axes, ys):
    ax.plot(y, '-o')
plt.gcf().set_size_inches(6,6)
plt.tight_layout()
plt.savefig('gridspec.png')


Seaborn


In [19]:
import seaborn as sns

Set style and context


In [20]:
sns.set_style('darkgrid')
sns.set_context('notebook', font_scale=1.5)

In [21]:
%load_ext rpy2.ipython

In [22]:
%R -o iris

In [23]:
iris.head()


Out[23]:
Sepal.Length Sepal.Width Petal.Length Petal.Width Species
1 5.1 3.5 1.4 0.2 setosa
2 4.9 3.0 1.4 0.2 setosa
3 4.7 3.2 1.3 0.2 setosa
4 4.6 3.1 1.5 0.2 setosa
5 5.0 3.6 1.4 0.2 setosa

In [24]:
import warnings

In [25]:
warnings.filterwarnings('ignore', category=np.VisibleDeprecationWarning)

In [26]:
sns.distplot(iris['Sepal.Length'], rug=True)
plt.savefig('displot.png')



In [27]:
sns.kdeplot(iris['Sepal.Width'], iris['Sepal.Width'], shade=True)
plt.savefig('kdeplot.png')



In [28]:
sns.regplot(x='Sepal.Width', y='Sepal.Length',  data=iris)
pass



In [29]:
sns.lmplot(x='Sepal.Width', y='Sepal.Length', 
           hue='Species', data=iris)
plt.savefig('lmplot.png')



In [30]:
sns.lmplot(x='Sepal.Width', y='Sepal.Length',
           hue='Species', col='Species', data=iris)
plt.savefig('lmplot_sep.png')



In [31]:
g = sns.lmplot(x='Sepal.Width', y='Sepal.Length',
               hue='Species', col='Species',  lowess=True,
               sharex=False, sharey=False, data=iris, scatter_kws={"s": 100, 'alpha': 0.5})
plt.savefig('lmplot_lowess.png')



In [32]:
iris_tall = pd.melt(iris, id_vars=['Species'])
iris_tall.head()


Out[32]:
Species variable value
0 setosa Sepal.Length 5.1
1 setosa Sepal.Length 4.9
2 setosa Sepal.Length 4.7
3 setosa Sepal.Length 4.6
4 setosa Sepal.Length 5.0

In [33]:
sns.factorplot(y='value', x='Species', col='variable', 
               kind='bar',
               data=iris_tall, margin_titles=True)
plt.savefig('factor_bar.png')



In [34]:
sns.factorplot(y='value', x='Species', col='variable', 
               kind='swarm',
               data=iris_tall, margin_titles=True)
plt.savefig('factor_swarm.png')



In [35]:
sns.factorplot(y='value', x='Species', col='variable', 
               kind='box',
               data=iris_tall, margin_titles=True)
plt.savefig('factor_box.png')



In [36]:
sns.factorplot(y='value', x='Species', col='variable', 
               kind='violin',
               data=iris_tall, margin_titles=True)
plt.savefig('factor_violin.png')


Color paltettes and colormaps


In [37]:
sns.factorplot(y='value', x='Species', col='variable', 
               kind='bar', palette='BuGn_r',
               data=iris_tall, margin_titles=True)
plt.savefig('factor_bar_BuGn_r.png')


Sequential, divergent and qualitative plattes


In [38]:
sns.choose_colorbrewer_palette('sequential')


Out[38]:
[(0.95755478915046244, 0.95755478915046244, 0.95755478915046244),
 (0.90120723387774304, 0.90120723387774304, 0.90120723387774304),
 (0.83289505032932054, 0.83289505032932054, 0.83289505032932054),
 (0.75021916137022127, 0.75021916137022127, 0.75021916137022127),
 (0.64341409276513495, 0.64341409276513495, 0.64341409276513495),
 (0.53871589525073182, 0.53871589525073182, 0.53871589525073182),
 (0.44032295626752516, 0.44032295626752516, 0.44032295626752516),
 (0.34288351570858677, 0.34288351570858677, 0.34288351570858677),
 (0.22329873945198808, 0.22329873945198808, 0.22329873945198808),
 (0.1046981975144031, 0.1046981975144031, 0.1046981975144031)]

In [39]:
sns.choose_colorbrewer_palette('divergent')


Out[39]:
[(0.69227222075649331, 0.092272204803485525, 0.16770473370949396),
 (0.83921569585800171, 0.37647059559822083, 0.30196079611778259),
 (0.95455594273174504, 0.64175319262579378, 0.5057285948126925),
 (0.99215686321258534, 0.85882353782653809, 0.78039216995239269),
 (0.96570549992954036, 0.96724336988785686, 0.96808919836493101),
 (0.81960785388946544, 0.89803922176361084, 0.94117647409439076),
 (0.56647445816619735, 0.76870435826918648, 0.8685121185639324),
 (0.26274511218070995, 0.57647061347961415, 0.76470589637756337),
 (0.12725875369620088, 0.3958477567808299, 0.66874281039424976)]

In [40]:
current_palette = sns.choose_colorbrewer_palette('qualitative')



In [42]:
sns.factorplot(y='value', x='Species', col='variable', 
               kind='bar', palette=current_palette,
               data=iris_tall, margin_titles=True)
plt.savefig('factor_bar_colorbrewer.png')



In [ ]: