How to Plot in Python

What you’ll build or solve

You’ll create a clear plot in Python using Matplotlib, then add labels, plot multiple lines, and try common chart types like bar and scatter.

When this approach works best

This approach works best when you need to:

  • Visualize a small set of numbers, like trends over time.
  • Make a quick chart for a report, school, or a script.
  • Save a chart as a PNG to share or embed.

Avoid this approach when:

  • You need interactive dashboards. Plotly or a web app may fit better.
  • You are plotting huge datasets. You may need downsampling or a different tool.

Prerequisites

  • Python 3 installed
  • Matplotlib installed (python -m pip install matplotlib)
  • You know what a list is
  • You can run a Python script

Step-by-step instructions

1) Make a basic line plot

Start with matplotlib.pyplot and plt.plot().

import matplotlib.pyplot as plt

x = [1, 2, 3, 4]
y = [1, 4, 9, 16]

plt.plot(x, y)
plt.show()

What to look for:

In a normal script, the plot window appears only after plt.show().


2) Add a title and axis labels

Labels make plots readable.

import matplotlib.pyplot as plt

x = [1, 2, 3, 4]
y = [1, 4, 9, 16]

plt.plot(x, y)
plt.title("Squares")
plt.xlabel("x")
plt.ylabel("x squared")
plt.show()

3) Plot multiple lines

Call plot() more than once and add a legend.

import matplotlib.pyplot as plt

x = [1, 2, 3, 4]
squares = [1, 4, 9, 16]
cubes = [1, 8, 27, 64]

plt.plot(x, squares, label="squares")
plt.plot(x, cubes, label="cubes")
plt.legend()
plt.show()

4) Try common chart types

Use a bar chart for category counts and a scatter plot for paired data.

Bar chart

import matplotlib.pyplot as plt

labels = ["Coffee", "Tea", "Water"]
counts = [3, 2, 6]

plt.bar(labels, counts)
plt.title("Drinks Today")
plt.xlabel("Drink")
plt.ylabel("Count")
plt.show()

Scatter plot

import matplotlib.pyplot as plt

hours = [1, 2, 3, 4, 5]
scores = [55, 63, 68, 74, 80]

plt.scatter(hours, scores)
plt.title("Study Hours vs Score")
plt.xlabel("Hours")
plt.ylabel("Score")
plt.show()

5) Change the look with markers and line settings

Markers help when you have few points.

import matplotlib.pyplot as plt

x = [1, 2, 3, 4]
y = [1, 4, 9, 16]

plt.plot(x, y, marker="o", linewidth=2)
plt.show()

6) Save a plot to a file

Saving helps when you do not want a pop-up window or when you run on a server.

import matplotlib.pyplot as plt

x = [1, 2, 3, 4]
y = [1, 4, 9, 16]

plt.plot(x, y)
plt.title("Squares")
plt.savefig("squares.png")

What to look for:

The image saves to your current folder. If you do not see it, check which folder you ran the script from.


Examples you can copy

1) Plot daily steps (line plot)

import matplotlib.pyplot as plt

days = ["Mon", "Tue", "Wed", "Thu", "Fri"]
steps = [6200, 7100, 5600, 8300, 7900]

plt.plot(days, steps, marker="o")
plt.title("Daily Steps")
plt.xlabel("Day")
plt.ylabel("Steps")
plt.show()

2) Compare categories (bar chart)

import matplotlib.pyplot as plt

labels = ["Beginner", "Intermediate", "Advanced"]
counts = [12, 7, 3]

plt.bar(labels, counts)
plt.title("Learners by Level")
plt.xlabel("Level")
plt.ylabel("Count")
plt.show()

3) Spot a relationship (scatter plot)

import matplotlib.pyplot as plt

sleep_hours = [5, 6, 7, 8, 9]
focus_score = [40, 55, 65, 78, 82]

plt.scatter(sleep_hours, focus_score)
plt.title("Sleep vs Focus")
plt.xlabel("Hours of sleep")
plt.ylabel("Focus score")
plt.show()

Common mistakes and how to fix them

Mistake 1: Getting ModuleNotFoundError: No module named 'matplotlib'

Why it happens:

Matplotlib is not installed in the Python you are running.

Fix:

python -m pip install matplotlib

Mistake 2: No window appears when running a script

You might forget plt.show().

Fix:

import matplotlib.pyplot as plt

plt.plot([1, 2, 3], [1, 4, 9])
plt.show()

Mistake 3: ValueError because x and y lengths do not match

You might write:

x = [1, 2, 3]
y = [1, 4, 9, 16]
plt.plot(x, y)

Why it breaks:

Matplotlib needs one y-value for each x-value.

Fix:

x = [1, 2, 3, 4]
y = [1, 4, 9, 16]
plt.plot(x, y)
plt.show()

Troubleshooting

If you see ModuleNotFoundError, install Matplotlib with python -m pip install matplotlib, then run your script again.

If nothing appears on screen, add plt.show() or save the plot with plt.savefig("plot.png").

If you get a backend or display error on a server, save the plot to a file instead of opening a window.

If plots pile up while experimenting in an interactive session, restart Python or run your code as a fresh script.


Quick recap

  • Import Matplotlib: import matplotlib.pyplot as plt
  • Make a line plot: plt.plot(x, y)
  • Add labels: plt.title(), plt.xlabel(), plt.ylabel(), and plt.legend()
  • Try other charts: plt.bar(labels, values) and plt.scatter(x, y)
  • Show with plt.show() or save with plt.savefig("file.png")