Matplotlib Visualization Tutorial

PythonPythonBeginner
Practice Now

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

Introduction

This tutorial will guide you through creating a simple plot using Python's Matplotlib library. Matplotlib is a data visualization library widely used in scientific computing to create static, animated, and interactive visualizations in Python.

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.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL matplotlib(("`Matplotlib`")) -.-> matplotlib/BasicConceptsGroup(["`Basic Concepts`"]) matplotlib(("`Matplotlib`")) -.-> matplotlib/PlottingDataGroup(["`Plotting Data`"]) matplotlib(("`Matplotlib`")) -.-> matplotlib/PlotCustomizationGroup(["`Plot Customization`"]) python(("`Python`")) -.-> python/BasicConceptsGroup(["`Basic Concepts`"]) python(("`Python`")) -.-> python/DataStructuresGroup(["`Data Structures`"]) python(("`Python`")) -.-> python/ModulesandPackagesGroup(["`Modules and Packages`"]) python(("`Python`")) -.-> python/PythonStandardLibraryGroup(["`Python Standard Library`"]) python(("`Python`")) -.-> python/DataScienceandMachineLearningGroup(["`Data Science and Machine Learning`"]) python(("`Python`")) -.-> python/FunctionsGroup(["`Functions`"]) matplotlib/BasicConceptsGroup -.-> matplotlib/importing_matplotlib("`Importing Matplotlib`") matplotlib/BasicConceptsGroup -.-> matplotlib/figures_axes("`Understanding Figures and Axes`") matplotlib/PlottingDataGroup -.-> matplotlib/line_plots("`Line Plots`") matplotlib/PlotCustomizationGroup -.-> matplotlib/grid_config("`Grid Configuration`") python/BasicConceptsGroup -.-> python/variables_data_types("`Variables and Data Types`") python/DataStructuresGroup -.-> python/tuples("`Tuples`") python/ModulesandPackagesGroup -.-> python/importing_modules("`Importing Modules`") python/ModulesandPackagesGroup -.-> python/standard_libraries("`Common Standard Libraries`") python/PythonStandardLibraryGroup -.-> python/data_collections("`Data Collections`") python/DataScienceandMachineLearningGroup -.-> python/numerical_computing("`Numerical Computing`") python/DataScienceandMachineLearningGroup -.-> python/data_visualization("`Data Visualization`") python/FunctionsGroup -.-> python/build_in_functions("`Build-in Functions`") subgraph Lab Skills matplotlib/importing_matplotlib -.-> lab-48943{{"`Matplotlib Visualization Tutorial`"}} matplotlib/figures_axes -.-> lab-48943{{"`Matplotlib Visualization Tutorial`"}} matplotlib/line_plots -.-> lab-48943{{"`Matplotlib Visualization Tutorial`"}} matplotlib/grid_config -.-> lab-48943{{"`Matplotlib Visualization Tutorial`"}} python/variables_data_types -.-> lab-48943{{"`Matplotlib Visualization Tutorial`"}} python/tuples -.-> lab-48943{{"`Matplotlib Visualization Tutorial`"}} python/importing_modules -.-> lab-48943{{"`Matplotlib Visualization Tutorial`"}} python/standard_libraries -.-> lab-48943{{"`Matplotlib Visualization Tutorial`"}} python/data_collections -.-> lab-48943{{"`Matplotlib Visualization Tutorial`"}} python/numerical_computing -.-> lab-48943{{"`Matplotlib Visualization Tutorial`"}} python/data_visualization -.-> lab-48943{{"`Matplotlib Visualization Tutorial`"}} python/build_in_functions -.-> lab-48943{{"`Matplotlib Visualization Tutorial`"}} end

Import necessary libraries

Before we start creating the plot, we need to import the necessary libraries. In this case, we need to import numpy and matplotlib.pyplot.

import numpy as np
import matplotlib.pyplot as plt

Generate Data

We need to generate data for the plot. In this example, we will generate two arrays, t and s.

t = np.arange(0.0, 2.0, 0.01)
s = 1 + np.sin(2 * np.pi * t)

Create the plot

Now that we have the data, we can create the plot. First, we create a figure and axis object using plt.subplots(). Then, we plot the data using ax.plot().

fig, ax = plt.subplots()
ax.plot(t, s)

Add labels and title

We can add labels to the x and y axis, as well as a title to the plot using ax.set().

ax.set(xlabel='time (s)', ylabel='voltage (mV)', title='About as simple as it gets, folks')

Add a grid

Finally, we can add a grid to the plot using ax.grid().

ax.grid()

Show the plot

We can use plt.show() to display the plot.

plt.show()

Summary

This tutorial has walked you through creating a simple plot using Matplotlib. We started by importing the necessary libraries, generated data for the plot, created the plot, added labels and a title, and added a grid. Matplotlib is a powerful library for creating visualizations in Python, and this tutorial is just the beginning of what you can do with it.

Other Python Tutorials you may like