Adding a matplotlib legend

Add a label= to each of your plot() calls, and then call legend(loc=”upper left”). Consider this sample (tested with Python 3.8.0): import numpy as np import matplotlib.pyplot as plt x = np.linspace(0, 20, 1000) y1 = np.sin(x) y2 = np.cos(x) plt.plot(x, y1, “-b”, label=”sine”) plt.plot(x, y2, “-r”, label=”cosine”) plt.legend(loc=”upper left”) plt.ylim(-1.5, 2.0) plt.show() Slightly modified … Read more

Combine two Pyplot patches for legend

The solution is borrowed from the comment by CrazyArm, found here: Matplotlib, legend with multiple different markers with one label. Apparently you can make a list of handles and assign only one label and it magically combines the two handles/artists. import matplotlib.patches as mpatches import matplotlib.pyplot as plt import numpy as np x = np.linspace(0,1,11) … Read more

Remove line through marker in matplotlib legend

You can specify linestyle=”None” as a keyword argument in the plot command: import matplotlib.pyplot as pyplot Fig, ax = pyplot.subplots() for i, (mark, color) in enumerate(zip( [‘s’, ‘o’, ‘D’, ‘v’], [‘r’, ‘g’, ‘b’, ‘purple’])): ax.plot(i+1, i+1, color=color, marker=mark, markerfacecolor=”None”, markeredgecolor=color, linestyle=”None”, label=`i`) ax.set_xlim(0,5) ax.set_ylim(0,5) ax.legend(numpoints=1) pyplot.show() Since you’re only plotting single points, you can’t see … Read more

matplotlib: 2 different legends on same graph

There’s a section in the matplotlib documentation on that exact subject. Here’s code for your specific example: import itertools from matplotlib import pyplot colors = [‘b’, ‘r’, ‘g’, ‘c’] cc = itertools.cycle(colors) plot_lines = [] for p in parameters: d1 = algo1(p) d2 = algo2(p) d3 = algo3(p) pyplot.hold(True) c = next(cc) l1, = pyplot.plot(d1, … Read more