How do you create a legend for a contour plot?

You could also do it directly with the lines of the contour, without using proxy artists. import matplotlib import numpy as np import matplotlib.cm as cm import matplotlib.mlab as mlab import matplotlib.pyplot as plt matplotlib.rcParams[‘xtick.direction’] = ‘out’ matplotlib.rcParams[‘ytick.direction’] = ‘out’ delta = 0.025 x = np.arange(-3.0, 3.0, delta) y = np.arange(-2.0, 2.0, delta) X, Y … Read more

How to make custom legend

You can chose the artists and labels to display in the legend as follows. You’ll need to create custom artists for the elements in the legend that are not actually plotted. import matplotlib.pyplot as plt import numpy as np x = np.linspace(0,10,31) fig = plt.figure() ax = fig.add_subplot(1,1,1) #Plot analytic solution ax.plot(x,1*x**2, color=”r”, label=”t = … Read more

Remove line through legend marker with .plot

You can specify linestyle=”None” or linestyle=”” as a keyword argument in the plot command. Also, ls= can replace linestyle=. import matplotlib.pyplot as plt fig, ax = plt.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) plt.show() … Read more

How do I assign multiple legend labels at once?

You can iterate over your line objects list, so labels are individually assigned. An example with the built-in python iter function: lineObjects = plt.plot(x, y) plt.legend(iter(lineObjects), (‘foo’, ‘bar’, ‘baz’))` Edit: after updating to matplotlib 1.1.1, it looks like the plt.plot(x, y), with y as a list of lists (as provided by the author of the … Read more

How to specify legend position in graph coordinates

The loc parameter specifies in which corner of the bounding box the legend is placed. The default for loc is loc=”best” which gives unpredictable results when the bbox_to_anchor argument is used. Therefore, when specifying bbox_to_anchor, always specify loc as well. The default for bbox_to_anchor is (0,0,1,1), which is a bounding box over the complete axes. … Read more

Global legend and title aside subplots

Global title: In newer releases of matplotlib one can use Figure.suptitle() method of Figure: import matplotlib.pyplot as plt fig = plt.gcf() fig.suptitle(“Title centered above all subplots”, fontsize=14) Alternatively (based on @Steven C. Howell’s comment below (thank you!)), use the matplotlib.pyplot.suptitle() function: import matplotlib.pyplot as plt # plot stuff # … plt.suptitle(“Title centered above all subplots”, … Read more