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?.
Related
I just started using Codespaces. In my python file I have this code:
import matplotlib.pyplot as plt
import pandas as pd
print("Hello")
titanic_data = pd.read_csv("https://raw.githubusercontent.com/datasciencedojo/datasets/master/titanic.csv")
titanic_data = titanic_data[titanic_data['Age'].notnull()]
titanic_data['Fare'] = titanic_data['Fare'].fillna(titanic_data['Fare'].mean())
titanic_data = titanic_data.drop_duplicates()
plt.scatter(titanic_data['Age'], titanic_data['Fare'])
plt.show()
print("Goodbye")
When I run this on my local machine, this works perfectly. I can see the console logs, and the figure appears as a new window:
However, when I run this in Codespaces, I can see all of the code running without any errors, but it does not show the figure. Is this a known limitation or a feature that is not yet supported? Is there another way I can plot figures in Codespaces?
They mention this in the docs docs:
The default container image that's used by GitHub Codespaces includes a set of machine learning libraries that are preinstalled in your codespace. For example, Numpy, pandas, SciPy, Matplotlib, seaborn, scikit-learn, Keras, PyTorch, Requests, and Plotly.
It sounds like it should be supported out of the box. Is additional configuration required?
Based on the experimentation I have done thus far, plotting these diagrams as one would do in a local dev environment is not (yet?) possible.
For this specific case, the next best solution was to create a new GitHub Codespace from this repo: https://github.com/education/codespaces-teaching-template-py
Once the repo has been cloned into the Codespace, navigate to an existing .ipynb file or create your own.
Inside there you'll be able to run chunks of custom code and plot figures.
The big limitation I see is that the figure cannot be interacted with the same way that one would be able to on a local machine (zooming, panning, etc).
As always, don't forget to shut your Codespace down when you're done using it!
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.
Is it possible to use any other graphing library in SPSS that the built in? I just discovered the python extensions that makes SPSS great.
import matplotlib.pyplot as plt
from numpy.random import rand
fig, ax = plt.subplots()
for color in ['red', 'green', 'blue']:
n = 750
x, y = rand(2, n)
scale = 200.0 * rand(n)
ax.scatter(x, y, c=color, s=scale, label=color,
alpha=0.3, edgecolors='none')
ax.legend()
ax.grid(True)
plt.show()
This will create a simple scatter plot and it works fine in any IDE, but when trying to use that code in SPSS BEGIN PROGRAM END PROGRAM i get the following error:
RuntimeError: Python is not installed as a framework. The Mac OS X backend will not be able to function correctly if Python is not installed as a framework. See the Python documentation for more information on installing Python as a framework on Mac OS X.
Please either reinstall Python as a framework, or try one of the other backends. If you are Working with Matplotlib in a virtual enviroment see 'Working with Matplotlib in Virtual environments' in the Matplotlib FAQ
Maybe I am asking too much out of the python extension in SPSS but it would be nice to use another graph library than the one they have built in.
Adding more information as another answer as the comment field is too limited.
I do not get the framework error on Windows, which is a different issue, I think. But running your code inside Statistics works - sort of. Instead of placing the image in the Viewer, it pops up in its own window (which may be buried behind another window).
So Statistics doesn't know about it and patiently waits for the program to complete, which doesn't happen until you dismiss that window (which does have a normal frame not shown in the graphic).
To make this work, you would need to direct the matplotlib code to write the image to a file somewhere and then use SpssClient apis to insert that image in the Viewer. See CreateImageChartItem Method (Python) in the Python programmability help. Alternatively, if you can direct matplotlib to write the image to the standard output stream, Statistics might be able to capture it directly in the Viewer.
I usually do programmability images with R code (even though Python is a way better language(!)), where this all works seamlessly. Or I use Python code to write Statistics graphics commands and GPL to have the Statistics engine, which is pretty powerful, do the charting.
Another thing you mind find helpful if you are into Python, is that you can run Python in external mode, where you start with Python code running from your IDE or a Python command line and then have it invoke Statistics by running
import spss
This has great advantages for developing and debugging Python code, but you can't use the SpssClient module methods directly. I ran your code from my IDE (Wing Professional), and the image window popped right up. And, of course, you can use the spss module and related apis in external mode to communicate with Statistics and control it.
You can use just about any Python code or library, but you need to do some configuring. When the Python support (Python Essentials) is installed, it installs a private, unregistered Python system in order not to conflict with any other Python that might be installed. So if you try to add other libraries, the installer doesn't know what to do or installs them somewhere that the Statistics installation won't know about.
The easiest way to get around this is to install another, standard Python installation (version 2.7 in recent versions or 3.4 with version 24 if you need Python 3). Then go to Edit > Options > Files and point to that distribution. You will need to restart Statistics for that to take effect. I use the Anaconda distribution, which includes a lot of other goodies.
I'm not a Mac user, but something like this should work.
matplotlib has a config file and IPython has its own. Which one has precedence when it comes to setting things like matplotlib backends?
For example, say my config file for matplotlib says to use a specific backend, but then I modify my IPython startup or config files to use a different one. Which one would be used when I start IPython and import matplotlib?
More generally, what is the right way to set things up so that different profiles use different matplotlib backends or matplotlib configurations?
IPython configuration is used, as IPython itself chooses the matplotlib backend.
For reference, see IPython:core/pylabtools.py:activate_matplotlib and notice how matplotlib.use(backend) is called explicitly.
I updated my python distribution yesterday to EPD 7.3-2 (64-bit). I am working on a mac with snow leopard.
Now the plot device of matplotlib is broken in at least two ways:
the "save" button doesn't work and makes the terminal or ipython crash and
the only way to see the figure is to have it in front of you, there is no python figure icon in the dock.
I did my homework and these same problems were reported here and here.
I tried to follow the instructions to fix this given in here, but this is the error that I get:
$python install_pythonw.py `which python`/../..
/Library/Frameworks/EPD64.framework/Versions/Current/.Python does not exist; exiting.
Indeed, I looked at the given folder and I could not find a .Python file. I added a comment at the answer to this problem but so far no one has replied to it :( :(
Any idea of how to fix this?
thanks!
I have seen this problem a few times, and it seems to be a problem in some backends. Also, it doesn't seem normal that your session crashes after 4 or 5 plots. In particular, the MacOSX backend seems buggy.
As you installed the EPD, I think it's less likely that your installation is broken.
The solution seems to be using a different backend. You can try with ipython --pylab a few backends, try their features and see if the save button works. You can try the following:
ipython --pylab=wx
ipython --pylab=tk
ipython --pylab=osx
The last one is the option that you're probably using right now, so perhaps not the best. If you just call ipython --pylab, it will use the default backend from your ~/.matplotlib/matplotlibrc file. Once you find a working backend you can change the default by editing that file. Look for a line like this:
backend : MacOSX
(your version may have a different backend.) Just change that setting to WXAgg, TkAgg, or Qt4Agg. With the --pylab option the names are slightly different, they don't have the Agg part. My favourite backend for OSX is the Qt4Agg backend, but I don't think it ships with EPD and the save button also doesn't work! But either WXAgg or TkAgg should work fine.
Other ways of changing the backend in a script are:
import matplotlib
matplotlib.use('WXAgg')
or
matplotlib.rcParams['backend'] = 'WXAgg'