Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save ImportanceOfBeingErnest/5e61287a6f63deb3a9f3da36c432124a to your computer and use it in GitHub Desktop.
Save ImportanceOfBeingErnest/5e61287a6f63deb3a9f3da36c432124a to your computer and use it in GitHub Desktop.
Code for adding a second legend without instantiating `legend.Legend`.
import numpy as np
import matplotlib.pyplot as plt
lines = []
styles = ['-', '--', '-.', ':']
x = np.linspace(0, 10, 1000)
fig, ax = plt.subplots()
for i in range(4):
lines += ax.plot(x, np.sin(x - i * np.pi / 2),
styles[i], color='black')
ax.axis('equal')
# specify the lines and labels of the first legend
leg1 = ax.legend(lines[:2], ['line A', 'line B'],
loc='upper right', frameon=False)
# Create the second legend and add the first manually.
leg2 = ax.legend(lines[2:], ['line C', 'line D'],
loc='lower right', frameon=False)
ax.add_artist(leg1)
plt.show()
@ImportanceOfBeingErnest
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment