matplotlib挂在mac osx上,不显示图形

时间:2015-05-16 20:46:15

标签: python macos matplotlib

我正在使用Mac Yosemite和Anaconda的Python 3.4发行版。每当我做的时候

 import matplotlib.pyplot as plt
 ...
 plt.semilogy(x, y)
 plt.show()

在挂断状态下弹出一个空窗口。

我看到了类似帖子:Matplotlib not showing up in Mac OSX,建议使用

sudo port install py25-matplotlib +cairo+gtk2
sudo port install py26-matplotlib +cairo+gtk2

由于我使用的是python 3.4,我尝试了

sudo port install py34-matplotlib +cairo+gtk2

安装顺利,但是当我在PyCharm内的Python控制台上尝试import matplotlib时,出现以下错误:

Backend GTKCairo is interactive backend. Turning interactive mode on.
Failed to enable GUI event loop integration for 'gtk'
Traceback (most recent call last):
  File "/Applications/PyCharm CE.app/Contents/helpers/pydev/pydev_console_utils.py", line 498, in do_enable_gui
enable_gui(guiname)
  File "/Applications/PyCharm CE.app/Contents/helpers/pydev/pydev_ipython/inputhook.py", line 479, in enable_gui
return gui_hook(app)
  File "/Applications/PyCharm CE.app/Contents/helpers/pydev/pydev_ipython/inputhook.py", line 227, in enable_gtk
from pydev_ipython.inputhookgtk import create_inputhook_gtk
  File "/Applications/PyCharm CE.app/Contents/helpers/pydev/pydev_import_hook.py", line 21, in do_import
module = self._system_import(name, *args, **kwargs)
  File "/Applications/PyCharm CE.app/Contents/helpers/pydev/pydev_ipython/inputhookgtk.py", line 19, in <module>
import gtk, gobject
  File "/Applications/PyCharm CE.app/Contents/helpers/pydev/pydev_import_hook.py", line 21, in do_import
module = self._system_import(name, *args, **kwargs)
ImportError: No module named 'gtk'
  1. 可能是什么问题?

  2. 我无法在PyCharm中找到任何gtk模块,但我找到PyGTKPyGTKCodeBufferPyGTKImageView等。是否有任何网站对于gtk模块?

  3. *编辑*

    我通过在Interactive : True文件中添加matplotlibrc解决了这个问题。

4 个答案:

答案 0 :(得分:3)

我按照this blog post中的说明开始工作。

简而言之,请替换:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>SpringBootSecond</groupId>
<artifactId>SpringBootSecond</artifactId>
<version>0.0.1-SNAPSHOT</version>
<properties>    
    <!-- Spring version -->
    <spring-framework.version>4.1.0.RELEASE</spring-framework.version>    
    <!-- ActiveMQ version -->
    <activemq.version>5.10.0</activemq.version>
</properties>

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.3.2.RELEASE</version>
</parent>

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
        <version>1.3.2.RELEASE</version>
    </dependency>

    <!-- Spring aritifacts -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>4.2.0.RELEASE</version>
    </dependency>

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-jms</artifactId>
        <version>4.2.4.RELEASE</version>
    </dependency>  

    <!-- ActiveMQ Artifacts -->
    <dependency>
        <groupId>org.apache.activemq</groupId>
        <artifactId>activemq-spring</artifactId>
        <version>${activemq.version}</version>
    </dependency>
</dependencies> 

<!-- Using JDK 1.7 for compiling -->
<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>2.5.1</version>
            <configuration>
                <source>1.7</source>
                <target>1.7</target>
            </configuration>
        </plugin>
    </plugins>
</build>

<!--    <parent>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-parent</artifactId>
            <version>1.3.2.RELEASE</version>
        </parent>

        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
            </dependency>
        </dependencies>

        <properties>
            <java.version>1.8</java.version>
        </properties>

        <build>
            <plugins>
                <plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
                </plugin>
            </plugins>
        </build>

        <repositories>
            <repository>
                <id>spring-releases</id>
                <url>https://repo.spring.io/libs-release</url>
            </repository>
        </repositories>
        <pluginRepositories>
            <pluginRepository>
                <id>spring-releases</id>
                <url>https://repo.spring.io/libs-release</url>
            </pluginRepository>
        </pluginRepositories> -->
</project>

以下内容:

 import matplotlib.pyplot as plt

并打开一个图形窗口(对我而言,它隐藏在我的IDE背后的背景中,所以起初我以为没有发生任何事情)

答案 1 :(得分:1)

对于像我这样使用IPython的用户,您可以输入:

%matplotlib auto

在Linux和MacOS上均可使用。

答案 2 :(得分:0)

你写的是这篇文章:     ply.show()

正确的形式是plt.show()。这是你的错误吗?或者你只在这篇文章上写错了

答案 3 :(得分:0)

为什么使用带anaconda的端口?

在您的conda环境中(即从命令行source activate my_env键入,其中my_env是您的环境的名称):

condo update conda
conda install matplotlib
conda install pyqt