Multiple mplot3d figures: new plots display over old ones?

Hi all,
I'm having trouble using multiple figures with mplot3d. Once each new figure
is plotted, the plots from new figure is also displayed in all of the old
figures. For example, once the plot for figure 2 has finished, the plot fo
figure 1 is replaced by a copy of the plot for figure 2, and so on...
I have included an abbreviated version of my script code. My original code
uses Cython and my GluCat library, but I am fairly sure the cause of the
problem lies either with mplot3d or with my Python script code.

I am using openSUSE 11.2 with
python-base-2.6.2-6.7.1.x86_64
python-matplotlib-1.0.1-20.1.x86_64
python-matplotlib-tk-1.0.1-20.1.x86_64
python-matplotlib-wx-1.0.1-20.1.x86_64

Best, Paul

Script excerpt:

...
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
...
# Plot C curves.
for i in xrange(0,C):
...
# Use a new figure for each curve.
fig=plt.figure(figsize=(15,12))
ax = fig.gca(projection='3d')
plt.show()
...
# Split the curve into M segments, each with an appropriate colour.
for j in range(0,M):
  # Find N points forming a curve segment ...
  # Determine the colour of the curve segment...
  # Plot the curve segment using the chosen colour.
  alow=(abot-1 if j>0 else abot)
  ax.plot(x[0,alow:atop],x[1,alow:atop],x[2,alow:atop],c=rgb.tolist())
  plt.draw()
plt.show()

Paul,

I am not exactly sure what your sample script is trying to do. Could you please attach a short self-contained working script that demonstrates your problem?

Ben Root

···

On Sat, Feb 5, 2011 at 10:54 PM, Paul Leopardi <paul.leopardi@…3313…> wrote:

Hi all,

I’m having trouble using multiple figures with mplot3d. Once each new figure

is plotted, the plots from new figure is also displayed in all of the old

figures. For example, once the plot for figure 2 has finished, the plot fo

figure 1 is replaced by a copy of the plot for figure 2, and so on…

I have included an abbreviated version of my script code. My original code

uses Cython and my GluCat library, but I am fairly sure the cause of the

problem lies either with mplot3d or with my Python script code.

I am using openSUSE 11.2 with

python-base-2.6.2-6.7.1.x86_64

python-matplotlib-1.0.1-20.1.x86_64

python-matplotlib-tk-1.0.1-20.1.x86_64

python-matplotlib-wx-1.0.1-20.1.x86_64

Best, Paul

Script excerpt:

from mpl_toolkits.mplot3d import Axes3D

import matplotlib.pyplot as plt

Plot C curves.

for i in xrange(0,C):

Use a new figure for each curve.

fig=plt.figure(figsize=(15,12))

ax = fig.gca(projection=‘3d’)

plt.show()

Split the curve into M segments, each with an appropriate colour.

for j in range(0,M):

Find N points forming a curve segment …

Determine the colour of the curve segment…

Plot the curve segment using the chosen colour.

alow=(abot-1 if j>0 else abot)

ax.plot(x[0,alow:atop],x[1,alow:atop],x[2,alow:atop],c=rgb.tolist())

plt.draw()

plt.show()

Hi all,

I'm having trouble using multiple figures with mplot3d.

I have appended an entire example script, below.

The script incrementally plots 3 curves, one in each of 3 figure windows. The
trouble is, once Figure 2 has finished plotting, the curve for Figure 1
disappears and is replaced by the curve for Figure 2, with the axes for Figure
1; once Figure 3 has finished plotting, the curves for Figures 1 and 2
disappear and are replaced by the curve for Figure 3, with the axes for Figure
1 and Figure 2, respectively.

The original code was written with incremental plotting because the points
took a long time to calculate. Without incremental plotting, the figures
stayed blank for a long time. The script below is very similar to my original
script, but does not depend on my GluCat library.

Best, Paul

···

On Sun, 6 Feb 2011 03:54:48 PM Paul Leopardi wrote:
---

# -*- coding: utf-8 -*-

# Imports needed for array calculation and plotting.
from numpy import array, floor, random, empty, cos, pi
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt

# Constants to control the plotting.
C=3 # Number of curves to plot.
P=1000 # Number of points overall.
R=2 # Scaling constant to use.
N=25 # Number of points in a curve segment.
M=P/N

# Array of points.
x=empty((3,P))
rgb=empty((3))

# Plot C curves.
for i in xrange(0,C):
# Initial point.
x0=random.randn(3)

