Matplotlib stopped working after upgrade to 1.1.0 - python

I'm trying to run matplotlib 1.1.0 under Ubuntu 11.10. I previously had matplotlib 1.0.1 installed and working, but I needed some features from version 1.1.0 (and was becoming quite annoyed that all the documentation was for another version than mine) so I decided to upgrade.
Now, I can't show any plots.
I have uninstalled everything I had from the package repository, and installed version 1.1.0 from source. I've read through the installation FAQ but the hints there did not help. I get the following output from their suggested troubleshooting procedure, but no plot:
$HOME=/home/tomas
CONFIGDIR=/home/tomas/.matplotlib
matplotlib data path /usr/local/lib/python2.7/dist-packages/matplotlib/mpl-data
loaded rc file /usr/local/lib/python2.7/dist-packages/matplotlib/mpl-data/matplotlibrc
matplotlib version 1.1.0
verbose.level helpful
interactive is False
platform is linux2
Using fontManager instance from /home/tomas/.matplotlib/fontList.cache
backend agg version v2.2
What do I need to do to get this working?
Update:
After following up on some troubleshooting tips in comments, I can report that it's the actual showing of plots that doesn't work.
Running the following script outputs a png with the expected plot, but does not show any plotting window.
from matplotlib import pyplot as plt
plt.plot([1, 2, 3])
plt.savefig('testfig.png')
plt.show()

When you import pyplot, it has to set a backend, and it may be setting it to a non-interactive one, which would explain the behavior you are seeing. See what backend is being used by running plt.get_backend()
To set the backend, you have to run these commands before importing pyplot:
import matplotlib
matplotlib.use(your_backend)
where
your_backend in set(["FLTKAgg", "GTK", "GTKAgg", "GTKCairo", "macosx",
"QTAgg", "QT4Agg", "TkAgg", "WX", "WXAgg"])
To make this a permanent setting, put the backend name (without quotes) in your ~/.matplotlib/matplotlibrc file, like this example:
backend : WXAgg

Related

How to change the default backend in matplotlib from 'QtAgg' to 'Qt5Agg' in Pycharm?

Qt5Agg is necessary to use the mayavi 3D visualization package. I have installed PyQt5 and mayavi using pip in a separate copied conda environment. The default backend then changes from TkAgg to QtAgg. This is a bit weird because in an earlier installation in a different PC the default changed directly to Qt5Agg. I always check the backend using the following commands from the python console :
import matplotlib
matplotlib.get_backend()
Even with the backend being 'QtAgg', I am able to use mayavi from the terminal without any issue but not when I do so in Pycharm. Here I get a non-responsive empty window (image below) :
Image of the non-responsive window
I have been able to get rid of this issue by explicitly using Qt5Agg instead of QtAgg before the plt call :
import matplotlib
matplotlib.use('Qt5Agg')
import matplotlib.pyplot as plt
But I would prefer a better way than using the above in every script that I write. As I had mentioned earlier, I already have mayavi installed and have used it successfully it in Pycharm in a different PC and there the default backend is 'Qt5Agg' and hence there is no need to change the backend explicitly.
Is there anything obvious that I'm overlooking ? Can you please let me know of a way to change the default backend for matplotlib from QtAgg to Qt5Agg after PyQt5 installation using pip ?
Thanks in advance !!
Thanks to #PaulH's comment, I was able to solve the issue. Owing to #mx0's suggestion, I shall now explicitly mention the fix below so that others can also benefit from it.
In a particular conda environment, if matplotlib package is installed, then there will be a 'matplotlibrc' file stored somewhere that defines what the default backend will be whenever matplotlib is imported from that conda environment. The location of this 'matplotlibrc' can be found using the following commands :
import matplotlib
matplotlib.matplotlib_fname()
Please look into the following link if there's any deprecation issue with the above commands :
https://matplotlib.org/stable/tutorials/introductory/customizing.html#customizing-with-matplotlibrc-files
Once the location of the 'matplotlibrc' file is known, open it and simply uncomment one line inside this file. Just change the backend from :
##backend: Agg
to :
backend: Qt5Agg
And that's it. All the plot window troubles in PyCharm will be solved as far as the mayavi 3D visualization package is concerned. For any other use, where a specific backend is necessary, you can also set the default to any other backend of choice.

matplotlib pyplot UserWarning: Matplotlib is currently using agg, which is a non-GUI backend, so cannot show the figure

