Matplotlib
Matplotlib is a Python library used to create charts and visualizations. It helps us turn numbers and data into pictures that are much easier to understand.
Matplotlib turns data into visual information.
Instead of looking at hundreds of numbers in a DataFrame, we can create a chart and quickly see trends, comparisons, and relationships.
What Is Matplotlib?
Matplotlib is one of the most commonly used Python libraries for data visualization.
It allows you to create different types of charts such as line charts, bar charts, histograms, and scatter plots.
Without visualization
January = 1200 February = 1500 March = 1800 April = 1600
You can understand the numbers, but you have to read each value.
With visualization
January → 1200
February → 1500
March → 1800
April → 1600
●
● ●
●
----------------
Jan Feb Mar Apr
The trend becomes much easier to see.
Install Matplotlib
If Matplotlib is not already installed, install it using pip.
pip install matplotlib
If you are working inside a virtual environment, make sure the environment is activated before installing it.
Import Matplotlib
The plotting functionality is commonly imported from
matplotlib.pyplot.
import matplotlib.pyplot as plt
The plt name is simply a short name that
makes the code easier to write.
Create Your First Chart
Let's create a simple line chart.
import matplotlib.pyplot as plt x = [1, 2, 3, 4, 5] y = [10, 20, 15, 30, 25] plt.plot(x, y) plt.show()
The x values represent the horizontal axis
and the y values represent the vertical
axis.
x → [1, 2, 3, 4, 5] y → [10, 20, 15, 30, 25] Matplotlib connects the points and displays them as a line chart.
Understand plt.plot()
The basic structure is:
plt.plot(x, y)
Think about it like this:
plt.plot(
horizontal_values,
vertical_values
)
Matplotlib takes matching values from both lists and creates points on the chart.
x = [1, 2, 3] y = [10, 20, 15] Points: (1, 10) (2, 20) (3, 15)
It then connects those points with a line.
Add a Title and Axis Labels
A chart should explain what the viewer is looking at. Add a title and labels to make it understandable.
import matplotlib.pyplot as plt
months = [
"January",
"February",
"March",
"April"
]
sales = [
1200,
1500,
1800,
1600
]
plt.plot(months, sales)
plt.title("Monthly Sales")
plt.xlabel("Month")
plt.ylabel("Sales")
plt.show()
Now the chart communicates three important things:
Title → Monthly Sales X-axis → Month Y-axis → Sales
Add Markers
Markers make individual data points easier to see.
import matplotlib.pyplot as plt
months = [
"January",
"February",
"March",
"April"
]
sales = [
1200,
1500,
1800,
1600
]
plt.plot(
months,
sales,
marker="o"
)
plt.title("Monthly Sales")
plt.xlabel("Month")
plt.ylabel("Sales")
plt.show()
marker="o" tells Matplotlib to display a
circular marker at each data point.
Add a Grid
A grid can make it easier to read values from a chart.
plt.plot(
months,
sales,
marker="o"
)
plt.title("Monthly Sales")
plt.xlabel("Month")
plt.ylabel("Sales")
plt.grid(True)
plt.show()
The important function here is:
plt.grid(True)
Save a Chart
You don't always want to display a chart only on the screen. You can save it as an image.
plt.plot(months, sales)
plt.title("Monthly Sales")
plt.savefig("monthly-sales.png")
plt.show()
savefig() saves the current figure to a
file.
Complete Example
Let's put the basic concepts together.
import matplotlib.pyplot as plt
months = [
"January",
"February",
"March",
"April",
"May"
]
sales = [
1200,
1500,
1800,
1600,
2100
]
plt.plot(
months,
sales,
marker="o"
)
plt.title("Monthly Sales")
plt.xlabel("Month")
plt.ylabel("Sales")
plt.grid(True)
plt.show()
The program performs these steps:
1. Import Matplotlib
↓
2. Create the data
↓
3. Create the plot
↓
4. Add title
↓
5. Add axis labels
↓
6. Add grid
↓
7. Display chart
Simple Mental Model
Don't try to memorize every Matplotlib function. Understand the basic workflow first.
Data ↓ plt.plot() ↓ Title ↓ Labels ↓ Grid / Markers ↓ plt.show()
Once you understand this workflow, the other chart types become much easier because they use the same basic Matplotlib idea.
Use Matplotlib With Pandas
This is where Matplotlib becomes especially useful in your Python for AI journey.
Pandas can prepare the data, and Matplotlib can visualize it.
import pandas as pd
import matplotlib.pyplot as plt
sales = pd.DataFrame({
"Month": [
"January",
"February",
"March",
"April"
],
"Sales": [
1200,
1500,
1800,
1600
]
})
plt.plot(
sales["Month"],
sales["Sales"],
marker="o"
)
plt.title("Monthly Sales")
plt.xlabel("Month")
plt.ylabel("Sales")
plt.grid(True)
plt.show()
Here Pandas stores and manages the data, while Matplotlib creates the visualization.
Pandas ↓ Prepare / organize data ↓ Matplotlib ↓ Visualize data
Why Visualization Matters in AI
Visualization isn't just about making data look nice. It helps you discover patterns that are difficult to notice in raw numbers.
Example 1 — Finding a trend
January → 100 February → 120 March → 150 April → 180 May → 220
A line chart makes the upward trend immediately obvious.
Example 2 — Finding unusual data
100 105 110 108 107 500 109
A visualization can make the unusual value 500 stand out immediately. This could indicate an outlier or a data problem.
Matplotlib turns data into charts that humans can understand quickly.
The basic workflow is simple: create your data, use a plotting function, add a title and labels, customize the chart when necessary, and display or save it. In the next topics, you will use this foundation to create Line Charts, Bar Charts, Histograms, and Scatter Plots.