Matplotlib.Pyplot不显示输出;没错 [英] Matplotlib.Pyplot does not show output; No Error

查看:474
本文介绍了Matplotlib.Pyplot不显示输出;没错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的平台如下

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

Centos 6.x (VirtualBox VM running on Win-7 host), Python 2.6.6, Matplotlib 1.3.1, Numpy 1.8.0, Scipy 0.14.0.dev-bb608ba

我正在运行以下histogram.py代码

I am running the below histogram.py code

#!/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()

但是没有输出图出现.我没有收到任何错误,因此变得难以调试.

But no output plot is appearing. I am receiving no error, so getting difficult to debug.

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

Following are the rc file-location and backend for my matplotlib installation

[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文件?

Do I need to modify my 'agg' backend to 'Qt4Agg' or something else? Does it need me to modify the rc file?

注意:我检查了matplotlibrc文件中是否只有backend : agg.其余所有参数均已注释.

Note: I checked my matplotlibrc file to have only backend : agg. Rest all parameters are commented.

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

As per below comments, I tried installing libpng but facing the below error:

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都已成功安装.

I have now successfully installed a stable and working duo of libpng-1.5.9/zlib-1.2.7 instead of the previous unstable versions libpng-1.6.6/zlib-1.2.8 and both the libs are successfully installed.

但是,尽管有一个有效且稳定的libpng,但我无法打开由python代码生成的png文件(如上所示). VM是否有任何特定的配置设置来打开.png文件?在Windows主机上运行的Linux VM上如何完全打开.png文件?

But despite having a working and stable libpng, I can't open the png file being generated by the python code (given above). Is there any specific configuration setting for VMs to open .png files? How exactly can .png files be opened on Linux VMs running on WIndows host?

推荐答案

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

The first step in debugging this is to replace plt.show() by plt.savefig('foo.png'). If it works, the problem is most probably with the backend:

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

尝试切换后端,看看是否有帮助: 如何在matplotlib/Python中切换后端

Try switching backends and see if that helps: How to switch backends in matplotlib / Python

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

If that does not help either, make sure you have all the dependencies (http://matplotlib.org/users/installing.html) --- I'd just reinstall from source. (not pip install matplotlib)

这篇关于Matplotlib.Pyplot不显示输出;没错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