Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users
Tabletop RPGs
Tabletop RPGs
Community Proposals
Community Proposals
tag:snake search within a tag
answers:0 unanswered questions
user:xxxx search by author id
score:0.5 posts with 0.5+ score
"snake oil" exact phrase
votes:4 posts with 4+ votes
created:<1w created < 1 week ago
post_type:xxxx type of post
Search help
Notifications
Mark all as read See all your notifications »
Q&A

Welcome to Software Development on Codidact!

Will you help us build our independent community of developers helping developers? We're small and trying to grow. We welcome questions about all aspects of software development, from design to code to QA and more. Got questions? Got answers? Got code you'd like someone to review? Please join us.

Post History

50%
+0 −0
Q&A Why won't Matplotlib show me a plot?

Matplotlib uses a backend to render the plot. Some backends are "GUI backends", meaning that they can render into a window that displays on your screen. Others are "non-GUI backends" which can only...

posted 6mo ago by Karl Knechtel‭

Answer
#1: Initial revision by user avatar Karl Knechtel‭ · 2024-06-18T11:59:36Z (6 months ago)
Matplotlib uses a *backend* to render the plot. Some backends are "GUI backends", meaning that they can render into a window that displays on your screen. Others are "non-GUI backends" which can only save the plot into an image file.

Matplotlib doesn't come with any GUI backends except for `TkAgg`, which uses Tkinter to display the window. But if [tkinter is missing](https://software.codidact.com/posts/291791) then this won't work. In these cases, Matplotlib will pre-configure itself to use a plain, non-GUI backend just called `agg`.

You can tell Matplotlib which backend to use by calling `matplotlib.use` before importing `matplotlib.pyplot`:

```
import matplotlib
matplotlib.use('TkAgg') # for the Tkinter backend, for example
import matplotlib.pyplot as plt
```

However, this will fail with an `ImportError` or `ModuleNotFoundError` if the named backend isn't available.

If you don't actually need to show the plot on screen, the `agg` backend works just fine: use `.savefig()` (providing a filename) instead of `.show()`. You [can also](https://matplotlib.org/stable/install/index.html) switch to the `pdf`, `ps` or `svg` backends to write files in those formats, instead of an image file.

Support for many backends can be installed via Pip - for example, `pip install pyqt5` will enable the `Qt5Agg` backend.

<section class="notice is-danger">

However, Tkinter [is special because it's supposed to be part of the standard library](https://software.codidact.com/posts/291791). **Do not try to use Pip to install Tkinter support; it will not work and you will install something that may be useless or even harmful.**
</section>

See also the [Matplotlib documentation for optional dependencies](https://matplotlib.org/stable/install/dependencies.html#optional).