Matplotlib Python Data Visualization Tutorial

MatplotlibMatplotlibBeginner
Practice Now

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

Introduction

This tutorial introduces the basic usage of Matplotlib library in Python, which is a popular data visualization tool in Python. Matplotlib is a library that allows users to create visualizations such as line plots, scatter plots, bar plots, and many more.

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 python(("`Python`")) -.-> python/BasicConceptsGroup(["`Basic Concepts`"]) matplotlib(("`Matplotlib`")) -.-> matplotlib/BasicConceptsGroup(["`Basic Concepts`"]) matplotlib(("`Matplotlib`")) -.-> matplotlib/PlottingDataGroup(["`Plotting Data`"]) matplotlib(("`Matplotlib`")) -.-> matplotlib/PlotCustomizationGroup(["`Plot Customization`"]) matplotlib(("`Matplotlib`")) -.-> matplotlib/SpecializedPlotsGroup(["`Specialized Plots`"]) python(("`Python`")) -.-> python/ControlFlowGroup(["`Control Flow`"]) python(("`Python`")) -.-> python/DataStructuresGroup(["`Data Structures`"]) python(("`Python`")) -.-> python/ModulesandPackagesGroup(["`Modules and Packages`"]) python(("`Python`")) -.-> python/DataScienceandMachineLearningGroup(["`Data Science and Machine Learning`"]) python/BasicConceptsGroup -.-> python/comments("`Comments`") matplotlib/BasicConceptsGroup -.-> matplotlib/importing_matplotlib("`Importing Matplotlib`") matplotlib/BasicConceptsGroup -.-> matplotlib/figures_axes("`Understanding Figures and Axes`") matplotlib/PlottingDataGroup -.-> matplotlib/line_plots("`Line Plots`") matplotlib/PlottingDataGroup -.-> matplotlib/scatter_plots("`Scatter Plots`") matplotlib/PlottingDataGroup -.-> matplotlib/bar_charts("`Bar Charts`") matplotlib/PlotCustomizationGroup -.-> matplotlib/titles_labels("`Adding Titles and Labels`") matplotlib/SpecializedPlotsGroup -.-> matplotlib/pie_charts("`Pie Charts`") python/ControlFlowGroup -.-> python/for_loops("`For Loops`") python/DataStructuresGroup -.-> python/lists("`Lists`") python/DataStructuresGroup -.-> python/tuples("`Tuples`") python/ModulesandPackagesGroup -.-> python/importing_modules("`Importing Modules`") python/DataScienceandMachineLearningGroup -.-> python/numerical_computing("`Numerical Computing`") python/DataScienceandMachineLearningGroup -.-> python/data_visualization("`Data Visualization`") subgraph Lab Skills python/comments -.-> lab-48800{{"`Matplotlib Python Data Visualization Tutorial`"}} matplotlib/importing_matplotlib -.-> lab-48800{{"`Matplotlib Python Data Visualization Tutorial`"}} matplotlib/figures_axes -.-> lab-48800{{"`Matplotlib Python Data Visualization Tutorial`"}} matplotlib/line_plots -.-> lab-48800{{"`Matplotlib Python Data Visualization Tutorial`"}} matplotlib/scatter_plots -.-> lab-48800{{"`Matplotlib Python Data Visualization Tutorial`"}} matplotlib/bar_charts -.-> lab-48800{{"`Matplotlib Python Data Visualization Tutorial`"}} matplotlib/titles_labels -.-> lab-48800{{"`Matplotlib Python Data Visualization Tutorial`"}} matplotlib/pie_charts -.-> lab-48800{{"`Matplotlib Python Data Visualization Tutorial`"}} python/for_loops -.-> lab-48800{{"`Matplotlib Python Data Visualization Tutorial`"}} python/lists -.-> lab-48800{{"`Matplotlib Python Data Visualization Tutorial`"}} python/tuples -.-> lab-48800{{"`Matplotlib Python Data Visualization Tutorial`"}} python/importing_modules -.-> lab-48800{{"`Matplotlib Python Data Visualization Tutorial`"}} python/numerical_computing -.-> lab-48800{{"`Matplotlib Python Data Visualization Tutorial`"}} python/data_visualization -.-> lab-48800{{"`Matplotlib Python Data Visualization Tutorial`"}} end

Importing the Required Libraries

First, we will import the necessary libraries. We will be using the pyplot module of the matplotlib library to create visualizations.

import matplotlib.pyplot as plt
import numpy as np

Creating a Simple Line Plot

We will create a simple line plot with X-axis values ranging from 0 to 5 and corresponding Y-axis values. We will use the plot function provided by the pyplot module to create the line plot.

## Creating X-axis values
x = np.arange(0, 5, 0.1)

## Creating Y-axis values
y = np.sin(x)

## Creating a line plot
plt.plot(x, y)

## Adding title and labels to the plot
plt.title('Simple Line Plot')
plt.xlabel('X-axis')
plt.ylabel('Y-axis')

## Displaying the plot
plt.show()

Creating a Scatter Plot

We will create a scatter plot with X-axis values ranging from 0 to 5 and corresponding Y-axis values. We will use the scatter function provided by the pyplot module to create the scatter plot.

## Creating X-axis values
x = np.arange(0, 5, 0.1)

## Creating Y-axis values
y = np.sin(x)

## Creating a scatter plot
plt.scatter(x, y)

## Adding title and labels to the plot
plt.title('Scatter Plot')
plt.xlabel('X-axis')
plt.ylabel('Y-axis')

## Displaying the plot
plt.show()

Creating a Bar Plot

We will create a bar plot with X-axis values ranging from 0 to 5 and corresponding Y-axis values. We will use the bar function provided by the pyplot module to create the bar plot.

## Creating X-axis values
x = np.arange(0, 5, 0.1)

## Creating Y-axis values
y = np.sin(x)

## Creating a bar plot
plt.bar(x, y)

## Adding title and labels to the plot
plt.title('Bar Plot')
plt.xlabel('X-axis')
plt.ylabel('Y-axis')

## Displaying the plot
plt.show()

Creating a Pie Chart

We will create a pie chart with five slices representing different data points. We will use the pie function provided by the pyplot module to create the pie chart.

## Creating data for the pie chart
data = [10, 20, 30, 25, 15]

## Creating labels for the pie chart
labels = ['Data 1', 'Data 2', 'Data 3', 'Data 4', 'Data 5']

## Creating a pie chart
plt.pie(data, labels=labels)

## Adding title to the plot
plt.title('Pie Chart')

## Displaying the plot
plt.show()

Summary

In this tutorial, we learned how to use Matplotlib library to create basic visualizations such as line plots, scatter plots, bar plots, and pie charts. We used the pyplot module of the matplotlib library to create these visualizations. Matplotlib is a powerful data visualization tool in Python and can be used to create a wide variety of visualizations.

Other Matplotlib Tutorials you may like