Hold-related strangeness, looks like a bug.

Hi all,

today at work we ran into some odd behavior, all of which seems to be
triggered by calling hold(). I'm using a fresh SVN build from this
afternoon.

Here's the first example demonstrating the problem, it's best to run
this in a fresh pylab shell or from the command line, since I suspect
internal state matters (when it shouldn't):

import pylab as P

x = P.arange(10)
y = x+1

P.figure()
P.plot(x,label='one')
P.plot(y,label='two')
P.legend()
P.title('Two plots ok')

P.figure()
P.hold(True)
P.plot(x,label='one')
P.plot(y,label='two')
P.legend()
P.title('Two plots ok - HOLD called')
P.hold(False)

P.figure()
P.plot(x,label='one')
P.plot(y,label='two')
P.legend()
P.title('One plot MISSING!')

P.show()

#### EOF

And here's the second manifestation of the problem, where the colorbar
gets all messed up:

import numpy as N
import pylab as P

a = N.random.rand(64,64)

P.figure()
P.imshow(a)
P.colorbar()
P.title('Colorbar OK')

# If the hold() calls are commented out, the problem disappears
P.figure()
P.hold(True)
P.plot(range(10))
P.hold(False)

P.figure()
P.imshow(a)
P.colorbar()
P.title('Colorbar BROKEN!')

P.show()

#### EOF

It looks like making calls to hold() messes up internal state in pylab
somehow. I've never used hold() myself before, but my officemate did,
coming from matlab, and started seeing bizarre behavior. These are
little self-contained examples showing the problem.

Cheers,

f

I do not think this is a bug. The default value for the hold function
is True. When you originally plotted x and y the hold state was already
set to True. So, actually your first case and second case are the same.
However, right before your third figure you set P.hold(False) . You
then plot x followed by y. So, yes I would expect to see only one line
in the final figure. This is exactly like Matlab (perhaps with the
exception that the default state of hold is False in Matlab). Perhaps
that is what is confusing you and your colleague. In Matlab the default
state of hold is False, however in Matplotlib is looks like the default
state of hold is True.

-Simon

···

On 3/8/07, Fernando Perez <fperez.net@…287…> wrote:

Hi all,

today at work we ran into some odd behavior, all of which seems to be
triggered by calling hold(). I’m using a fresh SVN build from this
afternoon.

Here’s the first example demonstrating the problem, it’s best to run

this in a fresh pylab shell or from the command line, since I suspect
internal state matters (when it shouldn’t):

import pylab as P

x = P.arange(10)
y = x+1

P.figure()
P.plot(x,label=‘one’)

P.plot(y,label=‘two’)
P.legend()
P.title(‘Two plots ok’)

P.figure()
P.hold(True)
P.plot(x,label=‘one’)
P.plot(y,label=‘two’)
P.legend()
P.title(‘Two plots ok - HOLD called’)

P.hold(False)

P.figure()
P.plot(x,label=‘one’)
P.plot(y,label=‘two’)
P.legend()
P.title(‘One plot MISSING!’)

P.show()

EOF

Hi Simon,

> Hi all,
>
> today at work we ran into some odd behavior, all of which seems to be
> triggered by calling hold(). I'm using a fresh SVN build from this
> afternoon.
>
> Here's the first example demonstrating the problem, it's best to run
> this in a fresh pylab shell or from the command line, since I suspect
> internal state matters (when it shouldn't):
>
> import pylab as P
>
> x = P.arange(10)
> y = x+1
>
> P.figure()
> P.plot(x,label='one')
> P.plot(y,label='two')
> P.legend()
> P.title('Two plots ok')
>
> P.figure()
> P.hold(True)
> P.plot(x,label='one')
> P.plot(y,label='two')
> P.legend()
> P.title('Two plots ok - HOLD called')
> P.hold(False)
>
> P.figure()
> P.plot(x,label='one')
> P.plot(y,label='two')
> P.legend()
> P.title('One plot MISSING!')
>
> P.show()
>
> #### EOF
>

I do not think this is a bug. The default value for the hold function is
True. When you originally plotted x and y the hold state was already set to
True. So, actually your first case and second case are the same. However,
right before your third figure you set P.hold(False) . You then plot x
followed by y. So, yes I would expect to see only one line in the final
figure. This is exactly like Matlab (perhaps with the exception that the
default state of hold is False in Matlab). Perhaps that is what is confusing
you and your colleague. In Matlab the default state of hold is False,
however in Matplotlib is looks like the default state of hold is True.

Many thanks for your clear explanation. Since I had never used hold
(or matlab for that matter) in my life, I was rather surprised by the
behavior and (mis)understood it as a bug.

I've always gotten by just fine in pylab without even knowing what
hold did, and simply clearing the figure by hand when needed or just
making a new one.

Sorry for the noise.

Regards,

f

···

On 3/8/07, Simon Wood <sgwoodjr@...287...> wrote:

On 3/8/07, Fernando Perez <fperez.net@...287...> wrote:

Hello,

I would like to display an image using pylab
without automatic scaling to the default size of the plot window.

How can I achieve this ?

Greetings, Uwe

If you want displayed image to just be a pixel dump of the actual
image use figimage

http://matplotlib.sf.net/examples/figimage_demo.py

JDH

···

On 3/9/07, Uwe Schmitt <uschmitt@...1506...> wrote:

I would like to display an image using pylab
without automatic scaling to the default size of the plot window.

How can I achieve this ?

Just to add a few data points -- the default "hold" state can be set
in your rc file. It is a property of the current figure and the
current axes. In you example, when you set fold before creating the
third figure, the setting applued to the current figure (figure 2) but
not the next figure.

JDH

···

On 3/9/07, Fernando Perez <fperez.net@...287...> wrote:

(or matlab for that matter) in my life, I was rather surprised by the
behavior and (mis)understood it as a bug.

I've always gotten by just fine in pylab without even knowing what
hold did, and simply clearing the figure by hand when needed or just
making a new one.