Interactive three-dimensional simulations & visualizations

Visualizing the beauty in physics and mathematics


Project maintained by zhendrikse Hosted on GitHub Pages — Theme by mattgraham

Surface plots of multivariate functions


The images are generated with multivariate_functions.py.

Background information


Implementation details


In order to make the code as generic as possible, we took an approach based on both Numpy and Matplotlib, with which 3D-plots may typically be generated using:

fig, ax = plt.subplots(subplot_kw={"projection": "3d"})

x = np.arange(-5, 5, 0.5)
y = np.arange(-5, 5, 0.5)
X, Y = np.meshgrid(x, y)
R = np.sqrt(X**2 + Y**2)
Z = np.sin(R)

ax.plot_wireframe(X, Y, Z, color='C0')

As we don’t have the possibility to import neither Numpy nor Matplotlib when running VPython in a web page (i.e. when it is transpiled to Javascript), we have to define a similar mesh grid-like function.

This functionality is implemented in the NumpyWrapper class, which we provide with a range for both axes. The three functions f_x(x, y), f_y(x, y), and f_z(x, y) then act on the mesh grid in a similar way as shown above:

def sin_sqrt(resolution=50):
    def f_x(x, _):
        return x

    def f_y(_, y):
        return y

    def f_z(x, y):
        return 5 * sin(sqrt(y * y + x * x))

    return NumpyWrapper(-2 * pi, 2 * pi, -2 * pi, 2 * pi, resolution).get_plot_data(f_x, f_y, f_z)

This approach will seamlessly accommodate much more complicated surfaces and (on polar coordinates based) parametrization, that you can find elsewhere on this site, see for instance the topological shapes in both surface and contour plots.


Share on: