Matplotlib

W3Schools.com

Matplotlib is one of the most widely used Python libraries for creating static, animated, and interactive visualizations. It's highly customizable and allows you to create a wide variety of plots, including line plots, bar charts, histograms, scatter plots, and more.


1. Installing Matplotlib

First, you need to install Matplotlib if you don't have it already. You can install it using pip:

pip install matplotlib


2. Importing Matplotlib

To use Matplotlib in your Python code, you need to import it. The standard convention is to import matplotlib.pyplot as plt:

import matplotlib.pyplot as plt

This is because pyplot is a module within Matplotlib that provides a simple interface for plotting.


3. Basic Plotting

The simplest plot you can create is a line plot, which shows a series of points connected by a line.

Basic Line Plot

import matplotlib.pyplot as plt

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

# Plotting the line
plt.plot(x, y)

# Adding title and labels
plt.title("Simple Line Plot")
plt.xlabel("X values")
plt.ylabel("Y values")

# Show the plot
plt.show()

In this example: