Plot3D with Julia + PyPlot

683 views
Skip to first unread message

Giacomo Kresak

unread,
Dec 1, 2014, 9:07:10 AM12/1/14
to julia...@googlegroups.com


Bonjour,
Trying to get a 3D (or surface) plot  of a point spread function (Bessel function of order one) using PyPlot: 

In[ ]:   v(tx, ty) = sqrt((1600*pi*tx/(41253*l))^2 + (1600*pi*ty/(41253*l))^2)
In[ ]:   o(tx, ty) = (2*besselj1(v(tx,ty)) / v(tx,ty))^2

In[ ]: using PyPlot
        using PyCall

Do you know if plt.plot is the good way to go? Not able to use plot3d or plot3D with Julia (PyPlot)! 
Do you know if such command is available?  Thanks, G.

David P. Sanders

unread,
Dec 1, 2014, 2:00:34 PM12/1/14
to julia...@googlegroups.com


El lunes, 1 de diciembre de 2014 08:07:10 UTC-6, Giacomo Kresak escribió:


Bonjour,
Trying to get a 3D (or surface) plot  of a point spread function (Bessel function of order one) using PyPlot: 

In[ ]:   v(tx, ty) = sqrt((1600*pi*tx/(41253*l))^2 + (1600*pi*ty/(41253*l))^2)
In[ ]:   o(tx, ty) = (2*besselj1(v(tx,ty)) / v(tx,ty))^2

In[ ]: using PyPlot
        using PyCall

`using PyPlot` automatically loads `PyCall` as well.

 

Do you know if plt.plot is the good way to go? Not able to use plot3d or plot3D with Julia (PyPlot)! 
Do you know if such command is available?  Thanks, G.

In the REPL (terminal) or in IJulia you can do

`plot<TAB>`

to see a list of the commands starting with `plot`.

If you do this after `using PyPlot`, you will find a list that includes `plot3D` (note the capitalization).
You can do `?plot3D` to get some (unfortunately still rather minimal) information about the function. 

This is for plotting lines in 3D. There is also `surf` for plotting surfaces in 3D, that is a wrapper around the matplotlib function with the same name.

You can also search this mailing list and the resources on
the Julia home page for examples.

Hope that helps.
David.

David P. Sanders

unread,
Dec 1, 2014, 2:03:23 PM12/1/14
to julia...@googlegroups.com
I forgot to say that as with the original matplotlib functions that the PyPlot module wraps, you must pass arrays of numbers, not functions (as you defined) to these plotting functions.

Steven G. Johnson

unread,
Dec 1, 2014, 8:32:38 PM12/1/14
to julia...@googlegroups.com
On Monday, December 1, 2014 9:07:10 AM UTC-5, Giacomo Kresak wrote:
Do you know if plt.plot is the good way to go? Not able to use plot3d or plot3D with Julia (PyPlot)! 

Matplotlib's plot3D function is indeed exported by PyPlot.   (You can't access it via plt.plot3D because it is not actually in the matplotlib.pyplot module, it is in mpl_toolkits.mplot3d)  Just call it via, for example:

      θ = linspace(0,6π,300)
      plot3D(cos(θ), sin(θ), θ)

There are also other 3d plotting functions from Matplotlib, e.g. surf(x,y,z) to do a surface plot.

Giacomo Kresak

unread,
Dec 3, 2014, 5:59:16 AM12/3/14
to julia...@googlegroups.com
Dear Steven, 
Thanks for your help.
Definitively Plot3D works on: 

fig = plt.figure()
ax = gca(projection="3d")
X = linspace(-5, 5, 300)
Y = linspace(-5, 5, 300)
R = linspace(-2pi, 2pi, 300)
X, Y = (X, Y)
R = sqrt(X.^2 + Y.^2)
Z = sin(R)
surf = plot_surface(X, Y, Z, rstride=1, cstride=1, linewidth=1, antialiased=False)
plot3D(X,Y,Z)
plt.show()




Not able to get version surface3d of it as suggested by mplot3d code: 



Some translations from python to Julia work ok. But not able to translate these: 

X,Y = meshgrid (X,Y) 
and 
fig.colorbar(surf, shrink=0.5, aspect=5)

I'd tried surf(X,Y,Z)  to get the surface3d aspect;  Error: not defined! 
In a second example I was able to use it: 

fig = plt.figure()
ax = gca(projection="3d")
theta = linspace(-4pi, 4pi, 300)
Z = linspace(-2, 2, 300)
R = Z.^2
X =  R .* sin(theta)
Y =  R .* cos(theta)
surf(X, Y, Z)                 <<<<< It works! 
plt.show() 

Welcome yours suggestions on those translations. 
At the end I would suggest to organize a mplot3d examples code translation from Python to Julia documentation, but I will need help of community!   
Thanks, G. 
 

Steven G. Johnson

unread,
Dec 3, 2014, 9:46:23 AM12/3/14
to julia...@googlegroups.com
The following reproduces the surface plot example from mplot3d:

fig = figure()
X = linspace(-5, 5, 100)'
Y = linspace(-5, 5, 100)
R = sqrt(X.^2 .+ Y.^2)
Z = sin(R)
surf = plot_surface(X, Y, Z, rstride=1, cstride=1, linewidth=0, antialiased=false, cmap="coolwarm")
zlim(-1.01,1.01)
ax = gca()
ax[:zaxis][:set_major_locator](matplotlib[:ticker][:LinearLocator](10))
ax[:zaxis][:set_major_formatter](matplotlib[:ticker][:FormatStrFormatter]("%.02f"))
fig[:colorbar](surf, shrink=0.5, aspect=5)

("surf" is a Matlab-like synonym for "plot_surface" in PyPlot.)

You don't need meshgrid because you can use broadcasting operations (.+) instead to combine a row vector (X: notice the ' to transpose the linspace) and a column vector (Y).

Unlike in mplot3d, you don't need to create the axis with gca(projection="3d"), because the 3d plotting functions like plot_surface do this for you in PyPlot.

As explained in the PyCall documentation, instead of foo.bar() in Python you do foo[:bar]() in Julia.  This is because Julia doesn't yet allow . to be overloaded.

Steven G. Johnson

unread,
Dec 3, 2014, 9:49:51 AM12/3/14
to julia...@googlegroups.com
(It would be great if someone were to undertake a translation of code examples from Matplotlib to PyPlot.   Ideally, create one or more IJulia notebooks with all of the examples, and we can put them in the PyPlot github repository and link to the nbviewer pages from the README.  I'd be happy to advise.)

Steven G. Johnson

unread,
Dec 3, 2014, 9:51:33 AM12/3/14
to julia...@googlegroups.com
The code can be simplified a bit further to:

X = linspace(-5, 5, 100)'
Y = linspace(-5, 5, 100)
R = sqrt(X.^2 .+ Y.^2)
Z = sin(R)
surf = plot_surface(X, Y, Z, rstride=1, cstride=1, linewidth=0, antialiased=false, cmap="coolwarm")
zlim(-1.01,1.01)
ax = gca()
ax[:zaxis][:set_major_locator](matplotlib[:ticker][:LinearLocator](10))
ax[:zaxis][:set_major_formatter](matplotlib[:ticker][:FormatStrFormatter]("%.02f"))
colorbar(surf, shrink=0.5, aspect=5)

Steven G. Johnson

unread,
Dec 3, 2014, 9:56:41 AM12/3/14
to julia...@googlegroups.com
And actually this produces almost the same plot:

X = linspace(-5, 5, 100)'
Y = linspace(-5, 5, 100)
R = sqrt(X.^2 .+ Y.^2)
Z = sin(R)
surf = plot_surface(X, Y, Z, rstride=1, cstride=1, linewidth=0, antialiased=false, cmap="coolwarm")
colorbar(surf, shrink=0.5, aspect=5)

I don't know why the example bothered to use LinearLocator etcetera when they weren't actually doing much with those options.

Daniel Høegh

unread,
Dec 3, 2014, 11:37:20 AM12/3/14
to julia...@googlegroups.com
I have found a good notebook in https://gist.github.com/gizmaa/7214002

Steven G. Johnson

unread,
Dec 3, 2014, 12:26:29 PM12/3/14
to julia...@googlegroups.com
Thanks, I've posted an issue to discuss this:

Giacomo Kresak

unread,
Dec 4, 2014, 6:07:56 AM12/4/14
to julia...@googlegroups.com
Ciao Steven, 
Thanks a lot for your great help. This is a helpful Julia group! 
Attached you will find notebook prepared to share with community. 
Idea is to prepare several of those. Let me know if the format is the good one!? - G.  
mplot3d_Example_in_Julia.ipynb
Reply all
Reply to author
Forward
0 new messages