Get started with Matplotlib Visualization

What will we cover?

In this tutorial we will get started with Matplotlib visualization. We will use the object-oriented approach with Matplotlib, this makes it less confusing for only one more line of code.

Plot a list of numbers with Matplotlib

Given a list of numbers, how can you make a connected line.

import matplotlib.pyplot as plt

fig, ax = plt.subplots()

ax.plot([1, 2, 3, 4])

Which results in the following output.

The numbers do not need to be on a straight line. But the line will be connected.

Make a Colored Scatter Plot with Matplotlib

Now you need tree lists.

import matplotlib.pyplot as plt

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

fig, ax = plt.subplots()
ax.scatter(x, y, c=c)
ax.set_title("Title")
ax.set_xlabel("X label")
ax.set_ylabel("Y label")

This results in the following plot.

Notice that we also added title and labels to the axis.

This could also be done in the connected line plot above.

Make a Histogram with Matplotlib

You can make a histogram as follows.

import matplotlib.pyplot as plt

data = [1, 1, 2, 2, 1, 2, 3, 3, 2, 3, 1, 3, 2]

fig, ax = plt.subplots()
ax.hist(data, bins=4)
ax.set_title("Title")
ax.set_xlabel("X label")
ax.set_ylabel("Y label")

This results in the following plot.

Want to learn more?

If this is something you like and you want to get started with Python, then this is part of a 8 hours FREE video course with full explanations, projects on each levels, and guided solutions.

The course is structured with the following resources to improve your learning experience.

  • 17 video lessons teaching you everything you need to know to get started with Python.
  • 34 Jupyter Notebooks with lesson code and projects.
  • A FREE 70+ pages eBook with all the learnings from the lessons.

See the full FREE course page here.

Leave a Reply