# Plot a curve using a random bivector in R_{5,0}
# with appropriate scaling.
w=random.randn(3) * 2*pi*R/P

# Use a new figure for each curve.
fig=plt.figure(figsize=(15,12))
# ax=Axes3D(fig)
ax = fig.gca(projection='3d')
plt.show()

# Coordinate limits to determine the colour of the first curve segment.
minx=array([-x0[0],x0[1],-x0[2]])
maxx=minx.copy()

# Split the curve into M segments, each with an appropriate colour.
for j in range(0,M):

  # Find N points forming a curve segment by
  # exponentiating w*k for k from j*N to (j+1)*N-1.
  abot=j*N
  atop=abot+N
  for k in xrange(abot,atop):
   for h in range(0,3):
    x[h,k]=x0[h]+cos(w[h]*k)

  # Determine the colour of the curve segment.
  amid=floor((abot+atop)/2)
  for h in range(0,3):
   sign=(-1)**(h+1)
   minx[h]=min(minx[h],min(sign*x[h,abot:atop]))
   maxx[h]=max(maxx[h],max(sign*x[h,abot:atop]))
   rgb[h]=max(0.0,min((sign*x[h,amid]-minx[h])/(maxx[h]-minx[h]),1.0))

  # Plot the curve segment using the chosen colour.
  alow=(abot-1 if j>0 else abot)
  ax.plot(x[0,alow:atop],x[1,alow:atop],x[2,alow:atop],c=rgb.tolist())
  plt.draw()
plt.show()

Paul,

As formatted, the code would not run. I presume that everything after “for j in range(0,M):” should be indented? When I did that and ran it in ipython, I could not reproduce your problem. What version of matplotlib are you running?

Ben Root

···

On Mon, Feb 7, 2011 at 5:02 AM, Paul Leopardi <paul.leopardi@…3313…> wrote:

Hi all,
On Sun, 6 Feb 2011 03:54:48 PM Paul Leopardi wrote:

I’m having trouble using multiple figures with mplot3d.

I have appended an entire example script, below.

The script incrementally plots 3 curves, one in each of 3 figure windows. The

trouble is, once Figure 2 has finished plotting, the curve for Figure 1

disappears and is replaced by the curve for Figure 2, with the axes for Figure

1; once Figure 3 has finished plotting, the curves for Figures 1 and 2

disappear and are replaced by the curve for Figure 3, with the axes for Figure

1 and Figure 2, respectively.

The original code was written with incremental plotting because the points

took a long time to calculate. Without incremental plotting, the figures

stayed blank for a long time. The script below is very similar to my original

script, but does not depend on my GluCat library.

Best, Paul


-- coding: utf-8 --

Imports needed for array calculation and plotting.

from numpy import array, floor, random, empty, cos, pi
from mpl_toolkits.mplot3d import Axes3D

import matplotlib.pyplot as plt

Constants to control the plotting.

C=3 # Number of curves to plot.

P=1000 # Number of points overall.

R=2 # Scaling constant to use.

N=25 # Number of points in a curve segment.

M=P/N

Array of points.

x=empty((3,P))

rgb=empty((3))

Plot C curves.

for i in xrange(0,C):

Initial point.

x0=random.randn(3)

Plot a curve using a random bivector in R_{5,0}

with appropriate scaling.

w=random.randn(3) * 2piR/P

Use a new figure for each curve.

fig=plt.figure(figsize=(15,12))

ax=Axes3D(fig)

ax = fig.gca(projection=‘3d’)

plt.show()

Coordinate limits to determine the colour of the first curve segment.

minx=array([-x0[0],x0[1],-x0[2]])

maxx=minx.copy()

Split the curve into M segments, each with an appropriate colour.

for j in range(0,M):

Find N points forming a curve segment by

exponentiating wk for k from jN to (j+1)*N-1.

abot=j*N

atop=abot+N

for k in xrange(abot,atop):

for h in range(0,3):

x[h,k]=x0[h]+cos(w[h]*k)

Determine the colour of the curve segment.

amid=floor((abot+atop)/2)

for h in range(0,3):

sign=(-1)**(h+1)

minx[h]=min(minx[h],min(sign*x[h,abot:atop]))

maxx[h]=max(maxx[h],max(sign*x[h,abot:atop]))

rgb[h]=max(0.0,min((sign*x[h,amid]-minx[h])/(maxx[h]-minx[h]),1.0))

Plot the curve segment using the chosen colour.

alow=(abot-1 if j>0 else abot)

ax.plot(x[0,alow:atop],x[1,alow:atop],x[2,alow:atop],c=rgb.tolist())

plt.draw()

plt.show()

Hi Ben,

As formatted, the code would not run. I presume that everything after "for
j in range(0,M):" should be indented? When I did that and ran it in
ipython, I could not reproduce your problem. What version of matplotlib
are you running?

Thanks for your continued close attention to this undoubted problem.

Your email system mangled the indentation. The correct indentation is
displayed at http://sourceforge.net/mailarchive/message.php?msg_id=27020771

I have also included a copy of the script matplotlib_bug_example.py with this
message. [*Not* posted to the list.] Here is how I run it:

ipython -pylab
Python 2.6.2 (r262:71600, Oct 28 2010, 20:54:41)
Type "copyright", "credits" or "license" for more information.
                                                                                                                                                                    
IPython 0.10 -- An enhanced Interactive Python.
? -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help -> Python's own help system.
object? -> Details about 'object'. ?object also works, ?? prints more.
                                                                                                                                                                    
  Welcome to pylab, a matplotlib-based Python environment.
  For more information, type 'help(pylab)'.
                                                                                                                                                                    
In [1]: run matplotlib_bug_example.py

My original post
http://sourceforge.net/mailarchive/message.php?msg_id=27016368
listed what I thought were the relevant versions:

Quote
I am using openSUSE 11.2 with
python-base-2.6.2-6.7.1.x86_64
python-matplotlib-1.0.1-20.1.x86_64
python-matplotlib-tk-1.0.1-20.1.x86_64
python-matplotlib-wx-1.0.1-20.1.x86_64
Unquote

I have since run the script on a second machine which uses Ubuntu Karmic, with
identical results.

Versions in brief:
Linux linfinit 2.6.31.12-0.1-default #1 SMP PREEMPT 2010-12-10 11:18:32 +0100
x86_64 x86_64 x86_64 GNU/Linux

IPython-0.10-3.2.noarch
python-2.6.2-6.7.1.x86_64
python-matplotlib-1.0.1-20.1.x86_64
python-matplotlib-tk-1.0.1-20.1.x86_64
python-matplotlib-wx-1.0.1-20.1.x86_64
python-numpy-1.5.0-17.2.x86_64
python-tk-2.6.2-6.7.1.x86_64

Linux cheeze 2.6.31-22-generic #70-Ubuntu SMP Wed Dec 1 23:51:13 UTC 2010
i686 GNU/Linux

ipython 0.10-1
python 2.6.4-0ubuntu1
python-matplotlib 0.99.0-1ubuntu1
python-numpy 1:1.3.0-3
python-tk 2.6.3-0ubuntu1

I have included a listing showing all versions of all packages with names
containing the string "python", on both machines. [*Not* posted to the list.]
Best, Paul

···

On Wed, 9 Feb 2011 03:15:19 AM you wrote:

Trying out the script on another machine of mine, I was able to reproduce the problem. I will have to see what is the difference between my two computers that would cause the other one to work perfectly fine.

Ben Root

···

On Tue, Feb 8, 2011 at 5:53 PM, Paul Leopardi <paul.leopardi@…3313…> wrote:

Hi Ben,

On Wed, 9 Feb 2011 03:15:19 AM you wrote:

As formatted, the code would not run. I presume that everything after "for

j in range(0,M):" should be indented? When I did that and ran it in

ipython, I could not reproduce your problem. What version of matplotlib

are you running?

Thanks for your continued close attention to this undoubted problem.

Your email system mangled the indentation. The correct indentation is

displayed at http://sourceforge.net/mailarchive/message.php?msg_id=27020771

I have also included a copy of the script matplotlib_bug_example.py with this

message. [Not posted to the list.] Here is how I run it:

ipython -pylab

Python 2.6.2 (r262:71600, Oct 28 2010, 20:54:41)

Type “copyright”, “credits” or “license” for more information.

IPython 0.10 – An enhanced Interactive Python.

? → Introduction and overview of IPython’s features.

%quickref → Quick reference.

help → Python’s own help system.

object? → Details about ‘object’. ?object also works, ?? prints more.

Welcome to pylab, a matplotlib-based Python environment.

For more information, type ‘help(pylab)’.

In [1]: run matplotlib_bug_example.py

My original post

http://sourceforge.net/mailarchive/message.php?msg_id=27016368

listed what I thought were the relevant versions:

Quote

I am using openSUSE 11.2 with

python-base-2.6.2-6.7.1.x86_64

python-matplotlib-1.0.1-20.1.x86_64

python-matplotlib-tk-1.0.1-20.1.x86_64

