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()
Related
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.
Interactive matplotlib plotting is already a thing, but does not work properly in Pycharm, when used within a jupyter notebook.
The %matplotlib notebook does not work (throws no error, but I get <IPython ... JavaScript object> instead of a plot. If I plot normally (also with or without plt.show()) I just get a png and cannot interact in any way (even if, e.g., sliders are visible).
I couldn't find any answers elsewhere to this exact problem. It might be working in the browser version of jupyter, but I would like to stick to using PyCharm.
Pycharm v 2017.3 Community Edition
You can try:
import matplotlib
matplotlib.use('Qt5Agg')
import matplotlib.pyplot as plt
Instead of importing only the peplos.
It's a trick I found on forum of Jetbrains
https://intellij-support.jetbrains.com/hc/en-us/community/posts/115000736584-SciView-in-PyCharm-2017-3-reduces-functionality-of-Matplotlib It works for me. With this, you skip actually the Sciview and plot in a normal matplotlib window
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?.
I am using
%matplotlib inline
to display plots inside the notebook. I would like to disable this for several cells. So, I try
%matplotlib qt
This outputs the following error:
ImportError: Matplotlib qt-based backends require an external PyQt4, PyQt5,
or PySide package to be installed, but it was not found.
I am not sure how to solve this, as everything is up to date.
How can I solve the above?
Is there another way to disable %matplotlib inline in a certain cell without restarting the entire kernel?
You might be able to use plt.switch_backend, although as the documentation states, this is an experimental feature. The following works for me, using matplotlib 1.5 and an IPython 4.0.1:
In [1]: from matplotlib import pyplot as plt
In [2]: import numpy as np
# plot appears inline (default)
In [3]:plt.plot(np.random.randn(10))
Out[3]:[<matplotlib.lines.Line2D at 0x7fac4408e390>]
In [4]: plt.switch_backend('QtAgg4')
# plot appears inside a separate Qt4 window
In [5]:plt.plot(np.random.randn(10))
Out[5]:[<matplotlib.lines.Line2D at 0x7fac3b408a90>]
You might need to change 'QtAgg4' according to whichever version of PyQt you have installed - this could be the cause of the error you mentioned in the question. Another interactive backend that should work on Mac would be 'CocoaAgg'. If the images are very large you could also use the 'Agg' backend to suppress plotting altogether, and instead save the resulting figure(s) straight to disk.
If you don't have a specific backend installed use "agg":
import matplotlib
matplotlib.use('agg')
import matplotlib.pyplot as plt
Reference: https://github.com/matplotlib/matplotlib/issues/9017
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