Thanks to the efforts of some good people (messymind.net, Rob Story, Huy Nguyen, Tony Syu), it is easy to make matplotlib plots look a lot like they were produced by the acclaimed ggplot library for R.
Let's make a simple plot.
from pylab import *
import scipy.stats
def sine_plot():
t = arange(0.0, 100, 1)
s = sin(0.1*pi*t)*exp(-t*0.01)
fig = plt.figure()
ax = fig.add_subplot(111)
ax.plot(t,s, label = "Original")
ax.plot(t,s*2, label = "Doubled")
ax.legend()
return ax
ax = sine_plot()
Adjusting the style settings of matplotlib can go a long way to make this look better. We are going to use Tony Syu's mpltools package.
import mpltools.style
mpltools.style.use('ggplot')
ax = sine_plot()
That's prety good, but to get rid of the annoying extra tick marks at the top and right, we'll need to run some code. Let's bring out rstyle() from messymind's post.
import gg
ax = sine_plot()
gg.rstyle(ax)
That's better! Now let's do a histograms, which should be grayscale.
data = scipy.stats.norm.rvs(size = 1000)
fig = plt.figure()
ax = fig.add_subplot(111)
_ = gg.rhist(ax, data, label='Histogram', bins=50)
ax.legend()
gg.rstyle(ax)
Density plots look nice, with default alpha and edgecolor automatically set to be a darker facecolor.
x_range = np.linspace(-8, 8, 100)
fig = plt.figure()
ax = fig.add_subplot(111)
_ = gg.rfill(ax, x_range, scipy.stats.norm(scale=1).pdf(x_range))
_ = gg.rfill(ax, x_range, scipy.stats.norm(scale=2).pdf(x_range))
gg.rstyle(ax)
Let's plot a density over the histogram to demonstrate twinx().
x_range = np.linspace(-4, 4, 100)
fig = plt.figure()
ax1 = fig.add_subplot(111)
_ = gg.rhist(ax1, data, bins=30)
gg.rstyle(ax1)
ax2 = ax1.twinx()
_ = gg.rfill(ax2, x_range, scipy.stats.norm(scale=1).pdf(x_range))
A box plot is also provided.
box_data = [
scipy.stats.norm.rvs(size = 100),
scipy.stats.norm.rvs(size = 100),
scipy.stats.norm.rvs(size = 100)]
fig = plt.figure()
ax = fig.add_subplot(111)
ax.legend()
gg.rbox(ax, box_data, names=("One", "Two", "Three"), colors=["#1f77b4", "#ff7f0e"])
gg.rstyle(ax)