python-matplotlib-wx-1.0.1-20.1.x86_64

Unquote

I have since run the script on a second machine which uses Ubuntu Karmic, with

identical results.

Versions in brief:

Linux linfinit 2.6.31.12-0.1-default #1 SMP PREEMPT 2010-12-10 11:18:32 +0100

x86_64 x86_64 x86_64 GNU/Linux

IPython-0.10-3.2.noarch

python-2.6.2-6.7.1.x86_64
python-matplotlib-1.0.1-20.1.x86_64

python-matplotlib-tk-1.0.1-20.1.x86_64

python-matplotlib-wx-1.0.1-20.1.x86_64

python-numpy-1.5.0-17.2.x86_64

python-tk-2.6.2-6.7.1.x86_64

Linux cheeze 2.6.31-22-generic #70-Ubuntu SMP Wed Dec 1 23:51:13 UTC 2010

i686 GNU/Linux

ipython 0.10-1

python 2.6.4-0ubuntu1

python-matplotlib 0.99.0-1ubuntu1

python-numpy 1:1.3.0-3

python-tk 2.6.3-0ubuntu1

I have included a listing showing all versions of all packages with names

containing the string “python”, on both machines. [Not posted to the list.]

Best, Paul

Paul,

I have not figured out what is causing the difference between my computers. This might be backend-dependent (and maybe version-dependent). Have you tested your code on different backends?

Ben Root

···

On Tue, Feb 8, 2011 at 8:43 PM, Benjamin Root <ben.root@…3176…> wrote:

On Tue, Feb 8, 2011 at 5:53 PM, Paul Leopardi <paul.leopardi@…3313…> wrote:

Hi Ben,

On Wed, 9 Feb 2011 03:15:19 AM you wrote:

As formatted, the code would not run. I presume that everything after "for

j in range(0,M):" should be indented? When I did that and ran it in

ipython, I could not reproduce your problem. What version of matplotlib

are you running?

Thanks for your continued close attention to this undoubted problem.

Your email system mangled the indentation. The correct indentation is

displayed at http://sourceforge.net/mailarchive/message.php?msg_id=27020771

I have also included a copy of the script matplotlib_bug_example.py with this

message. [Not posted to the list.] Here is how I run it:

ipython -pylab

Python 2.6.2 (r262:71600, Oct 28 2010, 20:54:41)

Type “copyright”, “credits” or “license” for more information.

IPython 0.10 – An enhanced Interactive Python.

? → Introduction and overview of IPython’s features.

%quickref → Quick reference.

help → Python’s own help system.

object? → Details about ‘object’. ?object also works, ?? prints more.

Welcome to pylab, a matplotlib-based Python environment.

For more information, type ‘help(pylab)’.

In [1]: run matplotlib_bug_example.py

My original post

http://sourceforge.net/mailarchive/message.php?msg_id=27016368

listed what I thought were the relevant versions:

Quote

I am using openSUSE 11.2 with

python-base-2.6.2-6.7.1.x86_64

python-matplotlib-1.0.1-20.1.x86_64

python-matplotlib-tk-1.0.1-20.1.x86_64

python-matplotlib-wx-1.0.1-20.1.x86_64

Unquote

I have since run the script on a second machine which uses Ubuntu Karmic, with

identical results.

Versions in brief:

Linux linfinit 2.6.31.12-0.1-default #1 SMP PREEMPT 2010-12-10 11:18:32 +0100

x86_64 x86_64 x86_64 GNU/Linux

IPython-0.10-3.2.noarch

python-2.6.2-6.7.1.x86_64
python-matplotlib-1.0.1-20.1.x86_64

python-matplotlib-tk-1.0.1-20.1.x86_64

python-matplotlib-wx-1.0.1-20.1.x86_64

python-numpy-1.5.0-17.2.x86_64

python-tk-2.6.2-6.7.1.x86_64

Linux cheeze 2.6.31-22-generic #70-Ubuntu SMP Wed Dec 1 23:51:13 UTC 2010

i686 GNU/Linux

ipython 0.10-1

python 2.6.4-0ubuntu1

python-matplotlib 0.99.0-1ubuntu1

python-numpy 1:1.3.0-3

python-tk 2.6.3-0ubuntu1

I have included a listing showing all versions of all packages with names

containing the string “python”, on both machines. [Not posted to the list.]

Best, Paul

Trying out the script on another machine of mine, I was able to reproduce the problem. I will have to see what is the difference between my two computers that would cause the other one to work perfectly fine.

Ben Root