Matplotlib

Getting Started with Matplotlib


Basic plot creation using pyplot

This page covers a getting started with matplotlib tutorial.

Matplotlib offers a vast collection of methods when dealing with plots. In addition, it implements the creation of plots very easy and straightforward. Let’s explore how to create plots step by step.

Firstly, we need to import the module. In matplotlib, the majority of the most commonly used tools are inside the submodule pyplot. As a result, it is a common practice to import the specific submodule, instead of the whole library. Therefore, we import matplotlib.pyplot as plt (common usage).

import matplotlib.pyplot as plt

There are two important functions: plot() and show().

  1. The plot() method draws inside the plot (or chart).
  2. The show() method displays all figures inside the plot.

Let’s look at an example.

import matplotlib.pyplot as plt

# Sample data
x = [1, 2]
y = [5, 6]

# Plot the data
plt.plot(x, y)

# Display the plot
plt.show()

The code creates a basic plot. As seen, the plot() function takes 2 arguments (x & y coordinate points). The first argument passes points on the x-axis and the second on the y-axis.

Note that the function by default produces a line between the points. Below is a basic visual explanation of how coordinates work in a chart.

The example above is very basic and only draws 2 points onto the plot.

Now, we can include more points, making it a bit more interesting.

import matplotlib.pyplot as plt

# Sample data
x = [1, 4, 7, 9]
y = [2, 3, 7, 6]

# Plot the data
plt.plot(x, y)

# Display the plot
plt.show()

Multiple plots

The above scenario displays a chart data with one line. The data has values on the x-axis as well as on the y-axis. Then, the plot() function plots the axes and the show() method displays the outcome.

Following the same logic, we can simply add more data points (separately) and plot them. As a result, we end up having more lines (or multiple plots).

Below is an example of a multiplot figure.

import matplotlib.pyplot as plt

# Sample data 1
x = [1, 4, 7, 9]
y = [2, 3, 7, 6]

# Sample data 2
a = [3, 2, 6, 7]
b = [1, 3, 5, 9]

# Plot the data
plt.plot(x, y)
plt.plot(a, b)

# Display the plot
plt.show()
matplotlib-multiplot-chart

Anatomy of matplotlib figure and axes

In matplotlib, a figure is the whole image, where we can have multiple plots or axes. Axes, on the other hand, represent an individual plot or graph within the figure. Below we break down the anatomy of both.

Anatomy of a Figure

  1. Figure: The entire area where plots are drawn.
  2. Axes: The area on which data is plotted. It’s the actual plot inside the figure.
  3. Axis: The number line-like objects that set the graph limits and generate ticks and ticklabels.
  4. Title: The title of the entire figure.
  5. Legend: A box that explains the meaning of each plot element.
  6. Labels: Labels for the x and y axes.
  7. Ticks and Tick Labels: The marks on the axes that denote specific data values.

Anatomy of Axes

  1. Spines: The lines that make up the edges of the plot.
  2. Ticks: The marks along the axes to denote specific values.
  3. Tick Labels: The text corresponding to each tick mark.
  4. Grid: The horizontal and vertical lines that help in reading values from the plot.
  5. Data: The actual points, lines, bars, or other graphical elements representing the data being visualised.
  6. Annotations: Additional text or shapes added to the plot to provide further information.

The anatomy of figures and axes in matplotlib offers better control over how plots are structured and presented. As a result, this allows for more effective visualisation of data.


This is an original getting started with matplotlib tutorial. Created by aicorr.com.

Next: Customising Plots