Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

matplotlib figures are not displayed when one types imshow(img) in the command prompt in pdb mode

Has anyone encountered this problem using spyder in debug mode (PDB)? It works fine in the interactive mode.

One suggested solution was to use pause(1) instead of show() after imshow(img).

Is there a better way to see my figures in debug mode? If there was, it would be a real Matlab killer!

like image 707
Yonatan Simson Avatar asked May 29 '14 06:05

Yonatan Simson


People also ask

How do you show images on Imshow?

imshow( BW ) displays the binary image BW in a figure. For binary images, imshow displays pixels with the value 0 (zero) as black and 1 as white. imshow( X , map ) displays the indexed image X with the colormap map . imshow( filename ) displays the image stored in the graphics file specified by filename .

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.

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.


1 Answers

To answer my own question. Apparently this is bug and the pause(1) is the only way to see plot figures in PDB mode.

The other method is to run the entire program as a script by cutting and pasting it into the command line. This way show() can be used instead of pause(1). The advantage to doing it this way, is one can zoom in on the plot. When using pause(1) this is only possible during the pause.

For example:

import numpy as np
from matplotlib import pyplot as plt
import cv2

file_name = 'myimage.jpg'
img = cv2.imread(file_name)
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
edges = cv2.Canny(gray,150,100,apertureSize = 3)

#display image
plt.figure(1)
plt.imshow(edges)
plt.title('edges of image')
plt.show()

Edit:

I just discovered a nice alternative plotting tool in python called guiqwt.

It works with pdb unlike matplotlib

import numpy as np
from guiqwt.pyplot import *
figure("simple plot")
subplot(1, 2, 1)
plot(x, np.tanh(x+np.sin(12*x)), "g-", label="Tanh")
legend()
subplot(1, 2, 2)
plot(x, np.sinh(x), "r:", label="SinH")
show()

You can get it as part of the included packages in python(x,y) or you can download it from here

Edit2:

I just found out the latest IDE released by Pycharm supports matplotlib much better. You can use plt.imshow(img) and don't even have to use plt.show() to display the image in debug mode

like image 184
Yonatan Simson Avatar answered Sep 28 '22 05:09

Yonatan Simson