Created
March 13, 2020 14:27
-
-
Save ImportanceOfBeingErnest/5e61287a6f63deb3a9f3da36c432124a to your computer and use it in GitHub Desktop.
Code for adding a second legend without instantiating `legend.Legend`.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Corrected version of code found in
https://jakevdp.github.io/PythonDataScienceHandbook/04.06-customizing-legends.html