Skip to content

What is Matplotlib?

Matplotlib is one of the most popular libraries in Python and is particularly suitable for visualizing data in a wide variety of forms, such as line or bar charts, but also more complex variants. In this article, we take a closer look at how the library is structured and which basic functions users should be familiar with to get the most out of it. To do this, we look at a few specific examples and also show how the charts can be designed to be meaningful.

What is Matplotlib?

Matplotlib is a powerful library in Python that can be used for data visualization. It allows you to display data in the form of charts, graphs, or plots, offering a wide range of line charts to complex multidimensional visualizations. It was developed in 2003 by John Hunter and is now constantly maintained and further developed by a large community of developers.

In practice, it is mainly used for data analysis and in scientific publications, but it also offers the possibility of designing interactive dashboards. Due to the high flexibility of the library, it is not only suitable for quick, exploratory analyses, but also for high-quality illustrations that can be used for technical articles or presentations.

Python offers a variety of modules that can be used for data visualization. Matplotlib forms the basis on which many of these libraries are built. For example, the well-known Seaborn library, which enables several statistical evaluations, is based on Matplotlib, as are the basic Pandas plot functions that are already integrated.

How can Matplotlib be installed?

The Matplotlib library is not included in Python by default, but must be installed once before first use. Depending on the working environment, you can either use pip:

pip install matplotlib

Or you can install it using conda:

conda install matplotlib

You can then simply use the module by importing it at the beginning of the code. The following notation for importing has become standard in recent years:

import matplotlib.pyplot as plt

How does the Figure-Axes-Plot model work in Matplotlib?

Matplotlib is based on a so-called object-oriented approach, which means that diagrams are not drawn anywhere, but follow a clear hierarchy of objects. The most important elements include:

  • The Figure is the entire area in which the diagram will later be output. You can think of it as a canvas on which one or more diagrams can be placed. A grid can be defined on this canvas, where the diagrams can be freely placed.
  • The axes, in turn, are the actual diagram areas within a figure and consist of their own x and y axes. In 3D plots, there may also be an additional Z-axis.
  • Finally, within the axes are the xaxis and yaxis objects, which handle the formatting for the individual charts. These can be used to control the scaling, ticks, or labels on individual axes, for example.
  • The plot elements then contain the representation of the data in their respective form, such as line or bar charts.

In the following example, we create a figure with two diagram areas (axes) arranged side by side. Within these areas, a line chart and a bar chart are drawn.

import matplotlib.pyplot as plt

# Figure with two diagrams next to each other
fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(10, 4))

# First diagram (ax1)
ax1.plot([1, 2, 3], [1, 4, 9], color="blue", marker="o")
ax1.set_title("Squared Values")
ax1.set_xlabel("x")
ax1.set_ylabel("y")

# Second diagram (ax2)
ax2.bar(["A", "B", "C"], [5, 7, 3], color="orange")
ax2.set_title("Bar Chart")
ax2.set_ylabel("Appearances")

plt.suptitle("Example with several axes in one figure")
plt.tight_layout()
plt.show()

This model offers several advantages when working with diagrams. First, it provides a high degree of flexibility, as the arrangement of the diagrams in the figure can be easily customized, and, for example, a 2×2 grid can be created for comparisons. In addition, each Axes object can be controlled more easily, and independent configurations of titles or labels can be made. Finally, the code is also clean, as the object-oriented approach makes it neatly structured and easier to extend.

How can different types of diagrams be implemented in practice?

Matplotlib offers a variety of basic plot types that are particularly frequently used in data analysis. In this section, we will look at some of the most common chart types and see how they can be implemented in Matplotlib.

A line chart is particularly suitable for displaying time series or continuous functions. In our example, we will look at the temperature curve over several days of a week. To do this, we create a figure with a single subplot and generate the line chart with the .plot() function. We pass the values for the x-axis as a Python list first and the values for the y-axis as the second argument. We can then customize the axis labels and the title before finally outputting the chart using plt.show().

import matplotlib.pyplot as plt

# Example: Temperature over 7 days
days = ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"]
temperature = [12, 14, 15, 17, 19, 22, 20]

fig, ax = plt.subplots()
ax.plot(days, temperature, marker="o", color="blue")
ax.set_title("Temperature Over a Week")
ax.set_xlabel("Day")
ax.set_ylabel("Temperature (°C)")
plt.show()

A scatter plot creates point clouds, i.e., a diagram that displays each data point as a single point, revealing relationships and correlations between multiple variables. The basic procedure is the same as for a line chart, with the difference that the .scatter() function is used.

import matplotlib.pyplot as plt

# Example: Study hours vs. test scores
study_hours = [1, 2, 3, 4, 5, 6, 7]
scores = [52, 54, 58, 62, 68, 75, 80]

fig, ax = plt.subplots()
ax.scatter(study_hours, scores, color="green")
ax.set_title("Study Hours vs. Test Scores")
ax.set_xlabel("Study Hours")
ax.set_ylabel("Test Score")
plt.show()

Bar charts are used for data that can be assigned to different categories, allowing you to compare the sales of different product groups, for example. In Matplotlib, this can be done quickly and easily using the .bar() function:

import matplotlib.pyplot as plt

# Example: Sales per product
products = ["Product A", "Product B", "Product C"]
sales = [2300, 1700, 2900]

fig, ax = plt.subplots()
ax.bar(products, sales, color="orange")
ax.set_title("Sales per Product")
ax.set_ylabel("Sales (€)")
plt.show()

Histograms are very well suited for displaying distributions or frequencies, as they show the number of measurement points for each value range. In the Matplotlib function .hist(), a measurement series can be transferred, and the number of bins can also be determined directly, i.e., how many ranges the data should be divided into. For our example, we generate a random measurement series with the ages of a total of 200 people with an average of 35 and a standard deviation of 10.

import matplotlib.pyplot as plt
import numpy as np

# Example: Age distribution in a sample
ages = np.random.normal(35, 10, 200)  # mean=35, std=10

fig, ax = plt.subplots()
ax.hist(ages, bins=10, color="purple", edgecolor="black")
ax.set_title("Age Distribution")
ax.set_xlabel("Age")
ax.set_ylabel("Frequency")
plt.show()

These basic diagrams can be used to implement many applications and visualizations. They form the basis of many evaluations and can also be customized with different labels, colors, and layouts.

How can diagrams in Matplotlib be further customized?

An important advantage of Matplotlib is the great flexibility it offers in the design of diagrams, giving the user complete freedom over various elements. These key features include:

  • Axis labels and titles: As we have already seen in the previous examples, the axes and charts can be clearly named using set_xlabel(), set_ylabel(), and set_title(). If no value has been defined here, Matplotlib simply uses the default settings.
  • Legends: When multiple data series are plotted, it can be useful to include a legend to make it easier for the user to distinguish between the values. This can be easily added using ax.legend().
  • Colors, markers, and line styles: Matplotlib has various parameters that can be used to visually highlight the data series, making them easier to distinguish. In addition to colors, this can also be used to change the marker or line style, for example.
  • Subplots: As already mentioned, plt.subplots() can be used to display multiple charts in a single figure to create dashboards or enable comparisons between charts.
  • Layout adjustments: The plt.tight_layout() function ensures that titles, labels, and axes do not overlap, making the chart appear more harmonious overall.

With the help of these design options, standard charts can be easily customized, and professional plots can be created that are visually appealing. These features are applied in the following example:

import matplotlib.pyplot as plt
import numpy as np

# Example data
x = np.linspace(0, 10, 100)
y1 = np.sin(x)
y2 = np.cos(x)

# Figure with two subplots
fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(12, 4))

# First Subplot: Sine Function
ax1.plot(x, y1, color="blue", linestyle="--", marker="o", label="sin(x)")
ax1.set_title("Sine Function")
ax1.set_xlabel("x values")
ax1.set_ylabel("y values")
ax1.legend()  # Add legend

# Second Subplot: Cosine Function
ax2.plot(x, y2, color="red", linestyle="-", marker="x", label="cos(x)")
ax2.set_title("Cosine Function")
ax2.set_xlabel("x values")
ax2.set_ylabel("y values")
ax2.legend()  # Add legend

plt.tight_layout()
plt.show()

This is what you should take with you

  • Matplotlib is a comprehensive Python library that can be used for data visualization.
  • The basic structure consists of figures and axes in which various diagrams can be displayed. This makes it possible to easily display and compare diagrams side by side.
  • The library supports many different chart types, such as line charts, bar charts, and histograms.
  • In addition, there are many options for further customizing the standard views, for example by changing the axis labels or color palettes.
Python Programming Basics / Python Tutorial

Python Programming Basics: Learn it from scratch!

Master Python Programming Basics with our guide. Learn more about syntax, data types, control structures, and more. Start coding today!

Python Variables / Python Variablen

What are Python Variables?

Dive into Python Variables: Explore data storage, dynamic typing, scoping, and best practices for efficient and readable code.

Jenkins

What is Jenkins?

Mastering Jenkins: Streamline DevOps with Powerful Automation. Learn CI/CD Concepts & Boost Software Delivery.

Conditional Statements in Python / If else Python / Bedingte Anweisung

What are Conditional Statements in Python?

Learn how to use conditional statements in Python. Understand if-else, nested if, and elif statements for efficient programming.

XOR

What is XOR?

Explore XOR: The Exclusive OR operator's role in logic, encryption, math, AI, and technology.

Python Exception Handling / Ausnahmebehandlung in Python

How can you do Python Exception Handling?

Unlocking the Art of Python Exception Handling: Best Practices, Tips, and Key Differences Between Python 2 and Python 3.

You can find the official website here.

Niklas Lang

I have been working as a machine learning engineer and software developer since 2020 and am passionate about the world of data, algorithms and software development. In addition to my work in the field, I teach at several German universities, including the IU International University of Applied Sciences and the Baden-Württemberg Cooperative State University, in the fields of data science, mathematics and business analytics.

My goal is to present complex topics such as statistics and machine learning in a way that makes them not only understandable, but also exciting and tangible. I combine practical experience from industry with sound theoretical foundations to prepare my students in the best possible way for the challenges of the data world.

Cookie Consent with Real Cookie Banner