Skip to content

Instantly share code, notes, and snippets.

@viz-prakash
Last active February 23, 2023 20:50
Show Gist options
  • Save viz-prakash/60c394a8a577420ea02c2cf9e69ffefe to your computer and use it in GitHub Desktop.
Save viz-prakash/60c394a8a577420ea02c2cf9e69ffefe to your computer and use it in GitHub Desktop.
Adding an extra legend in matplotlib
Display the source blob
Display the rendered blob
Raw
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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