Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save MatthieuLoustaunau/5e3a6929573c4d600d0a58119bc0bde9 to your computer and use it in GitHub Desktop.
Save MatthieuLoustaunau/5e3a6929573c4d600d0a58119bc0bde9 to your computer and use it in GitHub Desktop.
legend outside plot matplotlib
import matplotlib.pyplot as plt
import numpy as np
x = np.arange(10)
fig = plt.figure()
ax = plt.subplot(111)
for i in xrange(5):
ax.plot(x, i * x, label='$y = %ix$'%i)
# Shrink current axis by 20%
box = ax.get_position()
ax.set_position([box.x0, box.y0, box.width * 0.8, box.height])
# Put a legend to the right of the current axis
ax.legend(loc='center left', bbox_to_anchor=(1, 0.5))
plt.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment