3D plotting in Spyder

1,542 views
Skip to first unread message

Ryan Villamil

unread,
Jan 11, 2014, 1:51:32 AM1/11/14
to spyd...@googlegroups.com
Hello I am relatively new to Spyder and IPython in general. I am trying to understand how the 2D/3D plotting system works in Spyder's interactive mode. I am new to the forum and I hope I havent broken any rules in the way I am posting.

I installed Spyder 2.1.13 from an Anaconda 1.4.0 install on Win7 64. Python 2.7.3, NumPy 1.7.0 SciPy 0.11.0 Matplotlib 1.2.1.

Spyder seems defaultly come up with a interactive console (that isn't the IPython specific one as far as I can tell). It is within this console that I am trying to do 3D mathplotlib plotting, and am having trouble. So far I have not had any problems with the 2D examples from the Mathplotlib site. I know that the interactive console specifies:
import numpy as np
import scipy as sp
import matplotlib as mpl
import matplotlib.pyplot as plt
from pylab import *
ion()  

at start up. So the example:
import pylab as pl
import numpy as np
X = np.linspace(-np.pi, np.pi, 256, endpoint=True)
C, S = np.cos(X), np.sin(X)
pl.plot(X, C)
pl.plot(X, S)
pl.show()

Translates to being entered in the interactive console as:
X = np.linspace(-np.pi, np.pi, 256, endpoint=True)
C, S = np.cos(X), np.sin(X)
plot(X, C)
plot(X, S)

The show() call does not seem to be necessary, and I see the plot as expected.

The issue i have is with the 3D example:
import numpy as np
from mpl_toolkits.mplot3d import Axes3D
fig = pl.figure()
ax = Axes3D(fig)
X = np.arange(-4, 4, 0.25)
Y = np.arange(-4, 4, 0.25)
X, Y = np.meshgrid(X, Y)
R = np.sqrt(X ** 2 + Y ** 2)
Z = np.sin(R)
ax.plot_surface(X, Y, Z, rstride=1, cstride=1, cmap=pl.cm.hot)
ax.contourf(X, Y, Z, zdir='z', offset=-2, cmap=pl.cm.hot)
ax.set_zlim(-2, 2)
pl.show()

Which I translate to:
from mpl_toolkits.mplot3d import Axes3D
fig = figure()
ax = Axes3D(fig)
X = np.arange(-4, 4, 0.25)
Y = np.arange(-4, 4, 0.25)
X, Y = np.meshgrid(X, Y)
R = np.sqrt(X ** 2 + Y ** 2)
Z = np.sin(R)
ax.plot_surface(X, Y, Z, rstride=1, cstride=1, cmap=cm.hot)
ax.contourf(X, Y, Z, zdir='z', offset=-2, cmap=cm.hot)
ax.set_zlim(-2, 2)

When entering that into the interactive console. A figure window pops up but it is empty. There doesn't seem to be any errors. and the console displays this:
[<matplotlib.lines.Line2D object at 0x0000000007C75FD0>]
>>> from mpl_toolkits.mplot3d import Axes3D
>>> 
>>> fig = figure()
>>> ax = Axes3D(fig)
>>> X = np.arange(-4, 4, 0.25)
>>> Y = np.arange(-4, 4, 0.25)
>>> X, Y = np.meshgrid(X, Y)
>>> R = np.sqrt(X ** 2 + Y ** 2)
>>> Z = np.sin(R)
>>> 
>>> ax.plot_surface(X, Y, Z, rstride=1, cstride=1, cmap=cm.hot)
<mpl_toolkits.mplot3d.art3d.Poly3DCollection object at 0x000000000800DC50>
>>> ax.contourf(X, Y, Z, zdir='z', offset=-2, cmap=cm.hot)
<matplotlib.contour.QuadContourSet instance at 0x000000000275CB08>
>>> ax.set_zlim(-2, 2)
(-2, 2)
>>> 

When just running a .py file (with the original 3D example code) through python in the windows console. I see the correct plot. So I know it works, just trying to figure out how to do this in the Spyder interactive console.

Am I thinking about interactive mode in the wrong way? Should this work, or can it be modified to work? Are there instances where show() is necessary? 

Any help would be appreciated.
-Ryan





David Verelst

unread,
Jan 11, 2014, 11:01:20 AM1/11/14
to spyder
Hi,

I am not entirely sure about this (you might want to check with the matplotlib developers or someone here could correct me), but it seems that the interactive pylab interface does not automatically draws the Axes3D objects on the figure's canvas (I can see the same behaviour on my Linux machine).

In your 3D example, you can update the figure's canvas as follows:
```fig.canvas.draw()```
after which any updates you made to ```fig``` will be drawn onto the canvas (read: the figure in the pop-up window) since the last ```draw()``` command.

Regards,
David


--
You received this message because you are subscribed to the Google Groups "spyder" group.
To unsubscribe from this group and stop receiving emails from it, send an email to spyderlib+...@googlegroups.com.
To post to this group, send email to spyd...@googlegroups.com.
Visit this group at http://groups.google.com/group/spyderlib.
For more options, visit https://groups.google.com/groups/opt_out.

Reply all
Reply to author
Forward
0 new messages