Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I get pyplot images to show on a console app?

I'm trying to create an image using matplotlib.pyplot.imshow(). However, when I run the program from my console, it doesn't display anything?

This is the code:

import matplotlib.pyplot

myimage = gen_image()

matplotlib.pyplot.gray()
matplotlib.pyplot.imshow(results)

But this shows nothing.

like image 262
Nathan Fellman Avatar asked Nov 03 '09 22:11

Nathan Fellman


People also ask

Does matplotlib work in terminal?

The best use of Matplotlib differs depending on how you are using it; roughly, the three applicable contexts are using Matplotlib in a script, in an IPython terminal, or in an IPython notebook.

Why isn't my plot showing in Python?

It means if we are not using the show() function, it wouldn't show any plot. When we use the show() function in the non-interactive mode. That means when we write the code in the file it will show all the figures or plots and blocks until the plots have been closed.


1 Answers

You have to call the show function to actually display anything, like

matplotlib.pyplot.show()

Unfortunately the matplotlib documentation seems to be currently broken, so I can't provide a link.

Note that for interactive plotting one typically uses IPython, which has special support for matplotlib.

By the way, you can do

import matplotlib.pyplot as plt

to make the typing less tedious (this is pretty much the official standard way).

like image 70
nikow Avatar answered Oct 22 '22 14:10

nikow