How to make two markers share the same label in the legend

Note that in recent versions of matplotlib you can achieve this using class matplotlib.legend_handler.HandlerTuple as illustrated in this answer and also in this guide: import matplotlib.pyplot as plt from matplotlib.legend_handler import HandlerTuple fig, ax1 = plt.subplots(1, 1) # First plot: two legend keys for a single entry p2, = ax1.plot([3, 4], [2, 3], ‘o’, mfc=”white”, … Read more

Matplotlib automatic legend outside plot [duplicate]

EDIT: I HIGHLY RECOMMEND USING THE ANSWER FROM ImportanceOfBeingErnest: How to put the legend outside the plot EDIT END This one is easier to understand: import matplotlib.pyplot as plt x = [1,2,3] plt.subplot(211) plt.plot(x, label=”test1″) plt.plot([3,2,1], label=”test2″) plt.legend(bbox_to_anchor=(0, 1), loc=”upper left”, ncol=1) plt.show() now play with the to coordinates (x,y). For loc you can use: … Read more

Scatter plot with color label and legend specified by c option [duplicate]

As in the example you mentioned, call plt.scatter for each group: import numpy as np from matplotlib import pyplot as plt scatter_x = np.array([1,2,3,4,5]) scatter_y = np.array([5,4,3,2,1]) group = np.array([1,3,2,1,3]) cdict = {1: ‘red’, 2: ‘blue’, 3: ‘green’} fig, ax = plt.subplots() for g in np.unique(group): ix = np.where(group == g) ax.scatter(scatter_x[ix], scatter_y[ix], c = … Read more

How to make a colored markers legend from scratch

Following the example given in the legend guide, you can use a Line2D object instead of a marker object. The only difference to the example given in the guide is you want to set linestyle=”None” import matplotlib.lines as mlines import matplotlib.pyplot as plt blue_star = mlines.Line2D([], [], color=”blue”, marker=”*”, linestyle=”None”, markersize=10, label=”Blue stars”) red_square = … Read more