Matplotlib.Pyplot不显示输出;No E

2024-03-28 11:35:47 发布

您现在位置:Python中文网/ 问答频道 /正文

我的平台如下

Centos 6.x(运行在Win-7主机上的VirtualBox虚拟机), Python2.6.6, Matplotlib 1.3.1版, 纽比1.8.0, 科学版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。Rest所有参数都被注释。

根据下面的评论,我尝试安装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版本,并且两个libs都已成功安装。

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


Tags: 文件importpngmatplotlibusrasnpplt
3条回答

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

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

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

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

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

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

我也有同样的问题。不过,这是我经过一点研究后的解决办法:

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

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

backend : Qt4Agg

相关问题 更多 >