Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 603
somnathchakrabarti Avatar asked Nov 01 '13 05:11

somnathchakrabarti


People also ask

Why is matplotlib not showing plot?

The issue actually stems from the matplotlib backend not being properly set, or from a missing dependency when compiling and installing matplotlib.

Why is matplotlib not working?

Occasionally, problems with Matplotlib can be solved with a clean installation of the package. In order to fully remove an installed Matplotlib: Delete the caches from your Matplotlib configuration directory. Delete any Matplotlib directories or eggs from your installation directory.

Why is my plot blank in Python?

The reason your plot is blank is that matplotlib didn't auto-adjust the axis according to the range of your patches.

What is matplotlib use (' AGG ')?

The last, Agg, is a non-interactive backend that can only write to files. It is used on Linux, if Matplotlib cannot connect to either an X display or a Wayland display.


2 Answers

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)

like image 154
ev-br Avatar answered Oct 25 '22 06:10

ev-br


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:

backend : Qt4Agg
like image 42
raf Avatar answered Oct 25 '22 06:10

raf