See more elementary examples at http://people.duke.edu/~ccc14/cfar-data-2016/index.html#day-2
In [1]:
import numpy as np
import pandas as pd
np.random.seed(123)
In [2]:
from IPython.display import Image
In [3]:
%matplotlib inline
import matplotlib as mpl
import matplotlib.pyplot as plt
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]:
In [7]:
mpl.style.available
Out[7]:
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')
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')
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')
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')
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')
In [19]:
import seaborn as sns
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]:
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]:
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')
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')
In [38]:
sns.choose_colorbrewer_palette('sequential')
Out[38]:
In [39]:
sns.choose_colorbrewer_palette('divergent')
Out[39]:
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 [ ]: