Exploratory Visualizations part-3 - Styling matplotlib
In the first two posts of this series I did a compare and contrast of out-of-the-box look and feel of Matplotlib and compared it with base R graphics and the ever so awesome ggplot2. In this post, I'll explore some options to make matplotlib plots prettier, or in other words look more like ggplot2.
Let's get right to it by importing the required packages.
#Import required Python modules
import matplotlib as mpl
from matplotlib import pyplot as plt
from matplotlib import colors
from pylab import cm
import numpy as np
import pandas as pd
import pickle
# make graphics inline
%matplotlib inline
ggplot style for matplotlib
The first and easiest way to do this is to use matplotlib.style (for version > 1.4). It has a ggplot style built-in and does a very good job of changing the look and feel.
plt.style.use('ggplot')
Let's bring back the cars dataset and re-plot some figures from the initial post for comparison.
#Read in the dataset from visualizing.org
cars=pickle.load(open('cars.pkl'))
fig, ax = plt.subplots(1, 3, sharex=True, figsize=(16,4))
ax[0].plot(cars['Retail Price'])
ax[0].legend(['Retail Price'], loc='best')
ax[0].set_title('Single Line (Default)', fontsize=12)
ax[1].plot(cars['HP'])
ax[1].plot(cars['Wheel Base'])
ax[1].plot(cars['Len'])
ax[1].plot(cars['Width'])
ax[1].set_title('Multi Line (Default, Fancy Legend)', fontsize=12)
ax[1].legend(['HP','Wheel Base', 'Len', 'Width'], loc='upper center', bbox_to_anchor=(0.5, -0.08),
fancybox=True, shadow=True, ncol=5)
ax[2].plot(cars['HP'], alpha=0.65)
ax[2].plot(cars['Wheel Base'], alpha=0.65)
ax[2].plot(cars['Len'], alpha=0.65)
ax[2].plot(cars['Width'], alpha=0.65)
ax[2].set_title('Multi Line, alpha=0.65', fontsize=12)
# ax[2].legend(['HP','Wheel Base', 'Len', 'Width'], loc='upper center', bbox_to_anchor=(0.5, -0.05),
# fancybox=True, ncol=5)
plt.suptitle('Line Charts with matplotlib', fontsize=15, y=1.02)
plt.show()
That's so much better on the eye. Let's try some more chart types.
fig, ax = plt.subplots(1, 3, figsize=(16,4))
cars['Hwy MPG'].plot(kind='hist', ax=ax[0])
cars['City MPG'].plot(kind='hist', ax=ax[0])
ax[0].set_xlim(5,60)
ax[0].set_title('Histograms (Default)')
ax[0].legend(loc='best')
cars[:10].plot('Vehicle Name','Retail Price', kind='bar', legend=False, ax=ax[1], color='black', alpha=.6, rot=80)
ax[1].set_title('Bar Plot (Default)')
ax[1].set_xlabel('')
cars[['Wheel Base']].plot(kind='hist', orientation='horizontal', ax=ax[2], color='Orange', alpha=.7, legend=False)
ax[2].set_title('Horizontal Histo (Default)')
ax[2].set_xlabel('Wheel Base')
plt.suptitle('Histo and Bar with matplotlib', fontsize=14, y=1.05)
plt.show()
plt.show()
Seaborn
Another cool option to quickly and easily make nicer-looking plots with matplotlib is using the seaborn package. As you can see below, seaborn takes some liberty to take over the color space and can modify the colors different from what we intended. But it never lets you down in terms of visual appeal.
import seaborn as sns
np.random.seed(sum(map(ord, "aesthetics")))
sns.set_context('notebook')
sns.set_style('darkgrid')
fig, ax = plt.subplots(1, 3, sharex=True, figsize=(16,4))
ax[0].plot(cars['Retail Price'])
ax[0].legend(['Retail Price'], loc='best')
ax[0].set_title('Single Line (Default)', fontsize=12)
ax[1].plot(cars['HP'])
ax[1].plot(cars['Wheel Base'])
ax[1].plot(cars['Len'])
ax[1].plot(cars['Width'])
ax[1].set_title('Multi Line (Default, Fancy Legend)', fontsize=12)
ax[1].legend(['HP','Wheel Base', 'Len', 'Width'], loc='upper center', bbox_to_anchor=(0.5, -0.08),
fancybox=True, shadow=True, ncol=5)
ax[2].plot(cars['HP'], alpha=0.65)
ax[2].plot(cars['Wheel Base'], alpha=0.65)
ax[2].plot(cars['Len'], alpha=0.65)
ax[2].plot(cars['Width'], alpha=0.65)
ax[2].set_title('Multi Line, alpha=0.65', fontsize=12)
# ax[2].legend(['HP','Wheel Base', 'Len', 'Width'], loc='upper center', bbox_to_anchor=(0.5, -0.05),
# fancybox=True, ncol=5)
plt.suptitle('Line Charts with matplotlib', fontsize=15, y=1.02)
plt.show()
pricey_cars=cars.sort('Retail Price', ascending=False, inplace=False)
pricey_cars.reset_index(drop=True, inplace=True)
colors=['r','b','g','black','c','y','m']
for i in range(7):
plt.barh(i+1, pricey_cars['Retail Price'].ix[i], label=pricey_cars['Vehicle Name'].ix[i], color=colors[i], alpha=.5)
plt.legend(bbox_to_anchor=(1.9, 1.03))
plt.title('Top 7 Priciest Cars of 2014 (default colors)', fontsize=12)
plt.show()
Colorbrewer
If you're like me and love the vastness of colors you get with R, you'll be pleased to know that the Rcolorbrewer package (which is available by default in R) is available in Python as well, thanks to the author of the blog post at pendandpants here.
Install the brewer2mpl package and you're ready to start using R colors in Python.
import brewer2mpl, matplotlib as mpl
mpl.rcParams['axes.color_cycle'] = brewer2mpl.get_map('Set2', 'qualitative', 7).mpl_colors
pricey_cars=cars.sort('Retail Price', ascending=False, inplace=False)
pricey_cars.reset_index(drop=True, inplace=True)
colors=['deeppink','limegreen','darkorange','darkseagreen','mediumpurple','salmon','goldenrod']
for i in range(7):
plt.barh(i+1, pricey_cars['Retail Price'].ix[i], label=pricey_cars['Vehicle Name'].ix[i], color=colors[i], alpha=.5)
plt.legend(bbox_to_anchor=(1.9, 1.03))
plt.title('Top 7 Priciest Cars of 2014 (default colors)', fontsize=12)
plt.show()
You can also despine easily using seaborn making the chart display only what is necessary and cut out what they call "chartjunk".
pricey_cars=cars.sort('Retail Price', ascending=False, inplace=False)
pricey_cars.reset_index(drop=True, inplace=True)
colors=['deeppink','limegreen','darkorange','darkseagreen','mediumpurple','salmon','goldenrod']
with sns.axes_style('white'):
for i in range(7):
plt.barh(i+1, pricey_cars['Retail Price'].ix[i], label=pricey_cars['Vehicle Name'].ix[i], color=colors[i], alpha=.5)
plt.legend(bbox_to_anchor=(1.5, 1.03))
plt.title('Top 7 Priciest Cars of 2014 (default colors)', fontsize=12)
sns.despine(left=True, bottom=True)
plt.show()
That's it for now. Later in a seperate post or series of posts, I'd like to explore interactive visualization libraries such as plotly, bokeh (which looks awesome) and mpl3d.
Happy Reading!
Share more, Learn more!
Comments
Comments powered by Disqus