import matplotlib.pyplot as plt
%matplotlib inline
import numpy as np
x = np.linspace(0,5,11)
y = x ** 2
functional method plot
plt.plot(x, y)
plt.xlabel('X label')
plt.ylabel('Y label')
plt.title('Title')
plt.show()
Multi plot
plt.subplot(1, 2, 1) # numOfRows, numOfCols, #cases
plt.plot(x, y, 'r')
plt.subplot(1, 2, 2)
plt.plot(y, x, 'b')
OOP plot
fig = plt.figure()
axes = fig.add_axes([0.1, 0.1, 0.8, 0.8])
axes.plot(x, y)
axes.set_xlabel('X label')
axes.set_ylabel('Y label')
axes.set_title('Title')
fig = plt.figure()
axes1 = fig.add_axes([0.1, 0.1, 0.8, 0.8])
axes2 = fig.add_axes([0.2, 0.5, 0.4, 0.3])
axes1.plot(x, y, 'r')
axes2.plot(y, x, 'b+')
axes2.set_title('Smaller')
axes1.set_title('Larger')
fig, axes = plt.subplots(1, 2)
plt.tight_layout() # correct the alignment
axes[0].plot(x, y)
axes[0].set_title('First')
axes[1].plot(y, x)
axes[1].set_title('Second')
Figure size and DPI
fig = plt.figure(figsize=(6,2))
ax = fig.add_axes([0,0,1,1])
ax.plot(x, x**2, label='X squared')
ax.plot(x, x**3, label='X cubed')
ax.legend()
fig.savefig('myfig.png', dpi=200)
Plot appearance
fig = plt.figure(figsize=(10, 4))
ax = fig.add_axes([0,0,1,1])
# color, linewidth, alpha channel,
ax.plot(x, y, 'red', linewidth=3, alpha=0.5, linestyle='-.') #RGB hex code can be used (FF8C00)
#marker
ax.plot(y, x, 'b', linewidth=1, marker='o', markersize=15, markerfacecolor='yellow',
markeredgewidth=3, markeredgecolor='black')
Plot range
fig, axes = plt.subplots(1, 2)
plt.tight_layout()
axes[0].plot(x, y)
axes[0].set_title('First')
axes[1].plot(x, y)
axes[1].set_title('Second')
# zoom in
axes[0].set_xlim([0, 3])
axes[0].set_ylim([0, 2])
Different plots
plt.scatter(x, y)
from random import sample
data = sample(range(1, 1000), 100)
plt.hist(data)
plt.show()
data = [np.random.normal(0, std, 100) for std in range(1, 4)]
plt.boxplot(data, vert=True, patch_artist=True)
plt.show()
import seaborn as sns
%matplotlib inline
tips = sns.load_dataset('tips') # built in example dataset
tips.head()
sns.set_style('darkgrid')
sns.distplot(tips['total_bill'])
sns.set_style('darkgrid')
sns.distplot(tips['total_bill'], kde=False, bins=30)
Joint plot
sns.set_style('darkgrid')
sns.jointplot(x='total_bill', y='tip', data=tips)
sns.set_style('darkgrid')
sns.jointplot(x='total_bill', y='tip', data=tips, kind='hex')
sns.set_style('darkgrid')
sns.jointplot(x='total_bill', y='tip', data=tips, kind='reg')
sns.set_style('darkgrid')
sns.jointplot(x='total_bill', y='tip', data=tips, kind='kde')
Pair plot
sns.pairplot(tips) # plot all pairs in the dataset
sns.pairplot(tips, hue='sex', palette='coolwarm') # hue attrib to categorichal data
# bar plot
sns.barplot(x='sex', y='total_bill', data=tips)
sns.barplot(x='sex', y='total_bill', data=tips, estimator=np.std)
Count plot
sns.countplot(x='sex', data=tips)
Box plot
sns.boxplot(x='day', y='total_bill', data=tips)
sns.boxplot(x='day', y='total_bill', data=tips, hue='smoker')
Violin plot
sns.violinplot(x='day', y='total_bill', data=tips, hue='sex', split=True)
Strip plot
sns.stripplot(x='day', y='total_bill', data=tips, hue='sex')
Swarm plot
sns.swarmplot(x='day', y='total_bill', data=tips, hue='sex')
sns.violinplot(x='day', y='total_bill', data=tips)
sns.swarmplot(x='day', y='total_bill', data=tips, hue='sex')
flights = sns.load_dataset('flights')
flights.head()
# correlation data
tc = tips.corr()
tc
Heat map
sns.heatmap(tc, annot=True, cmap='coolwarm')
f = flights.pivot_table(index='month', columns='year', values='passengers')
f
sns.heatmap(f, cmap='magma', linecolor='white', linewidths=1)
Cluster map
# cluster similar month and year
sns.clustermap(f, cmap='coolwarm', linewidths=1, linecolor='black', standard_scale=1)
iris = sns.load_dataset('iris')
iris.head()
g = sns.PairGrid(iris)
g.map_diag(sns.distplot)
g.map_upper(plt.scatter)
g.map_lower(sns.kdeplot)
tips.head()
g = sns.FacetGrid(data=tips, col='time', row='smoker')
g.map(sns.distplot, 'total_bill')
sns.lmplot(x='total_bill', y='tip', data=tips, hue='sex')
sns.lmplot(x='total_bill', y='tip', data=tips, col='sex', row='time')
sns.set_style('ticks')
sns.countplot(x='sex', data=tips)
sns.despine()
plt.figure(figsize=(12, 3))
sns.set_style('darkgrid')
sns.countplot(x='sex', data=tips)
sns.set_context('poster') # larger text ...
sns.countplot(x='sex', data=tips)
# colors
sns.set_context('notebook')
sns.lmplot(x='total_bill', y='tip', data=tips, hue='sex', palette='seismic') # google -> matplotlib colormap