Read our blogs, tips and tutorials
Try our exercises or test your skills
Watch our tutorial videos or shorts
Take a self-paced course
Read our recent newsletters
License our courseware
Book expert consultancy
Buy our publications
Get help in using our site
471 attributed reviews in the last 3 years
Refreshingly small course sizes
Outstandingly good courseware
Whizzy online classrooms
Wise Owl trainers only (no freelancers)
Almost no cancellations
We have genuine integrity
We invoice after training
Review 30+ years of Wise Owl
View our top 100 clients
Search our website
We also send out useful tips in a monthly email newsletter ...
An introduction to using matplotlib in Python to create charts Part five of a six-part series of blogs |
---|
If you want to create charts in Python, the chances are that you'll do it using the matplotlib module. This blog will get you started and explain some of its foibles! |
In this blog
To understand fully about saving matplotlib charts you'll need to learn all about backends, but fortunately most people will be able to get away with two simple functions: savefig and close.
You can save a chart using the savefig function:
import numpy as np
import matplotlib.pyplot as plt
# get the number of days of Xmas ( 1 to 12)
days_till_xmas = np.arange(1,13)
# panic levels increase exponentially
panic_level = days_till_xmas ** 2
# chart panic level against days till Xmas
plt.plot(days_till_xmas,panic_level)
# add a title
plt.title('Panic levels over the 12 days of Christmas')
# label the axes
plt.ylabel("Panic level")
plt.xlabel("Day number")
# save this chart as a file
plt.savefig(
"c:/wiseowl/Chart example.webp",
bbox_inches='tight'
)
# show the chart (you must save it first)
plt.show()
This code would produce a webp format image file, shown below in Visual Studio Code:
To produce a different type of image (such as PNG, for example), just use a different file extension in your code.
Notice that if you are showing your chart you need to save it first! This is because when you show a chart on screen you will then have to close your chart window before your code can resume and this action will dispose of the chart that you are intending to save.
You don't have to display a chart on screen to create an image of it, as this sample code shows:
# save this chart as a file
plt.savefig(
"c:/wiseowl/Chart example.webp",
bbox_inches='tight'
)
# close this chart
plt.close(1)
This would save the chart in question and then close it. Notice that (with wild inconsistency) Python numbers your charts from 1, not from 0. You could close all of your open charts like this:
# close all open charts
plt.close('all')
It's probably easier to create charts one at a time, though!
Parts of this blog |
---|
Some other pages relevant to the above blogs include:
Kingsmoor House
Railway Street
GLOSSOP
SK13 2AA
Landmark Offices
99 Bishopsgate
LONDON
EC2M 3XD
Holiday Inn
25 Aytoun Street
MANCHESTER
M1 3AE
© Wise Owl Business Solutions Ltd 2025. All Rights Reserved.