I have some problems related to plt.show() and I would describe the problem and what I tried.
#Problem
I am using python and plt.show() and those worked well until 4 hours before.
However, when I do plot something, suddenly
directory/plots/distance.py:119: UserWarning: Matplotlib is currently using agg, which is a non-GUI backend, so cannot show the figure.
plt.show()
showed at the bottom of terminal.
#Trial 1
I searched google and add import matplotlib, matplotlib.use('TkAgg'), import matplotlib.pyplot as plt for obspy-plot Data.seed.
This worked for my coworker who shared server and had the same problem with me.
But it did not work for me but showed site-packages/matplotlib/pyplot.py", line 275, in switch_backend raise ImportError( ImportError: Cannot load backend 'TkAgg' which requires the 'tk' interactive framework, as 'headless' is currently running.
So I changed 'TkAgg' to 'Agg' but there was no error but also no plots.
#Trial2
I try to conda install -c conda-forge python3-tk and conda install -c anaconda tk (I cannot use pip or sudo).
But nothing changed related to the 'TkAgg' error.
#Trial3
I add import tkinter before 'TkAgg'part but nothing changed.
#So..
I think the matplotlib in my anaconda has something wrong but have no idea how to fix it.
(I still cannot understand why this problem occurs suddenly..)
Could you please let me know how to solve this problem?

Figure not displayed while using matplotlib even when backend environment is correct

I have very similar problem as here. However, my default backend environment is correct for both python 2.7 as well as 3.6.
import matplotlib
matplotlib.get_backend()
...gives me TkAgg and GTK3Agg respectively.
I haven't used python on this machine for months so I do not know exactly when this behaviour changed. I have used matplotlib before on this machine and it worked just fine.
Any suggestions to solve this?

how to copy matplotlib figures to clipboard

After plotting the figure, I want to copy it to the clipbaoard instead of saving to figures. I searched the internet and the code does not work, either the required modules only support python 2.x or the program stopped by not well-defined functions.
I am using Python 3.6 in Windows 10. My matplotlib version is 2.0.2.
I tried 'PyGTK: copy matplotlib figure to clipboard'
and got the error: ImportError: Gtk* backend requires pygtk to be installed. I then visited www.pygtk.org/downloads.html to download pygtk/PyGObject (Windows) but it requires python 2.6 or file not found.
Can anyone show how to achieve it?
import matplotlib.pyplot as plt
a = [2,3,6,7,1]
plt.plot(a)
plt.show()

Using matplotlib *without* TCL

Exactly what the title says. Is there a way to use the matplotlib library without installing TCL? Please don't tell me to bite the bullet and install TCL - I know how to do it but for my own (ok maybe silly) reasons I don't want to.
I don't care about displaying the plots, I only want to be able to output them in a png. I tried various things (using different backends etc) but matplotlib always wanted to find tcl to work :( Why is TCL so essential for matplotlib?
Also, please notice that I am using windows -- I have installed everything that could be required (numpy, pandas, matplotlib) using pip.
#gerrit's solution is the correct one (I was trying to change the backends but I was doing it after loading pyplot -- the important thing seems to be that you need to change the backend immediately after imporing matplotlib). Here's a small example using it:
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt
fig, ax = plt.subplots( nrows=1, ncols=1 )
ax.plot([0,1,2], [10,20,3])
fig.savefig('foo.png')
plt.close(fig)
This will output a file named 'foo.png' without using TCL \o/
Matplotlib 3.0 and newer
(Added to answer in October 2018)
Starting with Matplotlib 3, released on 19 September 2018, the problem described in the question should not occur. From the what's new part of the documentation:
The default backend no longer must be set as part of the build process. Instead, at run time, the builtin backends are tried in sequence until one of them imports.
Headless linux servers (identified by the DISPLAY env not being defined) will not select a GUI backend.
So, as long as you make sure DISPLAY is not defined, you should not run into any problems with the backend when running in a script on a headless Linux server.
Matplotlib 2.2 and older
(Original answer May 2016)
Immediately after loading matplotlib, enter
matplotlib.use('Agg')
Do this before loading pyplot, if at all.
By default, Matplotlib uses the TkAgg backend, which requires Tcl. If you don't want to display the plots, Agg is fine. Other alternatives include WX and QTAgg, but both require the installation of additional libraries.
Alternately, you can set this directive in your matplotlibrc file:
backend : Agg
For details, see the Matplotlib Usage FAQ on What is a backend?.

Categories