"userwarning: Matplotlib is currently using agg, which is a non-GUI backend, so cannot show the figure." when plotting figure with pyplot on Pycharm
If you're getting the agg backend warning from Matplotlib, you need to set it to use a GUI backend instead. Add this to the top of your script:
Then simply proceed with your usual plotting. Stumbled upon TkAgg and can't find tkinter
around? Install it via apt-get install python3-tk
for Debian-based systems, or pacman -S tk
for the cool Arch folks. You'll need sudo powers for these commands to work. Don't use backend setting after loading matplotlib.pyplot
to avoid getting into a wrestling match with conflicting settings.
Setting up the backends: A step-by-step manual
Double-checking tkinter installation
To validate the installation of tkinter
, run this in your Python environment:
There's no pip
for tkinter
, so resort to good ol' system package managers to set it up, as explained above.
Touring the Mart's Alternate Backends
If TkAgg feels like eating the same meal every day, check out the alternative backends like Qt5Agg or GTKAgg. Install PyQt5 via a simple command:
Then call it with 'Qt5Agg' in the script:
Make sure you plot a test graph to ensure your backend setup didn't go south.
When GUIs play hide and seek: Saving plots
Cannot run GUI backends? No worries, you can save the plot to a file:
Feel free to view your masterpiece without the rigmarole of a GUI backend.
Final checks
Dive into Tkdocs or in-depth Python docs if you're up for some deep self-guided learning.
Mayday! Avoiding common calamities
Backend Conflicts: The Unwanted Guest
Changing backends after importing certain Matplotlib modules can cause a jam. Ensure you set the backend upfront before inviting matplotlib.pyplot
to kick off the show.
Virtual Environments: The Parallel Universe
Virtual environments carry their own package sets, completely oblivious to your system Python's setup. Make sure to check the local tkinter
packaging when using one.
Platform-Specific Twists and Twirls
Certain backends play better with specific platforms. macOSX
works like a charm for Apple wizards, but Windows and Linux might not share the sentiment.
Was this article helpful?