Last active
February 23, 2023 20:50
-
-
Save viz-prakash/60c394a8a577420ea02c2cf9e69ffefe to your computer and use it in GitHub Desktop.
Adding an extra legend in matplotlib
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 | |
import matplotlib.patches as mpatches | |
# https://matplotlib.org/3.5.1/tutorials/intermediate/legend_guide.html#sphx-glr-tutorials-intermediate-legend-guide-py | |
x = np.linspace(-0.75,1,100) | |
y0 = np.exp(2 + 3*x - 7*x**3) | |
y1 = 7-4*np.sin(4*x) | |
handles = plt.plot(x,y0,x,y1) | |
red_patch = mpatches.Patch(color='red', label='The red data') | |
handles.append(red_patch) | |
plt.gca().legend(handles=handles, labels=['y0','y1', "add legend here"]) | |
plt.show() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment