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

查看:66
本文介绍了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

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

#!/usr/bin/env python将 numpy 导入为 np导入 matplotlib.pyplot 作为 pltmu, sigma = 0, 1 # 均值和标准差f = np.random.normal(mu, sigma, 1000) # 生成具有正态分布的特征向量# 绘制直方图 - 检查分布计数,垃圾箱,忽略 = plt.hist(f, 30, normed=True)plt.plot(bins, 1/(sigma * np.sqrt(2 * np.pi)) *np.exp( - (bins - mu)**2/(2 * sigma**2) ),线宽=2,颜色='r')plt.xlabel('值')plt.ylabel('概率')plt.title('直方图')plt.text(60, .025, r'$mu=0,\sigma=1$')plt.axis([-0.4, 0.3, 0, 5])plt.grid(真)plt.show()

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

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

[hue@sandbox ~]$ pythonPython 2.6.6(r266:84292,2013 年 7 月 10 日,22:48:45)[GCC 4.4.7 20120313 (Red Hat 4.4.7-3)] 在 linux2输入帮助"、版权"、信用"或许可证"以获取更多信息.>>>导入 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.其余所有参数均已注释.

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

pngfix.o:在函数`zlib_reset'中:/usr/lib/hue/libpng-1.6.6/contrib/tools/pngfix.c:2151:对`inflateReset2'的未定义引用collect2: ld 返回 1 个退出状态

我现在已经成功安装了一个稳定且有效的 libpng-1.5.9/zlib-1.2.7 组合,而不是之前的不稳定版本 libpng-1.6.6/zlib-1.2.8,并且两个库都已成功安装.

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

解决方案

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

<预><代码>>>>导入 matplotlib>>>matplotlib.get_backend()'Qt4Agg'

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

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

My platform is as follows

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

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.

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'

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

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

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

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.

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?

解决方案

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'

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

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天全站免登陆