Problem with matplotlib draw()

11 views
Skip to first unread message

Frédéric Clette

unread,
Dec 3, 2012, 4:58:19 AM12/3/12
to python-dev
Dear friends,

Last week, I created programs that create a plot with matplotlib and
then updates it in a loop (same window and axes, but new curve with new
data values each time).
For each update, I included a draw() command in the program to redraw
the plot and it worked perfectly fine.

This morning, with the same program totally unchanged, nothing is
plotted and there is no error message. Absolutely nothing was changed in
the meantime. All my plotting programs are affected in the same way. So,
I was completely taken by surprise by this change of behaviour.

I tried to recover the previous behaviour without success.
If I replace draw() by show(), I then get the plot but the program is
then blocked on the first plot and it becomes impossible to loop and
update the plot.

After checking many Web pages where this kind of problem is mentioned
thousands of times, I also experimented with the pyplot.ion() ioff()
commands to turn on or off interactive mode. This has no effect in my
case. Why? I don't know.

Did anyone face this problem? Is there a cure?
I also wonder what could have caused draw() to cease working properly
overnight, without any change to Python and the program?

Show/draw is a very fundamental thing that seems to be the source of
many troubles for many matplotlib users. As it controls if the plot will
be drawn or not, it is virtually the most important functionality in
matplotlib, and it seems to be a confusing mess.
So, any explanation will probably be useful for many colleagues on this
list.

Thanks for any hint,

Fr�d�ric

(working with Python 2.6.5, on Ubuntu Linux, base IDLE)



Louis Dumortier

unread,
Dec 3, 2012, 5:11:48 AM12/3/12
to python-dev, Frédéric Clette
Le Mon, 03 Dec 2012 10:58:19 +0100, Frédéric Clette
<frederi...@oma.be> a écrit:
> Frédéric
>
> (working with Python 2.6.5, on Ubuntu Linux, base IDLE)
>
>


Hi All,

I had something like this once.
This arrives when the python calling program runs faster that the
underlying C routine.
The solution is to have TWO draw() instructions following.
The first one forces python to wait for graphics interface, the second one
forces the C routine to finish its job.

Here is a sample snippet :



# plot the correlation and fit to the correlation
ax2 = fig.add_subplot(212)
ax2.plot(xcorr0, ycorr0, 'k.')
ax2.plot(xcorr0, corrfit0, color='black')
ax2.plot([xcorrMean0, xcorrMean0], [0, ycorrtest0], 'g-')
ax2.grid(True)
ax2.set_xlabel('Velocity (km/s)')
plt.draw()
plt.draw() # not a mistake ! this draw has to be done twice.



Hope this helps.

Louis

Frédéric Clette

unread,
Dec 3, 2012, 8:20:53 AM12/3/12
to Louis Dumortier, python-dev
Thanks for the prompt suggestion ... but it does not solve the problem
(I even tried 3 draw() commands in a row!).
So, the cause must be elsewhere in my case.

Frédéric

Frédéric Clette

unread,
Dec 4, 2012, 4:07:19 AM12/4/12
to Louis Dumortier, python-dev
Although I did not recover entirely the past behaviour, I managed to get
the plot displayed by the draw() command, by adding the pyplot.ion()
command at the start of the program.
(setting interactive mode)

In my first attempts, this solution did not work either. I don't know
exactly why. In my sense, the only explanation of those
non-reproductible behaviours is that there is a memory effect. Somehow,
Python/matplotlib remembers the "interactive" or non-interactive" status
from one execution of a program to the next, even when interactive
scripts commands are used between the calls to the program.

A word of warning: I read that if the interactive mode is selected in a
program, the plot will be updated at each plotting command instead of
waiting until a draw or show command. So, in some cases, this may
considerably slow down the program. So, being forced to switch to
interactive mode is not without consequence.

Even if switching to interactive mode gives an acceptable solution in
this case, an issue remains (bug?): why doesn't the draw() command
produce a plot in the non-interactive mode? Based on the documentation,
it should.

Have a nice day,

Frédéric



On 12/03/2012 11:11 AM, Louis Dumortier wrote:
Reply all
Reply to author
Forward
0 new messages