How to do it...

The following code block plots 11 different ways a legend can be specified:

  1. Define the figure with its size:
plt.figure(figsize=(15, 10))

  1. Plot the first graph with an inline label:
plt.subplot(3,4,1)
line, = plt.plot([1, 2, 3], label='Inline label')
plt.legend()
  1. Plot the second graph with the legend, using the set_label method:
plt.subplot(3,4,2)
line, = plt.plot([1, 2, 3])
line.set_label('Label via method')
plt.legend()
  1. Plot the third graph with two lines, and the legend with a list of labels:
plt.subplot(3,4,3)
plt.plot([1, 2, 3])
plt.plot([3, 2, 1])
plt.legend(['Positive Slope', 'Negative Slope'])
plt.title('List of Labels')
  1. Plot the fourth graph with three lines, and the legend with handles and labels:
plt.subplot(3,4,4)
line1, = plt.plot([1, 2, 3])
line2, = plt.plot([3, 2, 1])
line3, = plt.plot([2,2])
plt.legend((line3, line2, line1), ('Zero Slope', 'Negative Slope',
'Positive Slope'))
plt.title('Handles and Labels')
  1. Plot the fifth graph with three lines and legend with a partial list of handles:
plt.subplot(3,4,5)
line_up, = plt.plot([1,2,3], label='Line 2')
line_down, = plt.plot([3,2,1], label='Line 1')
line_3, = plt.plot([2,3,4], label='no label')
plt.legend(handles=[line_up, line_down])
plt.title('Labels for given handles')
  1. Plot the sixth graph with three lines and a partial list of handles and labels:
plt.subplot(3,4,6)
line_up, = plt.plot([1,2,3], label='Line 2')
line_down, = plt.plot([3,2,1], label='Line 1')
line_3, = plt.plot([2,3,4], label='no label')
plt.legend([line_up, line_down], ['Line Up', 'Line Down'])
plt.title('partial handles & labels')

  1. Plot the seventh graph with a patch as a label:
plt.subplot(3,4,7)
red_patch = mpatches.Patch(color='red', label='The red data')
plt.legend(handles=[red_patch])
plt.title('Patch as a label')
  1. Plot the eighth graph with category labels in the legend:
plt.subplot(3,4,8)
z = randn(10)
blue_dot, = plt.plot(z, "bo", markersize=15)

# Put a white cross over some of the data.
white_cross, = plt.plot(z[:5], "w+", markeredgewidth=3,
markersize=15)
plt.legend([blue_dot, (blue_dot, white_cross)], ["Attr A", "Attr
A+B"])
plt.title('category labels')
  1. Plot the ninth graph with the legend on top of the figure in two columns:
plt.subplot(3,4,9)
plt.plot([1, 2, 3], label="test1")
plt.plot([3, 2, 1], label="test2")

# Place a legend above this subplot, expanding itself to
# fully use the given bounding box.
plt.legend(bbox_to_anchor=(0, 1.02, 1., .102), #left, bottom, width,
height
ncol=2, mode="expand", borderaxespad=0.5)
ax = plt.gca()
ax.set_title('Legend on top', pad=20)
  1. Plot the tenth graph with the legend on the right side of the figure:
plt.subplot(3,4,10)
plt.plot([1, 2, 3], label="test1")
plt.plot([3, 2, 1], label="test2")

# Place a legend to the right of this smaller subplot.
plt.legend(bbox_to_anchor=(1.02, 1.0), borderaxespad=0)
plt.title('Legend on right')

  1. Plot the eleventh graph with the legend split into multiple places on the figure:
plt.subplot(3,4,11)
line1, = plt.plot([1, 2, 3], label="Line 1", linestyle='--')
line2, = plt.plot([3, 2, 1], label="Line 2", linewidth=4)

# Create a legend for the first line.
first_legend = plt.legend(handles=[line1], loc=1)

# Add the legend manually to the current Axes. Repeated calls to plt.legend()
# will overwrite previous calls, so only last one remains
ax = plt.gca().add_artist(first_legend)

# Create another legend for the second line.
plt.legend(handles=[line2], loc=4)
plt.title('Split Legend')
  1. Blank out the twelfth plot space:
plt.subplot(3,4,12)
plt.axis('off')
  1. Adjust the space in between the plots, and display the figure on the screen:
plt.tight_layout(w_pad=5, h_pad=5)
plt.show()