Skip to main content
TopMiniSite

Back to all posts

How to Add Legends to A Matplotlib Plot?

Published on
4 min read
How to Add Legends to A Matplotlib Plot? image

Best Matplotlib Plotting Tools to Buy in October 2025

1 Dunzoom 3 Pcs Marine Navigation Kit, Basic Navigation Set Include 18" Marine Parallel Ruler with Clear Scales, 8" Diameter Nautical Plotter Protractor, 6" Fixed Point Divider for Boat Accessories

Dunzoom 3 Pcs Marine Navigation Kit, Basic Navigation Set Include 18" Marine Parallel Ruler with Clear Scales, 8" Diameter Nautical Plotter Protractor, 6" Fixed Point Divider for Boat Accessories

  • ESSENTIAL ALL-IN-ONE NAVIGATION KIT FOR CONFIDENT SAILING ADVENTURES.
  • DURABLE DESIGN ENSURES PRECISE READINGS AND LONG-LASTING RELIABILITY.
  • EFFORTLESSLY ENHANCE YOUR NAVIGATION SKILLS WITH CONVENIENT TOOLS.
BUY & SAVE
$32.99
Dunzoom 3 Pcs Marine Navigation Kit, Basic Navigation Set Include 18" Marine Parallel Ruler with Clear Scales, 8" Diameter Nautical Plotter Protractor, 6" Fixed Point Divider for Boat Accessories
2 Weems & Plath #176 Marine Navigation Ultralight Divider

Weems & Plath #176 Marine Navigation Ultralight Divider

  • DURABLE MARINE ALLOY: CORROSION-RESISTANT FOR LONG-LASTING USE.
  • EFFORTLESS OPERATION: CENTER GEAR MECHANISM ENSURES EASY HANDLING.
  • TRUSTWORTHY QUALITY: MADE IN GERMANY WITH A LIFETIME WARRANTY INCLUDED.
BUY & SAVE
$29.00
Weems & Plath #176 Marine Navigation Ultralight Divider
3 WEEMS & PLATH Essentials Navigation Kit

WEEMS & PLATH Essentials Navigation Kit

  • ULTRALIGHT DESIGN FOR EASY PORTABILITY AND CONVENIENCE.
  • ACCURATE PLOTTING WITH WEEMS PARALLEL PLOTTER FOR NAVIGATION.
  • NAUTICAL SLIDE RULE ENHANCES EFFICIENCY IN MARITIME CALCULATIONS.
BUY & SAVE
$84.00
WEEMS & PLATH Essentials Navigation Kit
4 Weems & Plath Marine Navigation Primary Navigation Set

Weems & Plath Marine Navigation Primary Navigation Set

  • ULTRALIGHT DIVIDER/COMPASS FOR PRECISE MEASUREMENTS AND NAVIGATION
  • 12 PARALLEL RULER FOR ACCURATE DRAWING AND DRAFTING TASKS
  • HANDY PLASTIC POUCH KEEPS ALL TOOLS ORGANIZED AND PROTECTED
BUY & SAVE
$92.00
Weems & Plath Marine Navigation Primary Navigation Set
5 Weems & Plath Marine Navigation Parallel Plotter

Weems & Plath Marine Navigation Parallel Plotter

  • PRECISION CRAFTSMANSHIP ENSURES RELIABLE NAVIGATION EVERY TRIP.
  • COMPACT DESIGN FITS EASILY IN ANY BOATING GEAR OR BAG.
  • HIGH-QUALITY MATERIALS GUARANTEE DURABILITY UNDER HARSH CONDITIONS.
BUY & SAVE
$39.00
Weems & Plath Marine Navigation Parallel Plotter
6 Premier Fixed Plotter for Pilots, Efficiently Plotting Tool for Pilots, Planning Flight Routes, Accurate Scale, Transparent

Premier Fixed Plotter for Pilots, Efficiently Plotting Tool for Pilots, Planning Flight Routes, Accurate Scale, Transparent

  • ACCURATE MEASUREMENT FOR EFFORTLESS ROUTE PLANNING IN AVIATION.
  • DURABLE RESIN WITHSTANDS EXTREME TEMPERATURES WITHOUT DAMAGE.
  • QUICK SCALE REFERENCES FOR EASY READING OF DISTANCES.
