matplotlib Plot Compute Model

Posted 4 years, 8 months ago | Originally written on 18 Aug 2014

I'm not a great fan of matplotlib despite how central it's become in data science. This is because of its API: I find it difficult to learn which suggests to me that it's counterintuitive. As long as I'm working with it I can do a lot but the moment I take a break to work on something else it's as if it all evaporates.

For this reason I've put together a barebones outline that I can come to when speed is of the essence.

import numpy as np 
import matplotlib.pyplot as plt

# aspect ratio
figsize = plt.figaspect( 2. )
fig = plt.figure( figsize=figsize )

# figure annotations
fig.set_title( "..." )
fig.suptitle( "..." )
ax.set_ylabel( "..." )

# subplot
ax = fig.add_subplot( 2, 1, 1 )
fig, ax = plt.subplots()
fig, (ax0, ax1 ) = plt.subplots( nrows=2, sharex=True ) 

# lineplot
l = ax.plot( ... )
l.set_dashes(  )

# surface plot
ax = fig.add_subplot( 2, 1, 2, projection='3d' )
surf = ax.plot.surface( ... )
ax.set_zlim3d( -1, 1 )

# histogram/barplot
n, bins, patches = plt.hist( ... )
rects = ax.bar( ... )
autolabel( rects )

# add a grid, modify axes lines and ticks
ax.grid( True )
ax.set_axis_off()
ax.spines['right'].set_visible( False )
ax.spines['right].set_position(( 'outward', 10 )) 
ax.yaxis.set_tick_position( 'left' ) 
ax.set_xlim(( 0, 2*np.pi ))
ax.set_xticks([ 0, np.pi, 2*np.pi ])
ax.set_ticklabels([ '0', '$\pi$', '2$\pi$' ])

# display
plt.subplots_adjust( hspace=0.3 ) 
plt.margins( 0.2 ) 
plt.show()