【问题标题】:Matplotlib.Pyplot does not show output; No ErrorMatplotlib.Pyplot 不显示输出;没有错误
【发布时间】:2013-11-12 08:04:31
【问题描述】:

我的平台如下

Centos 6.x(在 Win-7 主机上运行的 VirtualBox VM), 蟒蛇2.6.6, Matplotlib 1.3.1, Numpy 1.8.0, scipy 0.14.0.dev-bb608ba

我正在运行下面的 histogram.py 代码

#!/usr/bin/env python

import numpy as np
import matplotlib.pyplot as plt

mu, sigma = 0, 1 # mean and standard deviation
f = np.random.normal(mu, sigma, 1000) # generate feature-vector with normal distribution

# plot the histogram - check the distribution
count, bins, ignored = plt.hist(f, 30, normed=True)

plt.plot(bins, 1/(sigma * np.sqrt(2 * np.pi)) *
                np.exp( - (bins - mu)**2 / (2 * sigma**2) ),
        linewidth=2, color='r')
plt.xlabel('Values')
plt.ylabel('Probability')
plt.title('Histogram')
plt.text(60, .025, r'$\mu=0,\ \sigma=1$')
plt.axis([-0.4, 0.3, 0, 5])
plt.grid(True)
plt.show()

但没有出现输出图。我没有收到任何错误,因此很难调试。

以下是我的 matplotlib 安装的 rc 文件位置和后端

[hue@sandbox ~]$ python
Python 2.6.6 (r266:84292, Jul 10 2013, 22:48:45)
[GCC 4.4.7 20120313 (Red Hat 4.4.7-3)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import matplotlib
>>> matplotlib.matplotlib_fname()
'/usr/lib64/python2.6/site-packages/matplotlib-1.3.1-py2.6-linux-x86_64.egg/matplotlib/mpl-data/matplotlibrc'
>>> matplotlib.get_backend()
'agg'

我是否需要将我的“agg”后端修改为“Qt4Agg”或其他内容?需要我修改rc文件吗?

注意:我检查了我的 matplotlibrc 文件,发现只有 backend : agg。其余所有参数均已注释。

根据以下cmets,我尝试安装libpng,但遇到以下错误:

pngfix.o: In function `zlib_reset':
/usr/lib/hue/libpng-1.6.6/contrib/tools/pngfix.c:2151: undefined reference to `inflateReset2'
collect2: ld returned 1 exit status

我现在已经成功安装了 libpng-1.5.9/zlib-1.2.7 的稳定和工作二重奏,而不是以前的不稳定版本 libpng-1.6.6/zlib-1.2.8,并且两个库都已成功安装。

但是尽管有一个工作和稳定的 libpng,我无法打开由 python 代码生成的 png 文件(如上所示)。虚拟机是否有任何特定的配置设置来打开 .png 文件? .png 文件究竟如何在运行于 WIndows 主机上的 Linux 虚拟机上打开?

【问题讨论】:

  • 它适用于 Matplotlib 1.3.1、numpy 1.7.1、Python 2.7 64 位...
  • 在 Windows 机器上工作。
  • 它适用于 Win7、Python 2.7 32bit Numpy 1.7.1 Matplotlib 1.2.1... 是否出现了一个窗口但没有绘图?还是什么都没有发生?
  • @atomh33ls:没有窗口出现。我在运行 Centos 6.x 的 VirtualBox VM 上运行主机 Win 7
  • @atomh33ls:没有错误或任何消息。所以,是的,什么也没发生

标签: python numpy matplotlib scipy


【解决方案1】:

调试的第一步是将plt.show() 替换为plt.savefig('foo.png')。如果有效,问题很可能出在后端:

>>> import matplotlib
>>> matplotlib.get_backend()
'Qt4Agg'

尝试切换后端,看看是否有帮助: How to switch backends in matplotlib / Python

如果这也没有帮助,请确保您拥有所有依赖项 (http://matplotlib.org/users/installing.html) --- 我只是从源代码重新安装。 (不是pip install matplotlib

【讨论】:

  • 我可以做一个su -c "yum-builddep python-matplotlib"吗?实际上,在安装 matplotlib 之前,我已经安装了 libpng-devel freetype-devel 但这可能仅用于开发(来自 stackoverflow.com/questions/9829175/…)。可能是libpng和freetype没有安装所以plt.savefig('hist.png')打不开
  • 实际上这是一个问题,因为我在后期编辑中添加了版本冲突赌注 zlib 和 libpng。但会接受你的回答。感谢您的建议!
【解决方案2】:

我曾经遇到过类似的问题。我通过在import matplotlib.pyplot as plt 和随后的每个plt.show() 之后立即添加这些行来解决它:

plt.clf()
plt.cla()
plt.close()

【讨论】:

    【解决方案3】:

    我遇到了完全相同的问题。但是,经过一番研究,这是我的修复:

    sudo yum install PyQt4
    sudo gedit file at mpl.matplotlib_fname()
    

    在第 32 行更改第一个也是唯一一个未注释的设置(默认):

    backend : Qt4Agg
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-09-17
      • 2021-03-12
      • 1970-01-01
      • 2012-01-17
      • 1970-01-01
      相关资源
      最近更新 更多