Getting Started with Matplotlib – AICorr.com



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 diagram).
  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 diagram.

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()

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


Discover more from reviewer4you.com

Subscribe to get the latest posts to your email.

We will be happy to hear your thoughts

Leave a reply

0
Your Cart is empty!

It looks like you haven't added any items to your cart yet.

Browse Products
Powered by Caddy

Discover more from reviewer4you.com

Subscribe now to keep reading and get access to the full archive.

Continue reading