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.
First, you need to install Matplotlib if you don't have it already. You can install it using pip:
pip install 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.
The simplest plot you can create is a line plot, which shows a series of points connected by a line.
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:
plt.plot(x, y)
creates a line plot.