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:
Learn Python on Mimo
- 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().
CSS
importmatplotlib.pyplotasplt
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.
importmatplotlib.pyplotasplt
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.
importmatplotlib.pyplotasplt
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
importmatplotlib.pyplotasplt
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
importmatplotlib.pyplotasplt
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.
CSS
importmatplotlib.pyplotasplt
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.
importmatplotlib.pyplotasplt
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)
importmatplotlib.pyplotasplt
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)
importmatplotlib.pyplotasplt
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)
importmatplotlib.pyplotasplt
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:
CSS
importmatplotlib.pyplotasplt
plt.plot([1,2,3], [1,4,9])
plt.show()
Mistake 3: ValueError because x and y lengths do not match
You might write:
CSS
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:
CSS
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(), andplt.legend() - Try other charts:
plt.bar(labels, values)andplt.scatter(x, y) - Show with
plt.show()or save withplt.savefig("file.png")
Join 35M+ people learning for free on Mimo
4.8 out of 5 across 1M+ reviews
Check us out on Apple AppStore, Google Play Store, and Trustpilot