Skip to content

Instantly share code, notes, and snippets.

@cwebber314
Created May 3, 2013 05:26
Show Gist options
  • Save cwebber314/5507353 to your computer and use it in GitHub Desktop.
Save cwebber314/5507353 to your computer and use it in GitHub Desktop.
"""
Show legend to left outside of plot area.
"""
import sys
import matplotlib.pyplot as plt
import numpy as np
from matplotlib.pyplot import show
from matplotlib.ticker import MaxNLocator
import matplotlib.gridspec as gridspec
fig = plt.figure()
gs = gridspec.GridSpec(3, 1, height_ratios=[1, 2, 1])
x = np.linspace(0,2*np.pi,100)
y = 2*np.sin(x)
ax1 = plt.subplot(gs[0])
#ax1.set_title('foo')
ax1.plot(x,y, label='foo')
ax1.legend(frameon=True, loc='upper left')
ax1.yaxis.set_major_locator(MaxNLocator(5, prune='both'))
ax1.xaxis.set_ticks_position('none')
ax1.xaxis.set_ticks([])
ax2 = plt.subplot(gs[1])
#ax2.set_title('bar title')
ax2.plot(x,y, label='bar')
ax2.legend(frameon=True, loc='upper left')
#ax2.set_yticks([-2, 0, 2])
ax2.yaxis.set_major_locator(MaxNLocator(5, prune='both'))
ax2.xaxis.set_ticks_position('bottom')
ax2.xaxis.set_label('timestep')
for loc, spine in ax1.spines.items():
if loc in ['right', 'top', 'bottom']:
spine.set_color('none') # don't draw spine
for loc, spine in ax2.spines.items():
if loc in ['bottom']:
spine.set_position(('outward',10)) # outward by 10 points
elif loc in ['right', 'top']:
spine.set_color('none') # don't draw spine
ax1.axvline(5, color='gray', linestyle='--')
ax2.axvline(5, color='gray', linestyle='--')
plt.suptitle('pretty waveform plots', fontsize=14)
plt.subplots_adjust(hspace = 0.05)
plt.subplots_adjust(left = 0.05)
plt.subplots_adjust(right = 0.99)
plt.subplots_adjust(top = 0.92)
plt.xlabel('timestep')
plt.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment