Introduction to Matplotlib
Welcome to our first lecture on Matplotlib! In this session, we'll dive into the world of data
visualization using one of Python's most popular and powerful libraries.
Matplotlib is a comprehensive library for creating static, animated, and interactive visualizations
in Python. It's often described as the "grandfather" of Python visualization libraries, and for good
reason:
• Versatility: Matplotlib can produce a wide array of high-quality figures, including
line plots, scatter plots, bar charts, histograms, 3D plots, and much more.
• Customization: It offers fine-grained control over every aspect of a plot, from colors
and styles to axes and labels.
• Integration: Matplotlib integrates well with NumPy and can be used in various
Python environments, including Jupyter notebooks, web application servers, and
graphical user interfaces.
• Publication-ready output: The plots created with Matplotlib can be easily adjusted
to meet publication standards.
• Community and ecosystem: As one of the oldest and most widely-used plotting
libraries in Python, Matplotlib has a large community, extensive documentation, and
serves as the foundation for many other visualization libraries like Seaborn.
Matplotlib was created by John Hunter in 2003. His goal was to enable Python users to create
publication-quality plots suitable for scientific research. The library was inspired by MATLAB's
plotting capabilities, which is reflected in its name: "Matplotlib" is a play on "MATLAB plot".
Over the years, Matplotlib has evolved significantly, incorporating new features, improving
performance, and adapting to the changing landscape of data science and scientific computing.
Installing and Importing Matplotlib
Before we can start creating visualizations, we need to make sure Matplotlib is installed and
properly imported into our Python environment. Let's go through this process step by step.
Installation
If you haven't installed Matplotlib yet, you can easily do so using pip, Python's package installer.
Open your terminal or command prompt and run:
pip install matplotlib
For those using Anaconda, you can use conda to install Matplotlib:
conda install matplotlib
Importing Matplotlib
Once installed, we need to import Matplotlib into our Python script or Jupyter notebook. The
convention is to import the pyplot module, which provides a MATLAB-like plotting interface.
Here's how we typically import it:
import matplotlib.pyplot as plt
We use the alias plt for convenience, as it's a widely adopted convention in the Python
community.
Checking the Installation
To verify that Matplotlib is correctly installed and imported, we can check its version:
import matplotlib
matplotlib.__version__
'3.9.1'
This should output the version number of your Matplotlib installation.
Backend Considerations
In Matplotlib, a backend refers to the software layer that actually renders the plot. Think of it as
the "engine" that turns your Python commands into a visual representation. Matplotlib uses two
types of backends:
1. Interactive backends: These allow you to interact with the plot (zoom, pan, save,
etc.) in a separate window. Examples include Qt5Agg and TkAgg.
2. Non-interactive backends: These are used to save plots to files without displaying
them on screen. Examples include Agg for raster graphics and SVG for vector
graphics.
Matplotlib can use various backends to render plots, and the choice of backend can depend on
your specific needs and environment. For instance:
• In scripts, you might use a non-interactive backend to save plots to files.
• In desktop applications, you'd typically use an interactive backend.
• In Jupyter notebooks, you often use the inline backend to display plots directly in the
notebook.
You can set the backend using the following code:
import matplotlib
matplotlib.use('Agg') # Example: setting to Agg backend
However, in Jupyter notebooks, it's common to use the inline backend, which you can set up
with:
%matplotlib inline
This magic command ensures that plots are rendered inline in the notebook, making them
immediately visible after the code cell that creates them.
Understanding backends can be helpful when you need to switch between different plotting
environments or when you're troubleshooting display issues. For most basic plotting tasks, you
won't need to worry about backends, but it's good to know they exist and what they do.
A Simple Test Plot
To make sure everything is working correctly, let's create a simple plot:
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(0, 10, 100)
y = np.cos(x)
plt.plot(x, y)
plt.title("A Simple Sine Wave")
plt.show()
If you see a sine wave plot, congratulations! You've successfully installed and imported
Matplotlib, and you're ready to start your data visualization journey.
In the next section, we'll delve deeper into the structure of Matplotlib and understand its object
hierarchy, which is crucial for creating and customizing plots effectively.
The Matplotlib Object Hierarchy
Understanding Matplotlib's object hierarchy is crucial for creating and customizing plots
effectively. Let's break down the main components:
Figure
At the top of the hierarchy is the Figure object. Think of it as the entire window or page where
your plot(s) will be drawn.
• A figure can contain one or more axes (subplots).
• It manages the canvas, overall figure size, and other global properties.
Axes
Axes are where the actual plotting happens. Each Axes object is a single plot.
• A figure can have multiple axes.
• Axes contain most of the plot elements: X and Y axes, data points, legends, etc.
• When you call plotting methods like plot(), scatter(), or hist(), you're actually
calling methods of the Axes object.
Axis
Each Axes has two (or three in 3D) Axis objects:
• They handle the ticks and labels for the x and y (and z) axes.
• You can customize properties like tick locations, label formatting, and axis limits through
these objects.
Artist
Everything you see in a figure is an Artist object:
• This includes Text, Line2D, collections, Axis, etc.
• Artists handle drawing and layout within the figure.
Figure and Axes Objects
In Matplotlib, plots are organized in a hierarchical structure, with two main components at the
core: Figure and Axes.
A Figure is the top-level container in Matplotlib. It represents the entire window or page where
your plot(s) will be drawn. Think of it as a canvas that can contain one or more plots.
Key points about Figures:
• A Figure can hold multiple Axes (individual plots).
• It controls the overall size and dpi (dots per inch) of the plotting area.
• Figures can have additional elements like titles, legends, and colorbars that apply to all
contained plots.
An Axes is the actual plotting area where data is visualized. It's where you'll find your plotted
data, axis labels, tick marks, and titles.
Key points about Axes:
• Each Axes represents a single plot within a Figure.
• It contains methods for plotting various types of data (e.g., plot(), scatter(),
bar()).
• Axes control aspects like axis limits, scales, and labels.
• A Figure can contain multiple Axes, allowing for subplots.
To visualize this relationship:
Figure
└── Axes
├── Data (Line, Points, Bars, etc.)
├── X-axis (with ticks and label)
├── Y-axis (with ticks and label)
└── Title
Understanding this hierarchy is crucial because it determines how you create, customize, and
manage your plots. For instance:
• Some methods are applied to the Figure (like fig.suptitle() for an overall title).
• Other methods are applied to specific Axes (like ax.set_xlabel() for an x-axis label).
Creating a Figure
In Matplotlib, creating a Figure is typically the first step in generating a visualization. Let's
explore how to create and customize a Figure object.
Basic Figure Creation
The simplest way to create a Figure is using the plt.figure() function:
%matplotlib inline
import matplotlib.pyplot as plt
plt.figure()
<Figure size 640x480 with 0 Axes>
<Figure size 640x480 with 0 Axes>
This creates an empty Figure object with default size and properties.
Customizing Figure Creation
You can customize your Figure by passing parameters to plt.figure():
plt.figure(figsize=(10, 6), dpi=100, facecolor='lightblue',
edgecolor='red')
<Figure size 1000x600 with 0 Axes>
<Figure size 1000x600 with 0 Axes>
Let's break down these parameters:
• figsize: A tuple (width, height) in inches. Default is typically (6.4, 4.8).
• dpi: Dots per inch. Higher values result in larger images. Default is usually 100.
• facecolor: Background color of the Figure. Can be any valid color format.
• edgecolor: Color of the Figure edge. Can be any valid color format.
Other Useful Parameters
• num: An identifier for the Figure. Can be an integer or a string.
• frameon: If False, suppresses drawing the Figure frame.
• tight_layout: Automatically adjusts subplot params for a tight layout.
Example with additional parameters:
plt.figure(num="My Custom Figure", frameon=False, tight_layout=True)
<Figure size 640x480 with 0 Axes>
<Figure size 640x480 with 0 Axes>
Displaying the Figure
In a Jupyter notebook, if you use %matplotlib inline, your plots will show up automatically.
In a regular Python script, you usually need to call plt.show() to see the plot.
Here’s how automatic figure display works in Jupyter notebooks using Matplotlib:
1. Jupyter support: Matplotlib works directly with Jupyter notebooks.
2. Magic command: %matplotlib inline sets up the notebook to show plots
automatically.
3. Automatic display: Any figure created in a cell is shown at the end of that cell.
4. Figure tracking: Matplotlib keeps track of figures you create or change in a cell.
5. Display on completion: When the cell finishes running, new or updated figures are
automatically shown below it.
6. Clearing figures: After showing, figures are usually cleared so you don’t see duplicates in
later cells.
This makes plotting in Jupyter easy—you don’t need to call plt.show() for every plot.
Keep in mind, creating a figure is just the first step. To actually draw a plot, you’ll need to add
axes and plot data, which we will explain next.
For a comprehensive list of Figure parameters and methods, refer to the Matplotlib Figure
documentation. This resource provides detailed information on all available options for fine-
tuning your Figures.
Understanding Axes
Axes are the heart of Matplotlib plots. They contain the data, axes lines, labels, and titles that
make up a plot. Let's dive into how to create, manipulate, and understand Axes objects.
Creating Axes
The easiest way to create Axes is by using a Figure's add_subplot() method:
fig = plt.figure(figsize=(6, 4))
ax = fig.add_subplot(111)
Here, 111 is a short way to say "1 row, 1 column, first subplot." It’s the same as writing (1, 1,
1).
You can also use plt.subplots() to create a Figure and Axes at the same time:
fig, ax = plt.subplots(figsize=(6, 4))
Anatomy of Axes
An Axes has a few main parts:
• Data area – where the plot is drawn
• Axes lines – the X and Y lines around the data
• Ticks – small marks on the axes
• Labels – text showing what the axes or data mean
• Title – the name or description of the plot
Adding Data to Axes
Once you have an Axes object, you can add data to it:
import numpy as np
x = np.linspace(0, 5, 50)
y = np.cos(2 * x)
ax.plot(x, y)
fig
In this example, we've used the set_title(), set_xlabel(), and set_ylabel() methods
of the axis object to add our text elements.
Customizing Axes
Axes objects have numerous methods for customization:
ax.set_xlabel('X Axis')
ax.set_ylabel('Y Axis')
ax.set_title('Cosine Wave')
ax.grid(True)
fig
Axes vs. plt.gca()
When using the pyplot interface:
• plt.gca() (get current axes) gives you the current Axes object.
• plt.gcf() (get current figure) gives you the current Figure object.
These functions are helpful when you want to access or change the current plot.
Note: Many pyplot functions will automatically create a Figure and Axes if they don’t already
exist.
plt.plot(x, y) # Implicitly creates Figure and Axes
plt.xlabel('X Axis')
plt.ylabel('Y Axis')
plt.title('Sine Wave')
plt.grid(True)
Although convenient, automatically creating Figures and Axes can sometimes cause unexpected
results in more complex scripts.
For a full list of Axes methods and properties, see the Matplotlib Axes documentation. It has
detailed information on all the ways you can customize your plots using Axes objects.
Understanding Axes is important for making good visualizations with Matplotlib. In the next
section, we will learn how to use multiple Axes in a single Figure with subplots.
Subplots: Multiple Axes in a Figure
Subplots allow you to create multiple plots within a single Figure, enabling side-by-side
comparisons or the presentation of related data. Let's explore how to create and manipulate
subplots in Matplotlib.
Creating Subplots with plt.subplots()
The most convenient way to create subplots is using plt.subplots():
fig, axs = plt.subplots(2, 2, figsize=(8, 4))
This creates a 2x2 grid of subplots. fig is the Figure object, and axs is a 2D array of Axes
objects.
Adding Data to Subplots
You can access individual Axes using indexing:
import numpy as np
import matplotlib.pyplot as plt
# Create an array of 100 points from 0.1 to 10
# Start from 0.1 to avoid log(0) which is undefined
x = np.linspace(0.1, 10, 100)
# Create a Figure with a 2x2 grid of subplots
fig, axs = plt.subplots(2, 2, figsize=(10, 8))
# Plot sin(x) in the top-left subplot
axs[0, 0].plot(x, np.sin(x))
axs[0, 0].set_title('sin(x)') # Add title to the subplot
# Plot cos(x) in the top-right subplot
axs[0, 1].plot(x, np.cos(x))
axs[0, 1].set_title('cos(x)') # Add title to the subplot
# Plot exp(x) in the bottom-left subplot
axs[1, 0].plot(x, np.exp(x))
axs[1, 0].set_title('exp(x)') # Add title to the subplot
# Plot log(x) in the bottom-right subplot
axs[1, 1].plot(x, np.log(x))
axs[1, 1].set_title('log(x)') # Add title to the subplot
# Adjust layout to prevent overlap between subplots
fig.tight_layout()
# Display the figure
plt.show()
Adjusting Subplot Layouts
Matplotlib provides tools to adjust the layout of subplots:
fig, axs = plt.subplots(2, 2, figsize=(8, 4))
fig.tight_layout(pad=0)
fig.tight_layout(pad=10)
fig
/var/folders/g_/1vbng5jn1nv9ztt7z6_k8lg00000gn/T/
ipykernel_31760/594321112.py:1: UserWarning: Tight layout not applied.
tight_layout cannot make Axes height small enough to accommodate all
Axes decorations.
fig.tight_layout(pad=10)
tight_layout() automatically adjusts subplot params for a tight fit.
Creating Subplots with Unequal Sizes
You can create subplots of different sizes using gridspec:
import matplotlib.gridspec as gridspec
# Create sample data
x = np.linspace(0.1, 10, 100) # Start from 0.1 to avoid log(0)
# Create a Figure
fig = plt.figure(figsize=(6, 4))
# Define a 2x2 GridSpec layout
gs = gridspec.GridSpec(2, 2)
# Add subplots to the figure using GridSpec
ax1 = fig.add_subplot(gs[0, :]) # Top row, spans both columns
ax2 = fig.add_subplot(gs[1, 0]) # Bottom-left subplot
ax3 = fig.add_subplot(gs[1, 1]) # Bottom-right subplot
# Plot data on each subplot
ax1.plot(x, np.sin(x))
ax1.set_title('sin(x)') # Optional title
ax2.plot(x, np.exp(x))
ax2.set_title('exp(x)') # Optional title
ax3.plot(x, np.log(x))
ax3.set_title('log(x)') # Optional title
# Adjust layout to prevent overlap
fig.tight_layout()
# Display the figure
plt.show()
Sharing Axes
You can share x or y axes among subplots:
fig, (ax1, ax2) = plt.subplots(2, 1, sharex=True, figsize=(6, 4))
ax1.plot(x, np.sin(x))
ax2.plot(x, np.cos(x))
[<matplotlib.lines.Line2D at 0x7aa73194afd0>]
Subplots with plt.subplot()
For more control, you can use plt.subplot():
fig = plt.figure(figsize=(6, 4))
plt.subplot(221)
plt.plot(x, np.sin(x))
plt.subplot(222)
plt.plot(x, np.cos(x))
plt.subplot(212)
plt.plot(x, np.tan(x))
[<matplotlib.lines.Line2D at 0x7aa731884550>]
add_subplot() uses a shorthand to place subplots in a grid:
• 221 → 2×2 grid, first subplot
• 222 → 2×2 grid, second subplot
• 212 → 2×1 grid, second subplot
Subplots let you show multiple plots in one figure. Learning how to use them helps make your
visualizations more informative.
For more advanced subplot layouts and options, check the Matplotlib Subplots documentation.
It explains how to create complex layouts and customize subplot behavior.
Customizing Figure and Axes Properties
Customizing your Figure and Axes is crucial for creating clear, informative, and visually
appealing plots. Let's explore various ways to modify the properties of these objects.
Figure Customization
You can adjust the size and resolution of your Figure:
fig, ax = plt.subplots(figsize=(6, 4), dpi=100)
ax.plot([1, 2, 3, 4])
[<matplotlib.lines.Line2D at 0x14c3de890>]
Change the Figure's background color:
fig, ax = plt.subplots()
fig.patch.set_facecolor('#E6E6E6')
ax.plot([1, 2, 3, 4])
fig
Add titles to your Figure:
fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(8, 3))
ax1.plot([1, 2, 3, 4])
ax2.plot([4, 3, 2, 1])
fig.suptitle('Main Title', fontsize=16)
ax1.set_title('Subplot 1')
ax2.set_title('Subplot 2')
Text(0.5, 1.0, 'Subplot 2')
Axes Customization
Set labels and titles for your Axes:
fig, ax = plt.subplots()
ax.plot([1, 2, 3, 4])
ax.set_xlabel('X-axis')
ax.set_ylabel('Y-axis')
ax.set_title('Plot Title')
Text(0.5, 1.0, 'Plot Title')
Control the range of your axes:
fig, ax = plt.subplots()
ax.plot([1, 2, 3, 4])
ax.set_xlim(0, 5)
ax.set_ylim(0, 6)
(0.0, 6.0)
Add or customize grid lines:
fig, ax = plt.subplots()
ax.plot([1, 2, 3, 4])
ax.grid(True, linestyle='--', alpha=0.7)
Customize tick marks and labels:
fig, ax = plt.subplots()
ax.plot([1, 2, 3, 4])
ax.set_xticks([0, 1, 2, 3, 4])
ax.set_xticklabels(['Zero', 'One', 'Two', 'Three', 'Four'],
rotation=45)
ax.set_yticks([1, 2, 3, 4])
ax.set_yticklabels(['Low', 'Medium', 'High', 'Very High'])
[Text(0, 1, 'Low'),
Text(0, 2, 'Medium'),
Text(0, 3, 'High'),
Text(0, 4, 'Very High')]
Styling with rcParams
For consistent styling across all plots, you can modify Matplotlib's rcParams. This stands for
"Run Control" or "Runtime Configuration." It's a common convention in Unix-like systems for
configuration files.
import matplotlib.pyplot as plt
# Set global style parameters for Matplotlib
plt.rcParams['font.size'] = 14 # Default font size for
text
plt.rcParams['axes.labelsize'] = 16 # Font size for axis
labels
plt.rcParams['axes.titlesize'] = 18 # Font size for axes
titles
plt.rcParams['xtick.labelsize'] = 12 # Font size for x-axis
tick labels
plt.rcParams['ytick.labelsize'] = 12 # Font size for y-axis
tick labels
plt.rcParams['legend.fontsize'] = 12 # Font size for legend
text
plt.rcParams['figure.titlesize'] = 20 # Font size for figure
title
# Create a figure and a single axes
fig, ax = plt.subplots()
# Plot data on the axes
ax.plot([1, 2, 3, 4], label='Data') # Simple line plot with a legend
label
# Add labels and title
ax.set_xlabel('X-axis')
ax.set_ylabel('Y-axis')
ax.set_title('Plot with Custom Style')
# Add legend
ax.legend()
# Display the figure
plt.show()
Using Style Sheets
Matplotlib provides pre-defined style sheets for quick styling:
plt.style.use('seaborn-v0_8-darkgrid')
fig, ax = plt.subplots()
ax.plot([1, 2, 3, 4])
ax.set_title('Plot with Seaborn Style')
Text(0.5, 1.0, 'Plot with Seaborn Style')
You can see a list of available styles with plt.style.available:
plt.style.available
['Solarize_Light2',
'_classic_test_patch',
'_mpl-gallery',
'_mpl-gallery-nogrid',
'bmh',
'classic',
'dark_background',
'fast',
'fivethirtyeight',
'ggplot',
'grayscale',
'seaborn-v0_8',
'seaborn-v0_8-bright',
'seaborn-v0_8-colorblind',
'seaborn-v0_8-dark',
'seaborn-v0_8-dark-palette',
'seaborn-v0_8-darkgrid',
'seaborn-v0_8-deep',
'seaborn-v0_8-muted',
'seaborn-v0_8-notebook',
'seaborn-v0_8-paper',
'seaborn-v0_8-pastel',
'seaborn-v0_8-poster',
'seaborn-v0_8-talk',
'seaborn-v0_8-ticks',
'seaborn-v0_8-white',
'seaborn-v0_8-whitegrid',
'tableau-colorblind10']
To reset all rcParams to their default values:
plt.rcParams.update(plt.rcParamsDefault)
For a comprehensive list of customizable properties and methods, refer to the Matplotlib Figure
and Axes documentation. These resources provide detailed information on all available options
for fine-tuning your plots.
Mastering these customization techniques allows you to create polished, professional-looking
visualizations tailored to your specific needs. In the next section, we'll put these skills into
practice with some real-world examples.
Practical Examples
Let's apply what we've learned about Figures and Axes to create some practical, real-world
visualizations. These examples will demonstrate how to combine various concepts to produce
informative and visually appealing plots.
%matplotlib inline
Example 1: Multi-line Plot with Legend
Let's create a plot comparing multiple datasets:
import numpy as np
import matplotlib.pyplot as plt
# Create data points
t = np.linspace(0, 10, 100)
wave1 = np.sin(t) # Sine wave
wave2 = np.cos(t) # Cosine wave
decay = np.exp(-t/10) # Exponential decay
# Create a figure and axes
fig, ax = plt.subplots(figsize=(10, 6))
# Plot the data with different styles
ax.plot(t, wave1, label='Sine', color='purple', linewidth=2)
ax.plot(t, wave2, label='Cosine', color='orange', linestyle='--')
ax.plot(t, decay, label='Decay', color='teal', linestyle='-.')
# Add labels and title
ax.set_xlabel('Time')
ax.set_ylabel('Amplitude')
ax.set_title('Function Comparison Example')
# Add legend and grid
ax.legend()
ax.grid(True, linestyle=':', alpha=0.6)
# Adjust layout
fig.tight_layout()
# Display the plot
plt.show()
This example shows how to plot multiple lines, add a legend, customize colors, and add grid
lines.
Example 2: Scatter Plot with Color Mapping
Let's create a scatter plot with points colored based on a third variable:
import numpy as np
import matplotlib.pyplot as plt
# Set random seed for reproducibility
np.random.seed(123)
# Generate random data
x_vals = np.random.rand(100)
y_vals = np.random.rand(100)
color_vals = np.random.rand(100)
marker_sizes = 150 * np.random.rand(100) # scale sizes
# Create figure and axes
fig, ax = plt.subplots(figsize=(6, 4))
# Scatter plot with color mapping and transparency
scatter_plot = ax.scatter(
x_vals, y_vals,
c=color_vals,
s=marker_sizes,
alpha=0.7,
cmap='plasma', # changed colormap
edgecolor='k' # black edge for markers
)
# Add labels and title
ax.set_xlabel('X Values')
ax.set_ylabel('Y Values')
ax.set_title('Colored Scatter Plot Example')
# Add colorbar
color_bar = fig.colorbar(scatter_plot)
color_bar.set_label('Mapped Color')
# Adjust layout to avoid overlap
fig.tight_layout()
# Display the plot
plt.show()
This example demonstrates creating a scatter plot with points sized and colored based on data
values, and how to add a colorbar.
Example 3: Subplots with Shared Axis
Let's create multiple subplots with a shared x-axis:
import numpy as np
import matplotlib.pyplot as plt
# Generate data points
t = np.linspace(0, 10, 100)
sine_wave = np.sin(t)
cos_wave = np.cos(t)
tan_wave = np.tan(t)
# Create figure with 3 stacked subplots, sharing x-axis
fig, (ax_sin, ax_cos, ax_tan) = plt.subplots(3, 1, figsize=(6, 5),
sharex=True)
# Plot sine wave
ax_sin.plot(t, sine_wave, color='purple', linewidth=2)
ax_sin.set_title('Sine Function')
ax_sin.set_ylabel('Amplitude')
# Plot cosine wave
ax_cos.plot(t, cos_wave, color='orange', linestyle='--')
ax_cos.set_title('Cosine Function')
ax_cos.set_ylabel('Amplitude')
# Plot tangent wave
ax_tan.plot(t, tan_wave, color='teal', linestyle='-.')
ax_tan.set_title('Tangent Function')
ax_tan.set_xlabel('Time')
ax_tan.set_ylabel('Amplitude')
ax_tan.set_ylim(-5, 5) # limit y-axis to avoid extreme values
# Overall figure title
fig.suptitle('Basic Trigonometric Functions', fontsize=16)
# Adjust layout
fig.tight_layout()
# Display plot
plt.show()
This example shows how to create subplots with a shared x-axis, customize individual subplots,
and add an overall title to the figure.
Example 4: Bar Plot with Error Bars
Let's create a bar plot with error bars:
import numpy as np
import matplotlib.pyplot as plt
# Define categories and generate random values with errors
labels = ['A', 'B', 'C', 'D', 'E']
data_values = np.random.randint(10, 50, size=5)
data_errors = np.random.randint(1, 5, size=5)
# Create figure and axes
fig, ax = plt.subplots(figsize=(6, 4))
# Plot bar chart with error bars
ax.bar(
labels, data_values, yerr=data_errors, capsize=4,
color='lightgreen', edgecolor='darkgreen'
)
# Set labels and title
ax.set_xlabel('Category')
ax.set_ylabel('Value')
ax.set_title('Bar Chart with Error Bars')
# Add value labels above each bar
for idx, val in enumerate(data_values):
ax.text(idx, val + data_errors[idx] + 0.3, str(val), ha='center',
fontsize=10)
# Adjust y-axis limit for better spacing
ax.set_ylim(0, max(data_values + data_errors) * 1.2)
# Improve layout
fig.tight_layout()
# Show plot
plt.show()
Basic Plot Types in Matplotlib
In this section, we will learn four main ways to visualize data: line plots, scatter plots, bar
charts, and histograms.
Visualizing data is an important skill in data analysis. It helps us show complex information in a
clear and easy-to-understand way. Matplotlib has many types of plots, but these four are the
most common and useful.
1. Line Plots: Show trends over time or continuous data. Great for seeing how one
variable changes with another.
2. Scatter Plots: Show the relationship between two variables. Useful for spotting
patterns, clusters, or outliers.
3. Bar Charts: Compare values across different categories. Often used in business,
economics, and social sciences.
4. Histograms: Show the distribution of one variable. Help understand the shape,
center, and spread of your data.
Each plot type has its strengths and fits different kinds of data. By the end of this lecture, you
will be able to:
• Create these plots using Matplotlib
• Customize their appearance to show your data clearly
• Choose the right plot type for your data and analysis
Remember, the key is not just making a plot, but picking the plot that best shows your data and
message.
Line Plots
Line plots are one of the most common and versatile plot types in data visualization. They're
excellent for showing trends over time or relationships between continuous variables.
Let's start by creating a simple line plot:
import matplotlib.pyplot as plt
import numpy as np
# Create data
x = np.linspace(0, 10, 100)
y = np.sin(x)
# Create figure and axis objects
fig, ax = plt.subplots(figsize=(6, 3))
# Plot the data
ax.plot(x, y)
# Add title and labels
ax.set_title("Sine Wave")
ax.set_xlabel("X axis")
ax.set_ylabel("Y axis")
Text(0, 0.5, 'Y axis')
In this example, we:
1. Create our data using NumPy.
2. Use plt.subplots() to create a figure and axis object.
3. Use the ax.plot() method to create the line plot.
4. Set the title and axis labels using axis methods.
Customizing Line Plots
We can customize various aspects of our line plot:
# Create a figure and axes
fig, ax = plt.subplots(figsize=(7, 3))
# Plot multiple lines on the same axes
ax.plot(x, np.sin(x), label='sin(x)') # Standard sine curve
ax.plot(x, np.cos(x), label='cos(x)') # Standard cosine curve
# Plot a customized line (sine shifted up by 2)
ax.plot(x, np.sin(x) + 2, linestyle='--', color='r', linewidth=2,
label='sin(x) + 2')
# Add a legend to identify each line
ax.legend()
# Set limits for x and y axes
ax.set_xlim(0, 2*np.pi)
ax.set_ylim(-1.5, 3)
# Add a grid to the plot for better readability
ax.grid(True)
This example demonstrates:
• Plotting multiple lines on the same axes
• Customizing line styles, colors, and widths
• Adding a legend
• Setting axis limits
• Adding a grid
When to Use Line Plots
Line plots are ideal when you want to:
• Show trends over time
• Display continuous data
• Compare multiple series of data
• Visualize mathematical functions
In the next sections, we'll explore other plot types and see how they can be created and
customized using the same fig, ax = plt.subplots() approach.
Scatter Plots
Scatter plots are excellent for visualizing the relationship between two variables. They're
particularly useful for identifying correlations, clusters, or outliers in your data.
Let's start with a simple scatter plot:
# Generate data
np.random.seed(42) # Set seed for reproducibility
x = np.random.rand(50) # 50 random x-values between 0 and 1
y = x + np.random.normal(0, 0.1, 50) # y-values roughly follow x with
some noise
# Create figure and axes
fig, ax = plt.subplots(figsize=(6, 3)) # Create a figure of size 6x3
inches
# Plot the data as a scatter plot
ax.scatter(x, y) # Scatter plot of x vs y
# Add title and axis labels
ax.set_title("Basic Scatter Plot") # Plot title
ax.set_xlabel("X axis") # X-axis label
ax.set_ylabel("Y axis") # Y-axis label
Text(0, 0.5, 'Y axis')
In this example, we:
1. Generate random data using NumPy.
2. Create a figure and axis object using plt.subplots().
3. Use ax.scatter() to create the scatter plot.
4. Set the title and axis labels.
Customizing Scatter Plots
Scatter plots can be customized in various ways to enhance their informativeness:
import numpy as np
import matplotlib.pyplot as plt
# Generate example data
np.random.seed(123)
x_data = np.random.rand(50)
y_data = x_data + np.random.normal(0, 0.1, 50)
colors = np.random.rand(50)
sizes = 100 + 150 * np.random.rand(50) # slightly smaller range for
teaching
# Create the figure and axis
fig, ax = plt.subplots(figsize=(6, 4))
# Plot scatter points with colors and varying sizes
scatter = ax.scatter(x_data, y_data, c=colors, s=sizes, alpha=0.6,
cmap='cool')
# Add a color bar for reference
cbar = fig.colorbar(scatter)
cbar.set_label('Color Scale')
# Add labels and title
ax.set_xlabel("X values")
ax.set_ylabel("Y values")
ax.set_title("Simple Scatter Plot Example")
ax.grid(True, linestyle=':', alpha=0.5)
# Fit a linear trend line and plot it
fit = np.polyfit(x_data, y_data, 1)
line = np.poly1d(fit)
ax.plot(x_data, line(x_data), 'r--', label='Trend Line')
ax.legend()
fig.tight_layout()
This example demonstrates:
• Varying point colors based on a third variable
• Adjusting point sizes
• Adding transparency with alpha
• Including a colorbar
• Adding a trend line
• Customizing the overall appearance with a grid and legend
When to Use Scatter Plots
Scatter plots are ideal when you want to:
• Explore relationships between two continuous variables
• Identify correlations (positive, negative, or no correlation)
• Detect clusters or groupings in your data
• Spot outliers or unusual patterns
• Visualize multivariate data (using color and size for additional dimensions)
Scatter Plot Variations
There are several variations of scatter plots that can be useful in specific scenarios:
1. Bubble charts: Scatter plots where point sizes represent a third variable.
2. Pair plots: A grid of scatter plots showing pairwise relationships in a dataset.
3. 3D scatter plots: For visualizing relationships among three variables.
In the next section, we'll explore bar charts, another fundamental plot type in data visualization.
Bar Charts
Bar charts are excellent for comparing quantities across different categories. They're widely
used in business, economics, and social sciences to display comparative data.
Creating a Basic Bar Chart
Let's start with a simple bar chart:
# Create data
categories = ['A', 'B', 'C', 'D', 'E']
values = [23, 35, 14, 27, 10]
# Create figure and axis objects
fig, ax = plt.subplots(figsize=(7, 5))
# Create the bar chart
ax.bar(categories, values)
# Add title and labels
ax.set_title("Basic Bar Chart")
ax.set_xlabel("Categories")
ax.set_ylabel("Values")
# Add value labels on top of each bar
for i, v in enumerate(values):
ax.text(i, v, str(v), ha='center', va='bottom')
In this example, we:
1. Define categories and their corresponding values.
2. Create a figure and axis object using plt.subplots().
3. Use ax.bar() to create the bar chart.
4. Set the title and axis labels.
5. Add value labels on top of each bar.
Customizing Bar Charts
Bar charts can be customized in various ways to enhance their visual appeal and
informativeness:
import numpy as np
import matplotlib.pyplot as plt
# Sample data
labels = ['A', 'B', 'C', 'D', 'E']
group1_values = [23, 35, 14, 27, 10]
group2_values = [15, 29, 22, 18, 7]
fig, ax = plt.subplots(figsize=(10, 6))
# Position for each group
positions = np.arange(len(labels))
bar_width = 0.4
# Plot bars for each group
bars1 = ax.bar(positions - bar_width/2, group1_values, bar_width,
label='Group 1', color='#87CEEB')
bars2 = ax.bar(positions + bar_width/2, group2_values, bar_width,
label='Group 2', color='#90EE90')
# Add titles and labels
ax.set_title("Grouped Bar Chart Example")
ax.set_xlabel("Category")
ax.set_ylabel("Value")
ax.set_xticks(positions)
ax.set_xticklabels(labels)
ax.legend()
# Add horizontal grid lines
ax.grid(axis='y', linestyle=':', alpha=0.6)
# Function to add value labels above bars
def label_bars(bars):
for bar in bars:
height = bar.get_height()
ax.text(bar.get_x() + bar.get_width()/2, height + 0.5,
str(height),
ha='center', va='bottom', fontsize=10)
label_bars(bars1)
label_bars(bars2)
fig.tight_layout()
This example demonstrates:
• Creating a grouped bar chart to compare two sets of data
• Customizing bar colors
• Adding a legend
• Including a grid for easier value comparison
• Adding value labels to each bar
• Adjusting the overall layout and appearance
When to Use Bar Charts
Bar charts are ideal when you want to:
• Compare quantities across different categories
• Show the distribution of data across discrete groups
• Display data that has distinct, separate categories
• Visualize survey results or categorical data
Bar Chart Variations
There are several variations of bar charts that can be useful in specific scenarios:
1. Stacked bar charts: For showing the composition of each category
2. Horizontal bar charts: Useful when you have long category names or many categories
3. Error bar charts: To display uncertainty or variability in the data
In the next section, we'll explore histograms, which are particularly useful for understanding the
distribution of a single variable.
Histograms
Histograms are powerful tools for visualizing the distribution of a single continuous variable.
They provide insights into the central tendency, spread, and shape of your data.
Creating a Basic Histogram
Let's start with a simple histogram:
# Create data
np.random.seed(42)
data = np.random.normal(100, 20, 1000)
# Create figure and axis objects
fig, ax = plt.subplots(figsize=(8, 4))
# Create the histogram
ax.hist(data, bins=50, edgecolor='black', color='skyblue')
# Add title and labels
ax.set_title("Basic Histogram")
ax.set_xlabel("Values")
ax.set_ylabel("Frequency")
plt.show()
In this example, we:
1. Generate random data from a normal distribution using NumPy.
2. Create a figure and axis object using plt.subplots().
3. Use ax.hist() to create the histogram.
4. Set the title and axis labels.
Customizing Histograms
Histograms can be customized to better represent your data and enhance their visual appeal:
import numpy as np
import matplotlib.pyplot as plt
# Generate two normal distributions
np.random.seed(42)
group_a = np.random.normal(loc=105, scale=18, size=1000)
group_b = np.random.normal(loc=75, scale=25, size=1000)
fig, ax = plt.subplots(figsize=(8, 4))
# Plot overlapping histograms
ax.hist(group_a, bins=25, alpha=0.6, color='#1f77b4',
edgecolor='white', label='Group A')
ax.hist(group_b, bins=25, alpha=0.6, color='#ff7f0e',
edgecolor='white', label='Group B')
# Add vertical lines for means
ax.axvline(group_a.mean(), color='#1f77b4', linestyle='--',
linewidth=2, label='Mean A')
ax.axvline(group_b.mean(), color='#ff7f0e', linestyle='--',
linewidth=2, label='Mean B')
# Add labels and title
ax.set_title("Overlapping Histograms Example")
ax.set_xlabel("Value")
ax.set_ylabel("Count")
# Add grid and legend
ax.grid(axis='y', linestyle=':', alpha=0.5)
ax.legend()
fig.tight_layout()
This example demonstrates:
• Creating overlapping histograms to compare distributions
• Customizing transparency with alpha
• Adding a legend
• Including a grid for easier value comparison
• Adding vertical lines to indicate means
• Adjusting the overall layout and appearance
When to Use Histograms
Histograms are ideal when you want to:
• Visualize the distribution of a single continuous variable
• Identify the central tendency, spread, and shape of the distribution
• Detect outliers or unusual patterns in the data
• Compare distributions of different datasets
Histogram Variations and Related Plots
1. Kernel Density Estimation (KDE) plots: Smooth version of the histogram
2. Cumulative histogram: Shows the cumulative counts or percentages
3. 2D histograms: For visualizing the joint distribution of two variables
# Example of a KDE plot
from scipy import stats
fig, ax = plt.subplots(figsize=(8, 4))
# Plot histogram and KDE
ax.hist(data1, bins=30, density=True, alpha=0.7, edgecolor='black')
kde = stats.gaussian_kde(data1)
x_range = np.linspace(data1.min(), data1.max(), 100)
ax.plot(x_range, kde(x_range), label='KDE')
ax.set_title("Histogram with KDE")
ax.set_xlabel("Values")
ax.set_ylabel("Density")
ax.legend()
<matplotlib.legend.Legend at 0x1443555d0>
This example shows how to overlay a Kernel Density Estimation plot on a histogram, providing a
smooth estimate of the probability density function of the data.
Kernel Density Estimation (KDE) is a non-parametric method for estimating the
probability density function of a random variable based on a finite data sample. In
simple terms, it's a way to smooth out a histogram to create a continuous probability
distribution.
Understanding how to create and interpret histograms is crucial for exploratory data analysis
and for communicating the distribution of your data effectively.
Combining Multiple Plot Types
Combining different plot types can provide a more comprehensive view of your data, allowing
you to showcase various aspects of your dataset in a single figure. Matplotlib's flexibility enables
us to create complex, multi-faceted visualizations easily.
Creating a Figure with Multiple Subplots
Let's start by creating a figure that combines different plot types:
import numpy as np
import matplotlib.pyplot as plt
# Generate data
np.random.seed(42)
x_vals = np.linspace(0, 10, 100)
y_sin = np.sin(x_vals)
y_cos = np.cos(x_vals)
categories = ['A', 'B', 'C', 'D', 'E']
bar_vals = [20, 33, 15, 28, 12]
hist_data = np.random.normal(loc=95, scale=25, size=1000)
# Create a 2x2 grid of subplots
fig, axes = plt.subplots(2, 2, figsize=(9, 6))
# Line plot
axes[0, 0].plot(x_vals, y_sin, label='sin(x)', color='blue')
axes[0, 0].plot(x_vals, y_cos, label='cos(x)', color='red')
axes[0, 0].set_title('Line Plot Example')
axes[0, 0].set_xlabel('X values')
axes[0, 0].set_ylabel('Y values')
axes[0, 0].legend()
axes[0, 0].grid(True, linestyle='--', alpha=0.5)
# Scatter plot
axes[0, 1].scatter(y_sin, y_cos, color='purple', alpha=0.7)
axes[0, 1].set_title('Scatter Plot Example')
axes[0, 1].set_xlabel('sin(x)')
axes[0, 1].set_ylabel('cos(x)')
axes[0, 1].grid(True, linestyle=':', alpha=0.5)
# Bar chart
axes[1, 0].bar(categories, bar_vals, color='teal', edgecolor='black')
axes[1, 0].set_title('Bar Chart Example')
axes[1, 0].set_xlabel('Categories')
axes[1, 0].set_ylabel('Values')
axes[1, 0].grid(axis='y', linestyle='--', alpha=0.5)
# Histogram
axes[1, 1].hist(hist_data, bins=25, color='orange', edgecolor='white',
alpha=0.7)
axes[1, 1].set_title('Histogram Example')
axes[1, 1].set_xlabel('Data values')
axes[1, 1].set_ylabel('Frequency')
axes[1, 1].grid(axis='y', linestyle=':', alpha=0.5)
# Adjust layout
plt.tight_layout()
plt.show()
In this example, we:
1. Create a 2x2 grid of subplots using plt.subplots(2, 2).
2. Plot different types of charts in each subplot.
3. Customize each subplot with titles and labels.
4. Use plt.tight_layout() to automatically adjust spacing between subplots.
Combining Plot Types on the Same Axes
Sometimes, it's useful to combine different plot types on the same axes:
import numpy as np
import matplotlib.pyplot as plt
# Generate data
x_vals = np.linspace(0, 10, 50)
y_sin = np.sin(x_vals)
y_cos = np.cos(x_vals)
y_exp = np.exp(-x_vals / 10)
# Create figure and axis
fig, ax = plt.subplots(figsize=(8, 4.5))
# Plot lines
ax.plot(x_vals, y_sin, label='sin(x)', color='blue', linewidth=2)
ax.plot(x_vals, y_cos, label='cos(x)', color='orange', linestyle='--',
linewidth=2)
# Scatter plot
ax.scatter(x_vals[::3], y_exp[::3], color='red', s=60,
label='exp(-x/10) points', marker='o')
# Transparent bar plot
ax.bar(x_vals[::3], y_exp[::3], width=0.25, alpha=0.4, color='green',
label='exp(-x/10) bars')
# Customize plot
ax.set_title('Mixed Plot Example', fontsize=14)
ax.set_xlabel('X Axis', fontsize=12)
ax.set_ylabel('Y Axis', fontsize=12)
ax.legend()
ax.grid(True, linestyle='-.', alpha=0.6)
plt.tight_layout()
plt.show()
This example demonstrates:
• Plotting multiple line plots on the same axes
• Adding a scatter plot to show specific points
• Including a bar plot to emphasize certain values
• Customizing the overall appearance with a legend and grid
When to Combine Plot Types
Combining plot types can be beneficial when you want to:
• Show different aspects of the same data
• Compare different datasets or variables
• Highlight specific data points or ranges
• Create a dashboard-like view of your data
Tips for Combining Plot Types
1. Keep it clear: Don't overcrowd your plot. Make sure each element serves a purpose.
2. Use consistent colors: If the same data appears in multiple forms, use the same color for
easy identification.
3. Leverage secondary y-axes: For data with different scales, consider using a secondary y-
axis.
4. Add explanatory text: Use annotations to explain complex parts of your visualization.
5. Consider interactivity: For complex combinations, interactive plots (e.g., using libraries
like Plotly) can be very effective.
Combining plot types effectively can lead to powerful, insightful visualizations. However, it's
important to balance complexity with clarity to ensure your message is effectively
communicated.
Practical Examples and Exercises
In this section, we'll apply what we've learned about basic plot types in Matplotlib to real-world
scenarios. We'll go through practical examples and then provide exercises for you to practice on
your own.
Practical Example 1: Analyzing Stock Prices
Let's create a visualization that combines a line plot of stock prices with a bar chart of trading
volumes.
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
# Create sample data
np.random.seed(42)
dates = pd.date_range(start='2023-01-01', end='2023-12-31', freq='D')
prices = 100 + np.cumsum(np.random.randn(len(dates)) * 0.25)
volumes = np.random.randint(1000000, 5000000, size=len(dates))
# Create figure and axis objects
fig, ax1 = plt.subplots(figsize=(8, 3))
# Plot stock prices
ax1.plot(dates, prices, color='blue', label='Stock Price')
ax1.set_xlabel('Date')
ax1.set_ylabel('Price ($)', color='blue')
ax1.tick_params(axis='y', labelcolor='blue')
# Create a secondary y-axis for volume
ax2 = ax1.twinx()
ax2.bar(dates, volumes, alpha=0.3, color='gray', label='Trading
Volume')
ax2.set_ylabel('Volume', color='gray')
ax2.tick_params(axis='y', labelcolor='gray')
# Customize the plot
plt.title('Stock Price and Trading Volume Over Time')
fig.legend(loc='upper right', bbox_to_anchor=(1,1),
bbox_transform=ax1.transAxes)
<matplotlib.legend.Legend at 0x329aa4d30>
This example demonstrates how to:
• Combine a line plot (for stock prices) with a bar chart (for trading volumes)
• Use dual y-axes to display different scales
• Customize colors and labels for clarity
Practical Example 2: Analyzing Survey Results
Let's visualize the results of a hypothetical survey about preferred programming languages.
import matplotlib.pyplot as plt
import numpy as np
# Data
frameworks = ['Python', 'JavaScript', 'Java', 'C++', 'Ruby']
usage = [68, 59, 44, 36, 23]
difficulty_level = [3, 4, 7, 8, 5]
# Create figure and axes with a different style
fig, (ax_bar, ax_scatter) = plt.subplots(1, 2, figsize=(10, 5),
facecolor='#f5f5f5')
# Bar chart with gradient colors
colors_bar = plt.cm.plasma(np.linspace(0.2, 0.8, len(frameworks)))
bars = ax_bar.bar(frameworks, usage, color=colors_bar,
edgecolor='gray', linewidth=1.2)
ax_bar.set_title('Programming Language Popularity', fontsize=14,
color='darkblue')
ax_bar.set_xlabel('Languages', fontsize=11)
ax_bar.set_ylabel('Usage (%)', fontsize=11)
# Add value labels on top of the bars
for bar in bars:
height = bar.get_height()
ax_bar.text(bar.get_x() + bar.get_width()/2, height + 1,
f'{height}%',
ha='center', va='bottom', fontsize=9, color='black')
# Scatter plot with different sizes and colors
colors_scatter =
plt.cm.viridis(np.array(difficulty_level)/max(difficulty_level))
sizes = np.array(usage) * 2 + 50 # Scale point sizes by popularity
ax_scatter.scatter(difficulty_level, usage, s=sizes, c=colors_scatter,
alpha=0.8, edgecolor='k')
# Annotate each point with the framework name
for i, fw in enumerate(frameworks):
ax_scatter.annotate(fw, (difficulty_level[i], usage[i]),
xytext=(7, -7), textcoords='offset points',
fontsize=9, color='darkred')
ax_scatter.set_title('Popularity vs. Perceived Difficulty',
fontsize=14, color='darkgreen')
ax_scatter.set_xlabel('Difficulty (1-10)', fontsize=11)
ax_scatter.set_ylabel('Usage (%)', fontsize=11)
ax_scatter.grid(True, linestyle='-.', alpha=0.6)
plt.tight_layout()
plt.show()
This example shows how to:
• Create a bar chart to show the popularity of programming languages
• Use a scatter plot to explore the relationship between popularity and perceived difficulty
• Add annotations to make the plots more informative
Text, Labels, and Annotations in Matplotlib
In this session, we’re going to focus on one of the most important aspects of data visualization:
effectively communicating information through text elements.
Clear and informative text can transform a good plot into an excellent one. Text provides
context, highlights key points, and guides the viewer’s understanding of the data. In this lecture,
we’ll explore various ways to add and customize text in your Matplotlib plots.
We’ll cover:
• Titles and Axis Labels: How to add descriptive titles and label your axes for clarity.
• Customizing Text Properties: Techniques to adjust font type, size, color, and style to
enhance readability.
• Annotations: Adding explanatory text to specific data points to highlight important
insights.
• Legends: Creating and customizing legends to explain multiple data series clearly.
• Mathematical Expressions: Using LaTeX or math formatting for complex equations
within your plots.
• Tick Labels: Customizing tick marks and labels for precise or more readable axes.
• Colorbars: Adding and styling colorbars for plots that use color mapping, making your
visuals easier to interpret.
By mastering these techniques, you’ll be able to create plots that not only present data
accurately but also communicate insights effectively.
Adding Titles and Axis Labels
While we've touched on this topic before, it's worth revisiting and expanding on the essential
skill of adding titles and axis labels to your plots. Clear, descriptive titles and labels are crucial
for making your visualizations informative and easily understandable.
Let's start with a basic example and then explore some more advanced options:
import matplotlib.pyplot as plt
import numpy as np
%matplotlib inline
Customizing Titles and Labels
Multiple Lines in Titles and Labels
You can also create multi-line titles or labels using \n:
fig, ax = plt.subplots(figsize=(6, 4))
ax.plot(x, y)
ax.set_title('Sine Wave\nAmplitude vs. Time', fontsize=14)
ax.set_xlabel('Time\n(seconds)', fontsize=12)
ax.set_ylabel('Amplitude\n(meters)', fontsize=12)
Text(0, 0.5, 'Amplitude\n(meters)')
Remember, clear and informative titles and labels are key to creating effective visualizations.
They should concisely describe what the plot represents and provide context for the axes. In the
next section, we'll delve deeper into customizing text properties to further enhance our plots.
Working with Text Properties
Text properties in Matplotlib allow you to fine-tune the appearance of your text elements,
making your plots more visually appealing and easier to read. Let's explore various ways to
customize text properties.
Basic Text Properties
Here's an example showcasing some common text properties:
fig, ax = plt.subplots(figsize=(6, 4))
x = np.linspace(0, 10, 100)
y = np.sin(x)
ax.plot(x, y)
ax.set_title('Customized Text Properties', fontsize=16,
fontweight='bold', color='navy')
ax.set_xlabel('X-axis', fontsize=12, fontstyle='italic',
color='darkgreen')
ax.set_ylabel('Y-axis', fontsize=12, fontfamily='serif',
color='darkred')
Text(0, 0.5, 'Y-axis')
In this example, we've customized:
• fontsize: Size of the font
• fontweight: Weight of the font (e.g., 'normal', 'bold', 'light')
• fontstyle: Style of the font (e.g., 'normal', 'italic')
• fontfamily: Font family to use
• color: Color of the text
Using a Dictionary for Text Properties
You can also use a dictionary to set multiple text properties at once:
fig, ax = plt.subplots(figsize=(6, 4))
ax.plot(x, y)
title_props = {
'fontsize': 16,
'fontweight': 'bold',
'color': 'navy',
'verticalalignment': 'bottom'
}
ax.set_title('Title with Property Dictionary', **title_props)
ax.set_xlabel('X-axis')
ax.set_ylabel('Y-axis')
Text(0, 0.5, 'Y-axis')
Text Alignment and Rotation
You can control the alignment and rotation of text:
fig, ax = plt.subplots(figsize=(6, 4))
ax.plot(x, y)
ax.set_title('Centered Title', ha='center')
ax.set_xlabel('Rotated X-Label', rotation=15)
ax.set_ylabel('Vertical Y-Label', rotation=0, ha='right')
Text(0, 0.5, 'Vertical Y-Label')
Here, ha stands for horizontal alignment, and can be 'left', 'center', or 'right'.
Using LaTeX Rendering
Matplotlib can render LaTeX expressions for high-quality typesetting:
fig, ax = plt.subplots(figsize=(6, 4))
x = np.linspace(-10, 10, 100)
y = x ** 2
ax.plot(x, y)
ax.set(
title='$y = x^2$',
xlabel='X-axis',
ylabel='Y-axis'
)
[Text(0.5, 1.0, '$y = x^2$'), Text(0.5, 0, 'X-axis'), Text(0, 0.5, 'Y-
axis')]
Note: LaTeX rendering requires a LaTeX installation on your system.
Custom Fonts
You can use custom fonts if they're installed on your system:
from matplotlib import font_manager
# Add a custom font
font_path = '/path/to/your/font.ttf' # Replace with actual path
font_manager.fontManager.addfont(font_path)
fig, ax = plt.subplots(figsize=(6, 4))
ax.plot(x, y)
ax.set_title('Custom Font Title', fontfamily='Your Font Name',
fontsize=16)
ax.set_xlabel('X-axis')
ax.set_ylabel('Y-axis')
plt.show()
For more information on how to set up custom fonts in Matplotlib, refer to the official
documentation.
Remember, while customizing text properties can enhance your plots, it's important to maintain
readability and consistency. In the next section, we'll explore how to add annotations and text
boxes to highlight specific parts of your plots.
Adding Annotations and Text Boxes
Annotations and text boxes are powerful tools for highlighting specific data points or regions in
your plots, providing additional context or explanations. Let's explore how to add these
elements to your Matplotlib figures.
Basic Annotations
Let's start with a simple annotation:
fig, ax = plt.subplots(figsize=(6, 4))
x = np.linspace(0, 10, 100)
y = np.sin(x)
ax.plot(x, y)
ax.annotate(
'Peak', xy=(np.pi/2, 1), xytext=(np.pi/2, 1.3),
arrowprops=dict(facecolor='black', shrink=0.05),
horizontalalignment='center'
)
Text(1.5707963267948966, 1.3, 'Peak')
Here, we've added an annotation pointing to the peak of the sine wave. The xy parameter
specifies the point to annotate, and xytext determines where the text should be placed.
Customizing Annotations
We can further customize our annotations:
import matplotlib.pyplot as plt
import numpy as np
# Sample data
x = np.linspace(0, 2*np.pi, 100)
y = np.sin(x)
# Create figure and axis
fig, ax = plt.subplots(figsize=(6, 4))
# Plot the sine wave
ax.plot(x, y)
# Add an annotation with an arrow pointing to the local minimum
ax.annotate(
'Local minimum', # Text for the annotation
xy=(3*np.pi/2, -1), # Coordinates of the point
to annotate
xytext=(4, -0.5), # Position of the
annotation text
arrowprops=dict( # Properties of the arrow
facecolor='red', # Arrow color
shrink=0.05, # Shrink factor for arrow
length
width=2 # Width of the arrow shaft
),
fontsize=12, # Font size for annotation
text
color='red', # Text color
bbox=dict( # Background box around
text
boxstyle="round,pad=0.3", # Rounded box with padding
fc="white", # Fill color of the box
ec="red", # Edge color of the box
lw=2 # Line width of the box
border
)
)
# Show the plot
plt.show()
In this example, we've customized the arrow color and width, text color and size, and added a
box around the text.
Text Boxes
Text boxes are useful for adding explanatory text to your plots:
fig, ax = plt.subplots(figsize=(6, 4))
ax.plot(x, y)
ax.text(
5, 0.5, 'This is a sine wave\nwith period 2π',
bbox=dict(facecolor='white', edgecolor='black', alpha=0.7),
ha='center', va='center'
)
Text(5, 0.5, 'This is a sine wave\nwith period 2π')
The text() function allows you to add text at any location on your plot. The bbox parameter
creates a box around the text.
Annotating Multiple Points
You can annotate multiple points using a loop:
fig, ax = plt.subplots(figsize=(6, 4))
x = np.linspace(0, 4*np.pi, 100)
y = np.sin(x)
ax.plot(x, y)
peaks = [(np.pi/2, 1), (5*np.pi/2, 1)]
for i, (xi, yi) in enumerate(peaks, 1):
ax.annotate(
f'Peak {i}', xy=(xi, yi), xytext=(xi, yi+0.3),
arrowprops=dict(facecolor='black', shrink=0.05),
horizontalalignment='center'
)
Annotations and text boxes are excellent for drawing attention to specific aspects of your data or
providing additional context. They can significantly enhance the interpretability of your plots
when used judiciously. In the next section, we'll explore how to create and customize legends to
explain multiple data series in your plots.
Customizing Tick Labels
Tick labels are an essential part of any plot, providing context and scale for your data. Matplotlib
offers various ways to customize these labels to enhance readability and convey information
more effectively. Let's explore some techniques for customizing tick labels.
Basic Tick Label Customization
Let's start with a simple example of customizing tick labels:
fig, ax = plt.subplots(figsize=(6, 4))
x = np.linspace(0, 10, 100)
y = np.sin(x)
ax.plot(x, y)
ax.set_xticks(np.arange(0, 11, 2)) # Set x-ticks at 0, 2, 4, ..., 10
ax.set_yticks(np.arange(-1, 1.1, 0.5)) # Set y-ticks from -1 to 1 in
steps of 0.5
ax.tick_params(axis='both', which='major', labelsize=10,
colors='navy')
Here, we've set custom tick locations and adjusted the size and color of the tick labels.
Rotating Tick Labels
For long tick labels or to avoid overlapping, you can rotate them:
fig, ax = plt.subplots(figsize=(6, 4))
ax.plot(x, y)
ax.set_xticks(np.arange(0, 11, 1))
ax.set_xticklabels(['Point {}'.format(i) for i in range(11)],
rotation=45, ha='right')
plt.tight_layout() # Adjust layout to prevent cut-off labels
Using Scientific Notation
For very large or small numbers, scientific notation can be useful:
fig, ax = plt.subplots(figsize=(6, 4))
x = np.linspace(1e5, 1e6, 100)
y = x**2
ax.plot(x, y)
ax.ticklabel_format(style='sci', axis='both', scilimits=(0,0))
Custom Tick Formatter
For more control, you can use a custom formatter:
from matplotlib.ticker import FuncFormatter
def currency_formatter(x, p):
return f'${x:,.0f}'
fig, ax = plt.subplots(figsize=(6, 4))
x = np.linspace(1000, 10000, 100)
y = x**2
ax.plot(x, y)
ax.yaxis.set_major_formatter(FuncFormatter(currency_formatter))
This example formats y-axis labels as currency.
Log Scale with Custom Labels
For log scales, you might want to customize the labels:
fig, ax = plt.subplots(figsize=(6, 4))
x = np.logspace(0, 3, 100)
y = x**2
ax.loglog(x, y)
ax.set_xticks([1, 10, 100, 1000])
ax.set_xticklabels(['1', '10', '100', '1K'])
ax.set_yticks([1, 1e2, 1e4, 1e6])
ax.set_yticklabels(['1', '100', '10K', '1M'])
[Text(0, 1.0, '1'),
Text(0, 100.0, '100'),
Text(0, 10000.0, '10K'),
Text(0, 1000000.0, '1M')]