← Back to Python Course | Chapter 13: Data Science & Web | Lesson 5 of 14

Python Matplotlib Basics

Matplotlib draws charts and graphs from your numbers, like turning a list of scores into a picture. You can add titles, labels, and colors.
Syntax
python
import matplotlib.pyplot as plt

plt.plot(x_values, y_values)
plt.title("title")
plt.show()

What is Matplotlib?

Matplotlib is Python's foundational 2D plotting library, and its pyplot module offers a MATLAB-inspired interface for building charts step by step.

This tutorial's examples use the non-interactive Agg backend, which renders directly to image files rather than opening a graphical window, so the code runs safely without a display attached.

Example: What is Matplotlib?

python
import matplotlib
matplotlib.use("Agg")  # non-interactive backend, renders to a file instead of a window
import matplotlib.pyplot as plt

plt.plot([1, 2, 3], [4, 5, 6])  # x values, y values
plt.savefig("chart.png")
print("Chart saved")

Customizing Labels and Titles

plt.title(), plt.xlabel(), and plt.ylabel() add descriptive text around a plot, and plt.legend() adds a key identifying each line or series when a chart shows more than one.

A chart without these is often unreadable to anyone besides the person who made it, since the axes and series carry no context on their own.

Example: Customizing Labels and Titles

python
import matplotlib
matplotlib.use("Agg")
import matplotlib.pyplot as plt

plt.plot([1, 2, 3], label="Sales")
plt.title("Monthly Sales")  # chart title
plt.xlabel("Month")  # x-axis label
plt.ylabel("Amount")  # y-axis label
plt.legend()  # shows the "Sales" label as a key
plt.savefig("chart.png")

Line Customization

The color, linewidth, and linestyle arguments to plt.plot() control a line's visual appearance -- letting you distinguish multiple series on the same chart by more than just their shape, which matters especially once a plot has three or more overlapping lines.

Example: Line Customization

python
import matplotlib
matplotlib.use("Agg")
import matplotlib.pyplot as plt

plt.plot([1, 2, 3], color="red", linewidth=2, linestyle="--")  # customizes the line's appearance
plt.savefig("chart.png")
print("Styled line saved")

Creating Gridlines

plt.grid(True) overlays light reference lines across the plotting area, making it much easier for a viewer to read off approximate values at a glance rather than having to visually interpolate between bare axis tick marks.

Example: Creating Gridlines

python
import matplotlib
matplotlib.use("Agg")
import matplotlib.pyplot as plt

plt.plot([1, 2, 3])
plt.grid(True)  # overlays reference lines for easier reading
plt.savefig("chart.png")

Saving Figures

plt.savefig('filename.png') writes the current figure to disk as an image file, supporting formats like PNG, PDF, and SVG based on the file extension you give it.

This is how generated charts get embedded into reports, dashboards, or web pages instead of only appearing in an interactive window.

Example: Saving Figures

python
import matplotlib
matplotlib.use("Agg")
import matplotlib.pyplot as plt

plt.plot([1, 2, 3])
plt.savefig("chart.png")  # writes the current figure to disk as an image
print("Saved as PNG")
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Forgetting plt.show(), so no window appears in a plain script.
  2. Calling plt.savefig() after plt.show(), which saves a blank figure.
  3. Forgetting axis labels and a title, so the chart cannot be read without explanation.

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.