matplotlib graphs in SPSS - python

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.

Related

Matplotlib figures not generating in GitHub CodeSpaces

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!

Matplotlib image not coming up in Visual Studio Code on Mac

I'm running some basic code in the Visual Studio Code editor on MacOSX:
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(0, 20, 100)
plt.plot(x, np.sin(x))
plt.show()
...and can't seem to get the png/svg file image to come up after running this. This also doesn't stop executing and I have to manually terminate the process. However, if I run this directly in the Terminal (each line of code line for line) I get the resulting image. One work-around is to just save the file (plt.savefig('foo.png')). This seems to work - the image is saved in the specified file location. However, it would be good to just see the image come up after running the code.
When running matplotlib codes from the terminal, I experience the same kind of hanging of the application after saving the image to a file. In this case, one 'workaround' that has always worked for me is to turn off blocking. Basically alter your code in this way:
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(0, 20, 100)
plt.plot(x, np.sin(x))
plt.show(block=False)
input('press <ENTER> to continue')
It's not perfect, but the image is saved correctly and the application stops after you hit ENTER in the terminal. Hope this helps.
I am having a similar issue and I think it is a problem with the exact version of python that vs code is using to run the code.
For reference, I have vscode version 1.52.1 on a mac os Catalina. I installed python via anaconda and created a new environment for python 2.7 (tried with python 3.8 too). I open VSCode by calling code . from the folder I have my simple python codes saved in.
Like OP, I could reproduce the figure if I were to run the code from a python instance called from terminal but not from vscode.
MY SOLUTION:
From vscode, select as python interpreter the one found in /usr/bin/python not the one in ~/opt/anaconda3/env/python27/bin/python
But this is weird, because from a separate terminal window which python returns ~/opt/anaconda3/env/python27/bin/python. This suggests me (though I am no python expert) that there is an issue within vscode about linking the right interpreter with the libraries. Frankly being new to vscode, I find the need to pay attention to these details quite concerning.
I got the same problem, which was driving me nuts. The image was displayed when using Jupyter Notebooks, but not always when using VS Code. I just added one last line_ plt.show() - IMHO unnecessarily - but this worked well.
import matplotlib.pyplot as plt
import matplotlib.image as img
im = img.imread('myimage.png')
plt.imshow(im)
plt.show() # <- added this line and now the image shows every time!
I faced the same issue and here's what I did to solve it.
Since the code runs without any error but also does not generate any plot and needs to be terminated manually, it's difficult to figure out what's going on. I tried running python2.7 test.py
This works, plot is generated but python3 test.py does not work.
So, here's what you need to do -
Run, pip install matplotlib --upgrade to upgrade the matplotlib. This does not resolve the issue but now the error is printed.
"RuntimeError: Python is not installed as a framework" ......
So, finally, to solve the problem, refer to Working with Matplotlib on macOS
Since, I am using Anaconda, all I need to do is conda install python.app and then use pythonw to run all scripts. I hope you also find the solution to your particular case from the FAQ.
Overall, it's a Matplotlib problem, so upgrading (or reinstalling) and trying with different Python versions should get you going.

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?.

Importing mayavi from python creates pop-up with black background on UI (Mavericks/Canopy)

Image gallery: http://imgur.com/a/qZkTW#qGj7I0H
I just installed the new version of Canopy 1.3 from enthought. I opened up ipython, and I imported mayavi's mlab without issue. I then plotted a 3d sphere without issue using the following:
import mayavi
from mayavi import mlab
mlab.points3d(1,1,1)
mlab.show()
And I get what I would expect (See figure #2 in gallery). I can then open up the scene editor without issue (see figure #1 in gallery), but when I try to open any other traits editors for anything else, I get a weird black background with no text:
scalarscatter editor
This issue affects all other editors other than the scene editor. It has been reproduced after uninstalling canopy per the description on their website, restarting the computer and reinstalling canopy. It has persisted despite reinstallation with both 32- and 64-bit installations, and it also affects mayavi2 when run from the command line. I don't get this error when I open the Canopy.open an app and run everything from inside canopy, which is not really a viable option for my current workflow (I want to use ipython notebooks)
The only error I get via stderr seems to be unrelated:
Python[4434:d0f] CoreText performance note: Client called CTFontCreateWithName() using name ".Lucida Grande UI" and got font with PostScript name ".LucidaGrandeUI". For best performance, only use PostScript names when calling this API.
Python[4434:d0f] CoreText performance note: Set a breakpoint on CTFontLogSuboptimalRequest to debug.
I have updated all the canopy packages using the built-in installer. I'm using the built-in python for canopy. I never had any similar issues in the past with EPD, only since installing Canopy 1.3 on my computer.
I have searched the internet, and cannot find any other complaints of this issue. Please let me know if you have any ideas. I would really like to use the ipython notebook feature rather than opening Canopy.app every time.
Any help would be greatly appreciated!
Several notes:
1) This should do it:
ETS_TOOLKIT=qt4 ipython notebook --pylab qt
(These settings are default within the Canopy app).
2) Be sure that you are starting Canopy User Python from Terminal. sys.prefix in terminal should be the same as from within Canopy's (i)Python shell. For details, see https://support.enthought.com/entries/23646538-Make-Canopy-User-Python-be-your-default-Python
3) FWIW, IPython notebook is useable directly within Canopy (File / New / IPython Notebook), but admittedly the experience is still not as good as in a regular browser, especially on Mac. By Canopy 1.4 or 1.5 we hope that it will be, so you can have the best of both worlds.

How to generate a Cocoa-recognized plot using matplotlib in Python on OS X (Leopard preferably)

I'm not sure exactly what is going on under the hood, but here is my setup, example code, and problem:
setup:
snow leopard (10.6.8)
Python 2.7.2 (provide by EPD 7.1-2)
iPython 0.11 (provided by EPD 7.1-2)
matplotlib (provided by EPD 7.1-2)
example code:
import numpy as np
import pylab as pl
x=np.random.normal(size=(1000,))
pl.plot(x)
problem:
I can't use the standard Mac OS X shorcuts to access the window generated by the plot command.
For example, I can't Command-Tab to the window. Thus, if the window is behind some other window, I need to mouse over to it! Command-W doesn't close it.
Obviously, this is unacceptable. It seems like perhaps running Lion instead of Leopard might fix this, but i haven't upgraded yet. I feel like the problem has something to do with iPython generating windows that aren't fully Cocoa-aware in some sense, but I really know very little so I'm not particularly confident in this hypothesis.
Thus, any ideas on how to either resolve or get around this issue would be much appreciated.
From the description on the iPython page, it looks like Python uses Qt to generate
UI. This means that the windows it generates are definitely not Cocoa windows and will not act like them.
There's not likely to be an easy solution to this issue.
I experienced the same annoyance with my Anaconda installation of Python 2.7.10 on Mac OS X Yosemite 10.10.5. One solution I found was to change the backend to Mac OS X or Qt4Agg by creating a ~/.matplotlib/matplotlibrc file with the line:
backend: MacOSX
or
backend: Qt4Agg
Now I can easily get to the plot window with Application switcher using Command - Tab and close it with Command - W .

Categories