BUY & SAVE
$7.99
Premier Fixed Plotter for Pilots, Efficiently Plotting Tool for Pilots, Planning Flight Routes, Accurate Scale, Transparent
7 Drawing Compass and 6 Inch Protractor, Student Geometry Math Set

Drawing Compass and 6 Inch Protractor, Student Geometry Math Set

  • ALL-IN-ONE KIT: INCLUDES COMPASS, REFILL, AND PROTRACTOR FOR CONVENIENCE.
  • CLEAR STORAGE BOX: KEEPS TOOLS ORGANIZED AND EASY TO CARRY ANYWHERE.
  • PRECISION MEASUREMENTS: PROTRACTOR OFFERS CLEAR 0-180° READINGS IN BOTH UNITS.
BUY & SAVE
$5.98
Drawing Compass and 6 Inch Protractor, Student Geometry Math Set
+
ONE MORE?

Adding legends to a matplotlib plot is a useful way to label the different elements or data series in a plot. A legend can provide context and make it easier to interpret the chart. Here is how you can add a legend to a matplotlib plot:

  1. Import the necessary library:

import matplotlib.pyplot as plt

  1. Create your plot using the plt.plot() function or any other plotting function from matplotlib.
  2. Assign labels to each data series or element you want to include in the legend. This can be done by passing a label parameter to the plotting function for each data series:

plt.plot(x, y1, label='Data Series 1') plt.plot(x, y2, label='Data Series 2')

  1. After plotting all the data series, call the plt.legend() function. By default, it will create a legend using the labels defined earlier:

plt.legend()

  1. Optionally, you can customize the position and appearance of the legend by passing additional parameters to plt.legend(). For example, you can specify the location of the legend using the loc parameter:

plt.legend(loc='upper right')

Here are a few examples of available loc values: 'best', 'upper right', 'upper left', 'lower left', 'lower right', 'center', 'center left', 'center right', 'right', 'upper center', 'lower center'.

You can also control other properties of the legend, such as the title or the background color, by passing additional arguments to plt.legend().

  1. When you are finished customizing your plot, call plt.show() to display the plot with the legend.

Make sure to include these steps in your code to add legends to your matplotlib plots effectively.

What is the purpose of legends in Matplotlib plots?

The purpose of legends in Matplotlib plots is to provide a textual description or explanation of the elements present in the plot. Legends help in identifying the different data series or groups represented in the plot, especially when multiple elements or categories are plotted together. It allows the viewer to understand the meaning or interpretation of the different colors, shapes, or markers used in the plot. Legends are particularly useful in different types of plots, such as line plots, scatter plots, or bar plots, where multiple data series or groups are visualized in a single plot.

How to change the font size of a legend in Matplotlib?

To change the font size of a legend in Matplotlib, you can use the fontsize parameter in the plt.legend() function. Here is an example:

import matplotlib.pyplot as plt

Generate some data

x = [1, 2, 3] y = [4, 5, 6]

Scatter plot

plt.scatter(x, y, label='Data')

Create legend

legend = plt.legend(fontsize='large')

Show the plot

plt.show()

In this example, fontsize='large' is used to set the font size of the legend to a larger size. You can also set it to other values like 'small', 'medium', or a numeric value like 10.

What is the difference between a legend and a key in Matplotlib?

In Matplotlib, a legend and a key serve similar purposes of providing additional information about the plot elements. However, there are some differences between the two:

  1. Definition: A legend is a box or area on the plot that displays symbolic representations (e.g., colored lines, markers, etc.) and their respective labels, indicating what each element represents. On the other hand, a key is a single feature on the plot that represents an aspect of the visual representation (e.g., color, marker, linetype, etc.) and its corresponding meaning.
  2. Usage: A legend is commonly used when there are multiple elements in a plot, each with a unique label. It helps in identifying and understanding the elements based on their labels. A key, on the other hand, is primarily used to explain the meaning of various visual properties or styles used in the plot, such as color coding or line types.
  3. Appearance: A legend is typically displayed as a separate box or area within the plot, often placed at a corner or a specified position. It contains the labels and corresponding symbols or representations associated with the plot elements. A key, on the other hand, is a single representative feature on the plot, such as a colored line or a marker, accompanied by a label or text explaining its meaning.

In summary, a legend is used to display and explain multiple plot elements with their respective labels, while a key represents a single visual property or style and its corresponding meaning.