Matplotlib Pyplot Interface Tutorial

MatplotlibMatplotlibBeginner
Practice Now

This tutorial is from open-source community. Access the source code

Introduction

This tutorial provides a step-by-step guide to using the pyplot interface in Matplotlib. The pyplot module is a collection of functions that make Matplotlib work like MATLAB, allowing you to easily create and customize plots. This tutorial assumes you have a basic understanding of Matplotlib and its concepts.

VM Tips

After the VM startup is done, click the top left corner to switch to the Notebook tab to access Jupyter Notebook for practice.

Sometimes, you may need to wait a few seconds for Jupyter Notebook to finish loading. The validation of operations cannot be automated because of limitations in Jupyter Notebook.

If you face issues during learning, feel free to ask Labby. Provide feedback after the session, and we will promptly resolve the problem for you.

Generating a Simple Plot

To start, let's generate a simple plot using the plot function in pyplot. In this example, we'll plot a line graph with the y-values [1, 2, 3, 4]:

import matplotlib.pyplot as plt

plt.plot([1, 2, 3, 4])
plt.ylabel('some numbers')
plt.show()

Explanation:

  • We import the pyplot module from matplotlib and alias it as plt.
  • The plot function is used to generate a line graph. By providing a single list of y-values, the x-values are automatically generated as [0, 1, 2, 3], since Python ranges start with 0.
  • The ylabel function sets the label for the y-axis.
  • Finally, the show function displays the plot.

Formatting the Style of the Plot

Next, let's customize the style of our plot. We can use the optional third argument of the plot function to specify the format string, which indicates the color and line type of the plot. For example, let's plot the same line graph with red circles:

plt.plot([1, 2, 3, 4], [1, 4, 9, 16], 'ro')
plt.axis([0, 6, 0, 20])
plt.show()

Explanation:

  • We use the format string 'ro' to indicate red circles for the plot.
  • The axis function is used to set the viewport of the axes, specifying the range of values for the x- and y-axis.

Plotting Multiple Lines

We can also plot multiple lines with different styles in one function call using arrays. Let's plot three lines: a dashed red line, blue squares, and green triangles:

import numpy as np

t = np.arange(0., 5., 0.2)

plt.plot(t, t, 'r--', t, t**2, 'bs', t, t**3, 'g^')
plt.show()

Explanation:

  • We use the numpy module to create an array t with evenly sampled time values.
  • The plot function is called with three pairs of x and y values, followed by the format strings 'r--' (dashed red line), 'bs' (blue squares), and 'g^' (green triangles).

Plotting with Categorical Variables

Matplotlib allows you to create plots using categorical variables. Let's create a bar plot, scatter plot, and line plot with categorical variables:

names = ['group_a', 'group_b', 'group_c']
values = [1, 10, 100]

plt.figure(figsize=(9, 3))

plt.subplot(131)
plt.bar(names, values)
plt.subplot(132)
plt.scatter(names, values)
plt.subplot(133)
plt.plot(names, values)

plt.suptitle('Categorical Plotting')
plt.show()

Explanation:

  • We create a list names with three categorical values and a list values representing their corresponding values.
  • The figure function is called to create a new figure with a specified size.
  • We use the subplot function to create a grid of subplots. In this example, we create three subplots, each with a different type of plot: bar plot, scatter plot, and line plot.
  • The suptitle function is used to set the super-title of the figure.

Customizing Line Properties

Matplotlib allows you to customize various line properties, such as linewidth, dash style, and color. Let's demonstrate some ways to set line properties:

x = np.arange(0, 5, 0.1)
line, = plt.plot(x, np.sin(x), '-')

## Using the Line2D instance's setter method
line.set_linewidth(2.0)  ## Set the linewidth property of the line to 2.0

## Using the pyplot.setp function
plt.setp(line, color='r', linewidth=2.0)  ## Set the color and linewidth properties using the setp function

plt.show()

Explanation:

  • We create an array x and compute the corresponding y-values using the np.sin function.
  • The plot function is called to create a line plot.
  • We use the set method of the Line2D instance to set the linewidth property of the line to 2.0.
  • Alternatively, we can use the setp function to set multiple properties of the line, such as color and linewidth, using keyword arguments.

Summary

In this tutorial, we learned how to use the pyplot interface in Matplotlib to create and customize plots. We covered generating simple plots, formatting the style of plots, plotting multiple lines, using categorical variables, and customizing line properties. By utilizing these functionalities, you can create various types of plots to visualize your data effectively.

Other Matplotlib Tutorials you may like