VPDEMO: Surface plots of function f(x,y) on the rectangular domain in Jupyter Vpython

51 views
Skip to first unread message

Michal Kaukič

unread,
Apr 16, 2016, 12:02:21 PM4/16/16
to VPython-users
Maybe this is of interest for someone...

I need to make such plots for students in my Calculus course.
Below is an example, based on Rag demo (needs numpy and autograd package for automatic differentiation installed):

from vpython import vertex, vector, quad, scene, color
import autograd.numpy as np
from autograd import grad

scene
.width = scene.height = 600
scene
.scale = (1,1,0.4)

def plot_fxy(fxy,xydom,nx=50,ny=50,color=color.green):
    xmi
,xma,ymi,yma=xydom
    xx
= np.linspace(xmi,xma,nx+1)
    yy
= np.linspace(ymi,yma,ny+1)
    fdx
, fdy = grad(fxy,0), grad(fxy,1)
    verts
= np.zeros((ny+1,nx+1),dtype=object)

   
for yi,y in enumerate(yy):
       
for xi,x in enumerate(xx):
            verts
[yi,xi] = vertex(pos=vector(x,y,fxy(x,y)), normal=vector(-fdx(x,y),-fdy(x,y),1),
                                          shininess
= 0,color=color)

   
for yi in range(ny):
       
for xi in range(nx):
            quad
(v0=verts[yi,xi], v1=verts[yi,xi+1], v2=verts[yi+1,xi+1], v3=verts[yi+1,xi])

# ---------------------------------- example function and plot -------------------------------------------------

mex_hat
= lambda x,y:  np.sin(1-x**2-y**2)/((x**2+y**2)+1)
plot_fxy
(mex_hat,[-2,2,-2,2],40,50)




Bruce Sherwood

unread,
Apr 17, 2016, 12:39:34 AM4/17/16
to VPython-users
Thanks for the quite beautiful Jupyter VPython plot. I confess that I thought that Rag was some math package that I was unfamiliar with, but now I understand you were referring to the Rug demo. I'm intrigued that your program is as short as it is. I'm using Anaconda, and numpy was already installed, but I did have to install autograd.

Bruce Sherwood

unread,
Apr 17, 2016, 12:43:50 AM4/17/16
to VPython-users
You should delete the statement "scene.scale = (1,1,0.4)" which has no effect; it just addes a tuple to the scene object but is never referenced. Jupyter VPython does have "scene.range", with a scalar value.

Michal Kaukič

unread,
Apr 17, 2016, 4:14:09 AM4/17/16
to VPython-users
Hi, Bruce,

    I am glad that You liked the demo. Yes, Rag is Rug demo :-)  Thanks for advice with range. I have no previous knowledge  of VPython, but Jupyter version, I think, is the big step in the right direction. I will use it, following new releases closely. 

Dňa nedeľa, 17. apríla 2016 6:43:50 UTC+2 Bruce Sherwood napísal(-a):

Henri Girard

unread,
Apr 17, 2016, 7:45:24 AM4/17/16
to VPython-users
Hi Michal,
Would you share more graphs or tutorial about it ?
It's very interesting
Best regards
Hg

Michal Kaukič

unread,
Apr 17, 2016, 8:39:26 AM4/17/16
to vpytho...@googlegroups.com
Hi, Henri,

I have only Jupyter notebooks made with 2D graphics (matplotlib,
bokeh) and interactivity by means of ipywidgets. The material covered
- Laplace and z-transforms, solving differential and difference
equations, stability of continuous and discrete linear systems,...
You can contact me by mail, I will send You that material as .ipynb or
by jupyterhub running on my website feelmath.eu.

For 3D in Calculus 2 (functions of several variables) I used
IPython and mayavi2.
If the development of vpython-jupyter permits (needed features
available) I will transform some of the Python scripts to Jupyter
notebooks.

Michal

Henri Girard

unread,
Apr 17, 2016, 8:45:39 AM4/17/16
to VPython-users
Very interesting I use jupyter notebook for many kernels (vpython (ivisual) sage, etc... If you can send me then at ipynb it's great.
henri.girard_at_gmail.com
Best regards
Henri


Le samedi 16 avril 2016 18:02:21 UTC+2, Michal Kaukič a écrit :
Reply all
Reply to author
Forward
0 new messages