Basic matshow question

Hello,

I am relatively new to Python, numpy, matplotlib, etc., with a reasonable amount of Matlab experience.

I am trying to do some simple array visualizations before moving on to specific work.

e.g. I have a 5x5x10 array and I'd like to see each 5x5 piece as a "frame" with a short pause in between. I realize there are animation methods and such but I think the code below ought to work and don't understand what I'm getting.

Expect: A figure window with the first 5x5 piece of the array, a short pause, a new figure window overwriting the old one with the second 5x5 piece, and so on. Equivalent code in Matlab does this.

Result: Figure window opens, nothing gets drawn in it, there's a pause, the window closes, and another window opens in the same location with nothing in it, and so on.

If I refresh the kernel, create the 3D array, and manually show one slice, it works. As soon as I run the loop, all I get from then on is blank windows whether I do it within a loop or type the matshow, show commands.

This happens in iPython with and without the notebook interface.

I'm trying to understand Matplotlib in some detail and am watching the SciPy2014 videos along with RTFM.
https://www.youtube.com/watch?v=A2adyFMsut0 et.seq.

So far, I've come up empty in figuring out what I'm doing wrong.

I have Python 2.7.8/Anaconda 2.0.1

Thanks,

JBB
===== Test code

import numpy as np
from matplotlib.pylab import *
import time as time

# Create a 3D array of "frames"
A = np.random.rand(5,5,10)

#Turn on interactive mode
ion()
# Turn off interactive mode
# ioff()

# Attempt to show each frame with a short delay between frames
for k in range(10):
     matshow(A[:,:,k])
     show()
     time.sleep(1)
     close()

Hello,

I am relatively new to Python, numpy, matplotlib, etc., with a
reasonable amount of Matlab experience.

I am trying to do some simple array visualizations before moving on to
specific work.

e.g. I have a 5x5x10 array and I'd like to see each 5x5 piece as a
"frame" with a short pause in between. I realize there are animation
methods and such but I think the code below ought to work and don't
understand what I'm getting.

Expect: A figure window with the first 5x5 piece of the array, a short
pause, a new figure window overwriting the old one with the second 5x5
piece, and so on. Equivalent code in Matlab does this.

Result: Figure window opens, nothing gets drawn in it, there's a pause,
the window closes, and another window opens in the same location with
nothing in it, and so on.

If I refresh the kernel, create the 3D array, and manually show one
slice, it works. As soon as I run the loop, all I get from then on is
blank windows whether I do it within a loop or type the matshow, show
commands.

This happens in iPython with and without the notebook interface.

I'm trying to understand Matplotlib in some detail and am watching the
SciPy2014 videos along with RTFM.
https://www.youtube.com/watch?v=A2adyFMsut0 et.seq.

So far, I've come up empty in figuring out what I'm doing wrong.

I have Python 2.7.8/Anaconda 2.0.1

Thanks,

JBB
===== Test code

import numpy as np
from matplotlib.pylab import *
import time as time

# Create a 3D array of "frames"
A = np.random.rand(5,5,10)

#Turn on interactive mode
ion()
# Turn off interactive mode
# ioff()

# Attempt to show each frame with a short delay between frames
for k in range(10):
      matshow(A[:,:,k])
      show()
      time.sleep(1)
      close()

First, at least initially for this sort of thing, run in "ipython --pylab", and don't use any "ion()" or "ioff()".

Second, replace the "show(); time.sleep(1)" with "pause(1)".

I think that will do it.

Eric

···

On 2014/07/29, 7:04 PM, JBB wrote:

------------------------------------------------------------------------------
Infragistics Professional
Build stunning WinForms apps today!
Reboot your WinForms applications with our WinForms controls.
Build a bridge from your legacy apps to the future.
http://pubads.g.doubleclick.net/gampad/clk?id=153845071&iu=/4140/ostg.clktrk
_______________________________________________
Matplotlib-users mailing list
Matplotlib-users@lists.sourceforge.net
matplotlib-users List Signup and Options

Hello,

>>
>> I am relatively new to Python, numpy, matplotlib, etc., with a
>> reasonable amount of Matlab experience.

...[ Problem and test code trimmed ]

>
> First, at least initially for this sort of thing, run in "ipython
> --pylab", and don't use any "ion()" or "ioff()".
>
> Second, replace the "show(); time.sleep(1)" with "pause(1)".
>
> I think that will do it.
>
> Eric
>

Thank you, very much. It did indeed work.

Is there a pointer to why this worked when my initial approach did not? I thought from the documentation/videos that preparing a plot with relevant commands then issuing the show() command was the preferred approach within Python/Matplotlib.

