简体   繁体   English

Matplotlib.Pyplot不显示输出;没有错误

[英]Matplotlib.Pyplot does not show output; No Error

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 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

I am running the below histogram.py code 我正在运行以下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()

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 以下是我的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'

Do I need to modify my 'agg' backend to 'Qt4Agg' or something else? 我是否需要将'agg'后端修改为'Qt4Agg'或其他? Does it need me to modify the rc file? 我需要修改rc文件吗?

Note: I checked my matplotlibrc file to have only backend : agg . 注意:我检查了我的matplotlibrc文件只有backend : agg Rest all parameters are commented. 休息所有参数都被评论。

As per below comments, I tried installing libpng but facing the below error: 根据以下评论,我尝试安装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

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-1.5.9 / zlib-1.2.7,而不是以前的不稳定版本libpng-1.6.6 / zlib-1.2.8,并且两个lib都已成功安装。

But despite having a working and stable libpng, I can't open the png file being generated by the python code (given above). 但是尽管有一个工作且稳定的libpng,我无法打开由python代码生成的png文件(如上所述)。 Is there any specific configuration setting for VMs to open .png files? VM是否有特定的配置设置来打开.png文件? How exactly can .png files be opened on Linux VMs running on WIndows host? 如何在运行在WIndows主机上的Linux VM上打开.png文件?

The first step in debugging this is to replace plt.show() by plt.savefig('foo.png') . 调试它的第一步是用plt.savefig('foo.png')替换plt.show() 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 尝试切换后端,看看是否有帮助: 如何在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. 如果这也没有帮助,请确保您拥有所有依赖项( http://matplotlib.org/users/installing.html)---我只是从源代码重新安装。 ( not pip install matplotlib ) 不是 pip install matplotlib

I am having the exact same problem. 我现在正在遇到一模一样的问题。 However, this was my fix after a little bit of research: 然而,经过一些研究后,这是我的解决方案:

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

Change the first and only uncommented setting (as default) at line 32: 更改第32行的第一个也是唯一一个未注释的设置(默认设置):

backend : Qt4Agg

I once had a similar problem. 我曾经遇到过类似的问题。 I solved it by adding these lines imediately after import matplotlib.pyplot as plt and after every subsequent plt.show() : 我通过在import matplotlib.pyplot as plt之后以及在每个后续的plt.show()之后立即添加这些行来解决它:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM