Python Matplotlib Plotting Basics

MatplotlibMatplotlibBeginner
Practice Now

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

Introduction

This tutorial will guide you through the basics of Python Matplotlib, a plotting library for the Python programming language. In this tutorial, you will learn how to create and customize different types of plots using Matplotlib.

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.

Installation

Before using Matplotlib, you need to install it. You can use pip, a package manager for Python, to install Matplotlib by running the following command in your terminal:

pip install matplotlib

Importing Matplotlib

To use Matplotlib, you need to import it in your Python script. You can import Matplotlib using the following code:

import matplotlib.pyplot as plt

Creating a Simple Plot

The most basic plot in Matplotlib is a line plot. You can create a line plot using the plot() method. Here's an example code that creates a simple line plot:

import matplotlib.pyplot as plt

## Data
x = [1, 2, 3, 4, 5]
y = [2, 4, 6, 8, 10]

## Create a plot
plt.plot(x, y)

## Add labels and title
plt.xlabel('X-axis')
plt.ylabel('Y-axis')
plt.title('Simple Plot')

## Display the plot
plt.show()

In this code, we first define our data points as two lists x and y. We then create a plot using the plot() method and pass in our data points. We then add labels to the X and Y axes and a title to the plot using the xlabel(), ylabel(), and title() methods. Finally, we display the plot using the show() method.

Customizing the Plot

Matplotlib offers a wide range of customization options for plots. Here's an example code that customizes our simple line plot:

import matplotlib.pyplot as plt

## Data
x = [1, 2, 3, 4, 5]
y = [2, 4, 6, 8, 10]

## Create a plot
plt.plot(x, y, color='red', linewidth=2, linestyle='--', marker='o', markersize=8, markerfacecolor='yellow')

## Add labels and title
plt.xlabel('X-axis')
plt.ylabel('Y-axis')
plt.title('Customized Plot')

## Display the plot
plt.show()

In this code, we use various parameters of the plot() method to customize the plot. We change the color of the line to red, the linewidth to 2, the linestyle to dashed (--), the marker to a circle (o), the markersize to 8, and the markerfacecolor to yellow.

Creating Different Types of Plots

Matplotlib supports a wide range of plot types, including line plots, scatter plots, bar plots, and more. Here's an example code that creates a scatter plot:

import matplotlib.pyplot as plt
import numpy as np

## Generate some random data
x = np.random.rand(50)
y = np.random.rand(50)
colors = np.random.rand(50)
sizes = 1000 * np.random.rand(50)

## Create a scatter plot
plt.scatter(x, y, c=colors, s=sizes, alpha=0.5)

## Add labels and title
plt.xlabel('X-axis')
plt.ylabel('Y-axis')
plt.title('Scatter Plot')

## Display the plot
plt.show()

In this code, we use the scatter() method to create a scatter plot. We generate some random data using the NumPy library and pass it to the scatter() method. We also use the c parameter to specify the colors of the data points, the s parameter to specify the sizes of the data points, and the alpha parameter to specify the transparency of the data points.

Summary

In this tutorial, you learned how to create and customize different types of plots using Matplotlib in Python. You also learned how to install Matplotlib, import it in your Python script, and create a simple line plot and a scatter plot. Matplotlib is a powerful plotting library that offers a wide range of customization options for creating high-quality plots.

Other Matplotlib Tutorials you may like