JBB

···

On 7/29/14, 10:49 PM, Eric Firing wrote:
> On 2014/07/29, 7:04 PM, JBB wrote:

I think it is mostly an issue with how IPython interfaces with matplotlib. If you were running from a pure python prompt, then I would suspect it to work (haven’t tried myself, though). Note that the --pylab option to ipython is now highly discouraged. Instead, I would try the “%matplotlib interactive” cell magic instead (I think that is the right incantation).

···

On Wed, Jul 30, 2014 at 2:15 AM, JBB <jeanbigboute@…287…> wrote:

On 7/29/14, 10:49 PM, Eric Firing wrote:

On 2014/07/29, 7:04 PM, JBB wrote:

Hello,

I am relatively new to Python, numpy, matplotlib, etc., with a

reasonable amount of Matlab experience.

…[ Problem and test code trimmed ]

First, at least initially for this sort of thing, run in "ipython

–pylab", and don’t use any “ion()” or “ioff()”.

Second, replace the “show(); time.sleep(1)” with “pause(1)”.

I think that will do it.

Eric

Thank you, very much. It did indeed work.

Is there a pointer to why this worked when my initial approach did not?

I thought from the documentation/videos that preparing a plot with

relevant commands then issuing the show() command was the preferred

approach within Python/Matplotlib.

JBB


Infragistics Professional

Build stunning WinForms apps today!

Reboot your WinForms applications with our WinForms controls.

Build a bridge from your legacy apps to the future.

http://pubads.g.doubleclick.net/gampad/clk?id=153845071&iu=/4140/ostg.clktrk


Matplotlib-users mailing list

Matplotlib-users@lists.sourceforge.net

https://lists.sourceforge.net/lists/listinfo/matplotlib-users

Hi,

···

On Wed, Jul 30, 2014 at 9:30 AM, Benjamin Root <ben.root@...1304...> wrote:

I think it is mostly an issue with how IPython interfaces with matplotlib.
If you were running from a pure python prompt, then I would suspect it to
work (haven't tried myself, though). Note that the --pylab option to ipython
is now highly discouraged. Instead, I would try the "%matplotlib
interactive" cell magic instead (I think that is the right incantation).

Sorry for being slightly off-topic, but what is the current
recommendation then for interactive plots from the console, if --pylab
is discouraged?

Cheers,

Matthew

There is no change to the recommendations for interactive plots from the console, if by console you mean the normal python REPL. There is no --pylab for that. If you mean from the IPython console, then it is the cell magic I mentioned previously.

···

On Wed, Jul 30, 2014 at 1:19 PM, Matthew Brett <matthew.brett@…287…> wrote:

Hi,

On Wed, Jul 30, 2014 at 9:30 AM, Benjamin Root <ben.root@…1304…> wrote:

I think it is mostly an issue with how IPython interfaces with matplotlib.

If you were running from a pure python prompt, then I would suspect it to

work (haven’t tried myself, though). Note that the --pylab option to ipython

is now highly discouraged. Instead, I would try the "%matplotlib

interactive" cell magic instead (I think that is the right incantation).

Sorry for being slightly off-topic, but what is the current

recommendation then for interactive plots from the console, if --pylab

is discouraged?

Cheers,

Matthew

You would use “%matplotlib inline” if you want the plots to show up inline in the notebook. If you want to use one of the gui backends, it would be “%matplotlib ”. More detail here:

http://ipython.org/ipython-doc/2/api/generated/IPython.core.magics.pylab.html#IPython.core.magics.pylab.PylabMagics.matplotlib

Pylab is going to be removed in IPython 3.0 (in fact it’s already gone in master) since it has several bad interactions with the rest of the numpy/scipy universe and leads to un-reproducible code. See this blog post for more detail on why using pylab is a bad idea:

https://carreau.github.io/posts/10-No-PyLab-Thanks.ipynb.html

···

On Wed, Jul 30, 2014 at 10:19 AM, Matthew Brett <matthew.brett@…1003…7…> wrote:

Hi,

On Wed, Jul 30, 2014 at 9:30 AM, Benjamin Root <ben.root@…1304…> wrote:

I think it is mostly an issue with how IPython interfaces with matplotlib.

If you were running from a pure python prompt, then I would suspect it to

work (haven’t tried myself, though). Note that the --pylab option to ipython

is now highly discouraged. Instead, I would try the "%matplotlib

interactive" cell magic instead (I think that is the right incantation).

Sorry for being slightly off-topic, but what is the current

recommendation then for interactive plots from the console, if --pylab

is discouraged?

Cheers,

Matthew


Infragistics Professional

Build stunning WinForms apps today!

Reboot your WinForms applications with our WinForms controls.

Build a bridge from your legacy apps to the future.

http://pubads.g.doubleclick.net/gampad/clk?id=153845071&iu=/4140/ostg.clktrk


Matplotlib-users mailing list

Matplotlib-users@lists.sourceforge.net

https://lists.sourceforge.net/lists/listinfo/matplotlib-users

right, thank you, it is “inline”, not “interactive”. I really need a memory upgrade…

Cheers!

Ben Root

···

On Wed, Jul 30, 2014 at 1:26 PM, Nathan Goldbaum <nathan12343@…1896…> wrote:

You would use “%matplotlib inline” if you want the plots to show up inline in the notebook. If you want to use one of the gui backends, it would be “%matplotlib ”. More detail here:

http://ipython.org/ipython-doc/2/api/generated/IPython.core.magics.pylab.html#IPython.core.magics.pylab.PylabMagics.matplotlib

Pylab is going to be removed in IPython 3.0 (in fact it’s already gone in master) since it has several bad interactions with the rest of the numpy/scipy universe and leads to un-reproducible code. See this blog post for more detail on why using pylab is a bad idea:

https://carreau.github.io/posts/10-No-PyLab-Thanks.ipynb.html

On Wed, Jul 30, 2014 at 10:19 AM, Matthew Brett <matthew.brett@…1003…7…> wrote:

Hi,

On Wed, Jul 30, 2014 at 9:30 AM, Benjamin Root <ben.root@…1304…> wrote:

I think it is mostly an issue with how IPython interfaces with matplotlib.

If you were running from a pure python prompt, then I would suspect it to

work (haven’t tried myself, though). Note that the --pylab option to ipython

is now highly discouraged. Instead, I would try the "%matplotlib

interactive" cell magic instead (I think that is the right incantation).

Sorry for being slightly off-topic, but what is the current

recommendation then for interactive plots from the console, if --pylab

is discouraged?

Cheers,

Matthew


Infragistics Professional

Build stunning WinForms apps today!

Reboot your WinForms applications with our WinForms controls.

Build a bridge from your legacy apps to the future.

http://pubads.g.doubleclick.net/gampad/clk?id=153845071&iu=/4140/ostg.clktrk


Matplotlib-users mailing list

Matplotlib-users@lists.sourceforge.net

https://lists.sourceforge.net/lists/listinfo/matplotlib-users

Hi,

You would use "%matplotlib inline" if you want the plots to show up inline
in the notebook. If you want to use one of the gui backends, it would be
"%matplotlib <backend>". More detail here:

Module: core.magics.pylab — IPython 2.4.2-maint documentation

Pylab is going to be removed in IPython 3.0 (in fact it's already gone in
master) since it has several bad interactions with the rest of the
numpy/scipy universe and leads to un-reproducible code. See this blog post
for more detail on why using pylab is a bad idea:

https://carreau.github.io/posts/10-No-PyLab-Thanks.ipynb.html

I personally use the IPython console for my just-checking interactive
work, rather than the notebook.

Checking now, '%matplotlib inline' in the console raises an error,
understandably enough, but this works:

%matplotlib osx
import matplotlib.pyplot as plt
plt.plot(range(10))

I know that's what you said, it's just to give a complete snippet for Googlers,

Cheers,

Matthew

···

On Wed, Jul 30, 2014 at 10:26 AM, Nathan Goldbaum <nathan12343@...287...> wrote:

Quick clarifications:

- `--pylab` as a command-line flag is strongly discouraged and we will
likely remove it (having it in that location requires really awkward
special-case code everywhere). But we'll probably keep the %pylab magic
indefinitely, since it's useful for quick-and-dirty command-line work where
you don't care about namespace pollution, just about minimizing typing and
namespace access for convenience.

- The %matplotlib magic can also be used without args, case in which it
works like %pylab used to, and will pick up the user's default backend:

In [1]: %matplotlib
Using matplotlib backend: TkAgg

In [2]: import matplotlib.pyplot as plt

In [3]: plt.plot([1,2,3])
Out[3]: [<matplotlib.lines.Line2D at 0x4b90ed0>]

Cheers

f

···

On Wed, Jul 30, 2014 at 10:26 AM, Nathan Goldbaum <nathan12343@...287...> wrote:

Pylab is going to be removed in IPython 3.0 (in fact it's already gone in
master) since it has several bad interactions with the rest of the
numpy/scipy universe and leads to un-reproducible code.

--
Fernando Perez (@fperez_org; http://fperez.org)
fperez.net-at-gmail: mailing lists only (I ignore this when swamped!)
fernando.perez-at-berkeley: contact me here for any direct mail

I don't know what development version you are using, but for 2.1.0, it is still there--and it does what needs to be done.

···

On 2014/07/30, 7:26 AM, Benjamin Root wrote:

There is no --pylab for that.

He was talking about the normal Python interpreter, not IPython.

···

On 2014-07-30 14:51, Eric Firing wrote:

On 2014/07/30, 7:26 AM, Benjamin Root wrote:

There is no --pylab for that.

I don't know what development version you are using, but for 2.1.0, it
is still there--and it does what needs to be done.

--
Brendan Barnwell
"Do not follow where the path may lead. Go, instead, where there is no path, and leave a trail."
    --author unknown

Thanks for all the responses. I'll try pure Python via the Spyder IDE vs. Ipython/Ipython Notebook for this and report back. The 'No Pylab Thanks' was enlightening.

I am confused about the role of the Notebook interface. I've been using it to learn the language, do experiments, and preserve tips/tricks I pick up along the way. I've also seen it used in many online presentations and courses on Python, numpy, matplotlib, and other packages. I've found it very helpful so far and would like to stick with it.

But, it sounds like it there are some hidden issues in how the notebook i/f interacts with matplotlib? Is it not the best choice for learning plotting? If not, what's recommended - IDE? Python from a terminal window? ... ?

JBB

Hi,

Thanks for all the responses. I'll try pure Python via the Spyder IDE
vs. Ipython/Ipython Notebook for this and report back. The 'No Pylab
Thanks' was enlightening.

I am confused about the role of the Notebook interface. I've been using
it to learn the language, do experiments, and preserve tips/tricks I
pick up along the way. I've also seen it used in many online
presentations and courses on Python, numpy, matplotlib, and other
packages. I've found it very helpful so far and would like to stick with it.

But, it sounds like it there are some hidden issues in how the notebook
i/f interacts with matplotlib? Is it not the best choice for learning
plotting? If not, what's recommended - IDE? Python from a terminal
window? ... ?

I think the notebook is pretty good for all those things. I have got
out of the habit of using the notebook when I'm writing code, because
it can be hard to switch between the notebook and my usual development
tools - in my case vim and bash and occasionally the IPython console.
But - for exploring plotting, I would say it was a good choice.

Cheers,

Matthew

···

On Wed, Jul 30, 2014 at 5:14 PM, JBB <jeanbigboute@...287...> wrote:

Yup, I'm obviously as biased as they come, but I think that the immediate
feedback and the ability to keep plots together with their source code,
makes learning and experimenting with mpl (or other plotting libs) an ideal
use case for the notebook.

The only catch to be aware of is that, in order for a simple

plot(x)

to produce a figure without having to manually call show(), we do a bit of
magic behind the scenes. And that magic can occasionally lead to duplicate
figures when you start manually calling display(fig) yourself. So in that
more advanced scenario, you will need to add the occasional plt.close()
call.

HTH,

f

···

On Wed, Jul 30, 2014 at 6:11 PM, Matthew Brett <matthew.brett@...287...> wrote:

But - for exploring plotting, I would say it was a good choice.

--
Fernando Perez (@fperez_org; http://fperez.org)
fperez.net-at-gmail: mailing lists only (I ignore this when swamped!)
fernando.perez-at-berkeley: contact me here for any direct mail

I've followed up on several suggestions and here is what I've done/found.

(I know I don't use mlab or pylab but I pulled the import lines from another source and am leaving them in for the heck of it)

Code A:
import numpy as np
import matplotlib
import time
from matplotlib import pylab, mlab, pyplot

A = np.random.rand(5,5,10)
for j in range(10):
     pyplot.matshow(A[:,:,j])
     # pyplot.pause(.5)
     pyplot.show()
     time.sleep(0.5)
     pyplot.close()

Code B:
import numpy as np
import matplotlib
import time
from matplotlib import pylab, mlab, pyplot

A = np.random.rand(5,5,10)
for j in range(10):
     pyplot.matshow(A[:,:,j])
     pyplot.pause(.5)
     # pyplot.show()
     # time.sleep(0.5)
     pyplot.close()

···

================
1)
Code A:
Spyder Python (Mac) - A blank figure window is generated, closed, and repeated over ten iterations. No user intervention needed to allow loop to continue.

Adding a pyplot.ioff() line after the from matplotlib line gives the expected/hoped behavior: Ten arrays appear, one after the other, no intervention needed.

---

Ipython Notebook (no --pylab) - A figure window with an array appears. I have to close it using the Window Close titlebar button, then the next one appears, waits for close action, etc.

Adding a pyplot.ion() statement after the from matplotlib line yields the same result as Spyder Python w/o the pyplot.ioff() line. 10 blank windows appear one after the other.

Code B:
Spyder Python and Ipython Notebook (no --pylab) - Both display expected/hoped behavior. Figure window appears, figure displays, closes, and then opens with next piece of data

So, there's something about the pause command that's special, at least in this case, and I'd like to understand what that is.

2) Additional interactive test following code runs:

pyplot.matshow(A[:,:,6)]

Spyder Python - Figure pops up, plot is displayed. Zoom, pan, back/forth, home buttons all work

Ipython notebook (no --pylab) - Figure pops up, no plot displays.
Adding pause(x) causes figure to display. Plot is interactive for x seconds. Zoom, pan, etc. work for that time. Notebook shows In[*] for x seconds.

After x seconds, In[*] <-- In[Cell number]. Plot is no longer interactive, mousing over plot shows spinning Mac rainbow ball. Can't even close window using titlebar icons. pyplot.close() from a cell required to close.

I would like to stay with the Notebook as I learn matplotlib but I'm not sure how to get around the plot being interactive for only a defined time period, spinning rainbow balls, etc.

JBB

On 7/30/14, 9:30 AM, Benjamin Root wrote:

I think it is mostly an issue with how IPython interfaces with
matplotlib. If you were running from a pure python prompt, then I would
suspect it to work (haven't tried myself, though). Note that the --pylab
option to ipython is now highly discouraged. Instead, I would try the
"%matplotlib interactive" cell magic instead (I think that is the right
incantation).

On Wed, Jul 30, 2014 at 2:15 AM, JBB > <jeanbigboute@...287... > <mailto:jeanbigboute@…287…>> wrote:

    On 7/29/14, 10:49 PM, Eric Firing wrote:
      > On 2014/07/29, 7:04 PM, JBB wrote:
      >> Hello,
      >>
      >> I am relatively new to Python, numpy, matplotlib, etc., with a
      >> reasonable amount of Matlab experience.

    ...[ Problem and test code trimmed ]

      >
      > First, at least initially for this sort of thing, run in "ipython
      > --pylab", and don't use any "ion()" or "ioff()".
      >
      > Second, replace the "show(); time.sleep(1)" with "pause(1)".
      >
      > I think that will do it.
      >
      > Eric
      >

    Thank you, very much. It did indeed work.

    Is there a pointer to why this worked when my initial approach did not?
       I thought from the documentation/videos that preparing a plot with
    relevant commands then issuing the show() command was the preferred
    approach within Python/Matplotlib.

    JBB

    ------------------------------------------------------------------------------
    Infragistics Professional
    Build stunning WinForms apps today!
    Reboot your WinForms applications with our WinForms controls.
    Build a bridge from your legacy apps to the future.
    http://pubads.g.doubleclick.net/gampad/clk?id=153845071&iu=/4140/ostg.clktrk
    _______________________________________________
    Matplotlib-users mailing list
    Matplotlib-users@lists.sourceforge.net
    <mailto:Matplotlib-users@lists.sourceforge.net>
    matplotlib-users List Signup and Options

------------------------------------------------------------------------------
Infragistics Professional
Build stunning WinForms apps today!
Reboot your WinForms applications with our WinForms controls.
Build a bridge from your legacy apps to the future.
http://pubads.g.doubleclick.net/gampad/clk?id=153845071&iu=/4140/ostg.clktrk

_______________________________________________
Matplotlib-users mailing list
Matplotlib-users@lists.sourceforge.net
matplotlib-users List Signup and Options

Exec. summary - I was having strange behavior with matshow in a loop and also with discrepancies between how iPython Notebook and Python via IDE displayed plots.

Solutions:
1) Using pause instead of show fixed matshow in a loop

2) Explicitly invoking %matplotblib qt or generally %matplotlib {backend} before importing or using matplotlib fixed various problems with plots in notebooks. Now when I create a plot in a notebook, it appears, I can work with it, close it when appropriate, and simultaneously be able to do other work in notebook cells.

Thanks to everyone for the rapid responses.

JBB

I've followed up on several suggestions and here is what I've done/found.

(I know I don't use mlab or pylab but I pulled the import lines from
another source and am leaving them in for the heck of it)

[ Woe/intrigue trimmed ]

···

On 7/30/14, 10:04 PM, JBB wrote:

     Is there a pointer to why this worked when my initial approach did not?
        I thought from the documentation/videos that preparing a plot with
     relevant commands then issuing the show() command was the preferred
     approach within Python/Matplotlib.

     JBB