Python Matplotlib.Pyplot不显示输出;无误
我的平台如下 Centos 6.x(在Win-7主机上运行的VirtualBox虚拟机), Python 2.6.6, Matplotlib 1.3.1, Numpy 1.8.0, scipy0.14.0.dev-bb608ba 我正在运行下面的histogram.py代码Python Matplotlib.Pyplot不显示输出;无误,python,numpy,matplotlib,scipy,Python,Numpy,Matplotlib,Scipy,我的平台如下 Centos 6.x(在Win-7主机上运行的VirtualBox虚拟机), Python 2.6.6, Matplotlib 1.3.1, Numpy 1.8.0, scipy0.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
#!/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文件,使其只有后端:agg
。其余所有参数均已注释
根据以下评论,我尝试安装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文件(如上所述)。VM是否有任何特定的配置设置来打开.png文件?如何在WIndows主机上运行的Linux虚拟机上打开.png文件?调试的第一步是将
plt.show()
替换为plt.savefig('foo.png')
。如果可行,问题很可能是后端:
>>> import matplotlib
>>> matplotlib.get_backend()
'Qt4Agg'
尝试切换后端,看看这是否有帮助:
如果这也没有帮助,请确保您拥有所有依赖项()---我只需要从源代码重新安装。(不是
pip安装matplotlib
)我曾经遇到过类似的问题。我通过在import matplotlib.pyplot as plt
之后以及后续的plt.show()之后立即添加这些行来解决这个问题:
我也有同样的问题。然而,这是我经过一点研究后的解决方案:
sudo yum install PyQt4
sudo gedit file at mpl.matplotlib_fname()
在第32行更改第一个也是唯一一个未注释的设置(默认设置):
backend : Qt4Agg
它在Matplotlib 1.3.1、numpy 1.7.1、Python 2.7 64位上对我有效…在windows机器上工作。它在Win7、Python 2.7 32位numpy 1.7.1 Matplotlib 1.2.1上工作。。。是否出现一个窗口,但没有绘图?还是什么都没发生?@atomh33ls:没有窗户出现。我在VirtualBox虚拟机上运行,在Win主机上运行Centos 6.x7@atomh33ls:没有错误或任何消息。那么,是的,什么都没发生我可以做一个su-c“yum builddep python matplotlib”
?实际上,在matplotlib安装之前,我已经安装了libpng-devel-freetype-devel
,但这可能只是用于开发(从)。可能是libpng和freetype没有安装,这就是为什么plt.savefig('hist.png')
无法打开的原因。实际上,这是由于zlib和libpng版本冲突造成的问题,我在后期编辑中添加了这两个版本。但我会接受你的回答。谢谢你的建议!
backend : Qt4Agg