简体   繁体   English

SPSS中的matplotlib图

[英]matplotlib graphs in SPSS

Is it possible to use any other graphing library in SPSS that the built in? 是否可以在SPSS中使用内置的任何其他图形库? I just discovered the python extensions that makes SPSS great. 我刚刚发现了使SPSS很棒的python扩展。

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: 这将创建一个简单的散点图,它可以在任何IDE中正常工作,但是当尝试在SPSS BEGIN PROGRAM END PROGRAM使用该代码时,我收到以下错误:

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. 也许我对SPSS中的python扩展要求太多了,但使用另一个图库比使用它们内置的图库更好。

You can use just about any Python code or library, but you need to do some configuring. 您可以使用任何Python代码或库,但您需要进行一些配置。 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. 安装Python支持(Python Essentials)时,它会安装一个未注册的私有Python系统,以免与可能安装的任何其他Python冲突。 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. 因此,如果您尝试添加其他库,安装程序将不知道该做什么或将它们安装在Statistics安装程序不知道的地方。

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). 解决此问题的最简单方法是安装另一个标准Python安装(如果需要Python 3,则在最新版本中安装2.7版本,在版本24安装3.4版本)。 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. 我使用Anaconda发行版,其中包括许多其他好东西。

I'm not a Mac user, but something like this should work. 我不是Mac用户,但这样的事情应该有效。

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. 我认为Windows上没有框架错误,这是一个不同的问题。 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). 它不是将图像放置在Viewer中,而是弹出自己的窗口(可能隐藏在另一个窗口后面)。

在此输入图像描述

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. 要使其工作,您需要指示matplotlib代码将图像写入某个文件,然后使用SpssClient apis在查看器中插入该图像。 See CreateImageChartItem Method (Python) in the Python programmability help. 请参阅Python可编程性帮助中的CreateImageChartItem方法(Python)。 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. 或者,如果您可以指示matplotlib将图像写入标准输出流,则Statistics可能能够直接在Viewer中捕获它。

I usually do programmability images with R code (even though Python is a way better language(!)), where this all works seamlessly. 我通常使用R代码编写可编程图像(即使Python是一种更好的语言(!)),这一切都可以无缝地工作。 Or I use Python code to write Statistics graphics commands and GPL to have the Statistics engine, which is pretty powerful, do the charting. 或者我使用Python代码编写统计图形命令和GPL以使统计引擎非常强大,可以绘制图表。

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 如果您使用的是Python,那么您会发现有用的另一件事是您可以在外部模式下运行Python,在这里您可以从运行IDE或Python命令行的Python代码开始,然后让它通过运行来调用统计信息

import spss 导入spss

This has great advantages for developing and debugging Python code, but you can't use the SpssClient module methods directly. 这对于开发和调试Python代码有很大的优势,但是不能直接使用SpssClient模块方法。 I ran your code from my IDE (Wing Professional), and the image window popped right up. 我从IDE(Wing Professional)运行您的代码,图像窗口立即弹出。 And, of course, you can use the spss module and related apis in external mode to communicate with Statistics and control it. 当然,您可以在外部模式下使用spss模块和相关的apis与Statistics通信并对其进行控制。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM