import matplotlib.pyplot as plt


fig = plt.figure()

ax = fig.add_subplot(211)
ax.plot([1, 2, 3], label="test1")
ax.plot([3, 2, 1], label="test2")
# Place a legend above this subplot, expanding itself to
# fully use the given bounding box.
ax.legend(bbox_to_anchor=(0., 1.02, 1., .102), loc='lower left',
           ncol=2, mode="expand", borderaxespad=0.)

ax = fig.add_subplot(223)
ax.plot([1, 2, 3], label="test1")
ax.plot([3, 2, 1], label="test2")
# Place a legend to the right of this smaller subplot.
ax.legend(bbox_to_anchor=(1.05, 1), loc='upper left', borderaxespad=0.)

plt.show()

 

Simple Legend02

import matplotlib.pyplot as plt

fig, ax = plt.subplots()

line1, = ax.plot([1, 2, 3], label="Line 1", linestyle='--')
line2, = ax.plot([3, 2, 1], label="Line 2", linewidth=4)

# Create a legend for the first line.
first_legend = ax.legend(handles=[line1], loc='upper right')

# Add the legend manually to the current Axes.
ax.add_artist(first_legend)

# Create another legend for the second line.
ax.legend(handles=[line2], loc='lower right')

plt.show()

Related articles

PGF texsystem

import matplotlib.pyplot as plt plt.rcParams.update({ "pgf.texsystem": "pdflatex", "pgf.preamble": "\n".join([ r"\usepackage[utf8x]{inputenc}", r"\usepackage[T1]{fontenc}", r"\usepackage{cmbright}", ]), })

PGF preamble

import matplotlib as mpl mpl.use("pgf") import matplotlib.pyplot as plt plt.rcParams.update({

matplotlib pgf fonts

PGF fonts import matplotlib.pyplot as plt plt.rcParams.update({ "font.family": "serif", # Use LaTeX default serif font. "font.serif": [],

matplotlib gridspec

This example demonstrates the use of `.pyplot.subplot2grid` to generate subplots.  Using `.GridSpec`, as demonstrated in :doc:`/gallery/userdemo/demo_gridspec03` is generally preferred

custom boxstyle

This example demonstrates the implementation of a custom `.BoxStyle`. Custom `.ConnectionStyle`\s and `.ArrowStyle`\s can be similarly defined.

connection style

When creating an annotation using `~.Axes.annotate`, the arrow shape can be controlled via the *connectionstyle* parameter of *arrowprops*. For further details see the description of `.FancyArrowPatch`.

matplotlib connect

ConnectionPatch` can be used to draw a line (possibly with arrow head) between points defined in different coordinate systems and/or axes.

annotate tex arrow

import numpy as np import matplotlib.pyplot as plt # Fixing random state for reproducibility np.random.seed(19680801)

matplotlib annotate explain

import matplotlib.pyplot as plt import matplotlib.patches as mpatches fig, axs = plt.subplots(2, 2) x1, y1 = 0.3, 0.3 x2, y2 = 0.7, 0.7

matplotlib anchored box04

from matplotlib.patches import Ellipse import matplotlib.pyplot as plt from matplotlib.offsetbox import (AnchoredOffsetbox, DrawingArea, HPacker, TextArea)