matplotlib cookbook

Matplotlib is the most popular 2D plotting library in Python. Seaborn is built on top of matplotlib and especially aims to exploratory analysis.

ref:
http://matplotlib.org/
http://seaborn.pydata.org/

matplotlib

import matplotlib
import matplotlib.pyplot as plt

matplotlib.style.use('ggplot')

ref:
http://www.scipy-lectures.org/intro/matplotlib/index.html
https://tonysyu.github.io/raw_content/matplotlib-style-gallery/gallery.html

matplotlib has two interface styles:

# matlab / pylab / pyplot style
plt.plot(x, np.sin(x))

# object-oriented style
fig, ax = plt.subplots()
ax.plot(x, np.sin(x))

Change Line Colors and Styles

plt.plot(x, np.sin(x - 0), color='blue')        # specify color by name
plt.plot(x, np.sin(x - 1), color='g')           # short color code (works for rgb & cmyk)
plt.plot(x, np.sin(x - 2), color='0.75')        # Greyscale between 0 and 1
plt.plot(x, np.sin(x - 3), color='#FFDD44')     # Hex color code (RRGGBB from 00 to FF)
plt.plot(x, np.sin(x - 4), color=(1.0,0.2,0.3)) # RGB tuple, values between 0 and 1
plt.plot(x, np.sin(x - 5), color='chartreuse'); # all html color names are supported;

plt.plot(x, x + 4, linestyle='-')  # solid
plt.plot(x, x + 5, linestyle='--') # dashed
plt.plot(x, x + 6, linestyle='-.') # dashdot
plt.plot(x, x + 7, linestyle=':')  # dotted;

plt.plot(x, x + 0, '-g')  # solid green
plt.plot(x, x + 1, '--c') # dashed cyan
plt.plot(x, x + 2, '-.k') # dashdot black
plt.plot(x, x + 3, ':r')  # dotted red;
plt.plot(x, x + 4, 'o', color='black');

Change Labels

plt.plot(x, np.sin(x))
plt.title("A Sine Curve")
plt.xlabel("x")
plt.ylabel("sin(x)")

plt.plot(x, np.sin(x), '-g', label='sin(x)')
plt.plot(x, np.cos(x), ':b', label='cos(x)')
plt.legend();

Histogram

plt.hist(df['Rating'])

Integrate with Seaborn

from pydataset import data
import pandas as pd
import seaborn as sns

columns = ['carat', 'cut', 'color', 'clarity', 'depth', 'table', 'price', 'x', 'y', 'z']
diamonds = pd.read_csv('datasets/diamonds/diamonds.csv', usecols=columns)

sns.countplot(x='cut', data=diamonds)

sns.barplot(x='cut', y='price', data=diamonds)

sns.jointplot(x='carat', y='price', data=diamonds, size=8, alpha=0.25, marker='.')

sns.pairplot(diamonds, hue='cut')

g = sns.FacetGrid(diamonds, col='color', hue='color', col_wrap=4)
g.map(sns.regplot, 'carat', 'price')

ref:
http://seaborn.pydata.org/tutorial.html
http://seaborn.pydata.org/tutorial/axis_grids.html