Skip to content
Tutorials Center
My learning

Topics

Lesson 28 of 42 ยท Maps and 3D

3D Surfaces, Meshes and Cameras

Draw 3D surfaces, contour projections and triangle meshes in Plotly, fix the camera, lighting and aspect, and learn which surface data shapes are required.

A scatter plot shows points. A surface shows a function of two variables: for every (x, y) on a grid there is one height z, and Plotly joins the heights into a continuous sheet that you can rotate. Surfaces are used for terrain, response curves, loss landscapes in machine learning and any "z depends on x and y" relationship. A mesh is the more general idea: a set of triangles in space, which can describe any shape, not only a height field.

This lesson covers go.Surface, go.Mesh3d and the camera controls that apply to all 3D charts. If you have not yet seen scenes, read 3D scatter and line plots first.

go.Surface: z as a 2D grid

A surface trace needs z as a 2D array (a list of rows). x and y are optional: if you give 1D lists, they label the columns and rows; if you give 2D arrays, they define a curvilinear grid. The shape rule is simple: z has one row per y value and one column per x value.

Python A surface from a formula

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

np.meshgrid builds two grids with the same shape as z, so that every formula can use X and Y like ordinary numbers. This is the most common way to prepare surface data.

Here is the same shape in Plotly.js, with a contour projection on the floor of the box. contours.z.project copies the contour lines onto an axis wall, so you can read heights from a 2D view while the 3D shape stays visible.

Surface with projected contours (Plotly.js)

Useful surface options: colorscale, showscale, opacity, cmin/cmax for the color range, surfacecolor (a second 2D array that colors the surface by a different variable) and hidesurface=True when you only want the contour lines.

Missing values

A None or NaN in z leaves a hole in the surface. This is useful for clipping a shape (for example, only drawing the part inside a circle) and for data with gaps. Set connectgaps=True to fill them by interpolation.

go.Mesh3d: any triangle shape

A mesh is described by vertices (x, y, z, one entry per point) and faces, given as three index lists i, j, k: face number n joins vertices i[n], j[n] and k[n]. With only x, y and z, Plotly can triangulate a point cloud automatically using alphahull (a small number such as 5 builds a hull; 0 is the convex hull).

Python A tetrahedron from explicit faces

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Meshes are the way to show STL models, scanned objects, molecule surfaces or finite-element results. intensity (one number per vertex) or facecolor (one color per face) paints the shape. Large meshes with hundreds of thousands of triangles work, because the drawing is done by WebGL.

Other 3D traces worth knowing: go.Cone (vector fields), go.Streamtube (flow lines), go.Isosurface and go.Volume (3D scalar fields such as temperature in a room), and go.Scatter3d from the previous lesson.

The camera

Rotation by mouse is the default, but for a static figure, a report or a "front view" button you set the camera yourself.

  • eye: where the camera is, in units of the scene's normalized box (the box spans -1 to 1 around the center when aspectmode="cube"). Larger numbers mean farther away.
  • center: the point the camera looks at (default 0, 0, 0).
  • up: which direction is up (default z).
  • projection.type: "perspective" or "orthographic".
Preset camera views with buttons

The buttons call relayout with a new camera. Buttons and menus are explained in dropdown menus and buttons. In Python the equivalent one-liner is fig.update_layout(scene_camera=dict(eye=dict(x=0.001, y=0.001, z=2.6))). To save the exact view a user has chosen, read it back from the plotly_relayout event (see hover, click and events).

A surface needs a regular grid. If you only have scattered (x, y, z) samples, interpolate onto a grid first (for example with scipy.interpolate.griddata), or use go.Mesh3d with alphahull, or use a 2D contour or heatmap instead.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. z for go.Surface has 30 rows and 50 columns. How long are y and x (as 1D lists)?

    Rows correspond to y values and columns to x values, so y has 30 entries and x has 50.
  2. 2. What do the i, j and k arrays of go.Mesh3d describe?

    Face n connects vertices i[n], j[n] and k[n] from the x, y, z lists.
  3. 3. What does NaN in the z array of a surface do?

    Missing heights are not drawn. Use connectgaps=True if you prefer interpolation.
  4. 4. Which setting changes where the camera sits?

    The eye vector is the camera position; center and up define where it looks and what is up.

Summary

  • go.Surface takes a 2D z (rows = y, columns = x); np.meshgrid prepares the grid.
  • contours.z.project adds readable contour lines on the walls of the box.
  • go.Mesh3d draws arbitrary triangle meshes from vertices and i, j, k face indexes.
  • The camera has eye, center, up and a projection type; buttons can switch presets.
  • Interpolate scattered samples onto a grid before drawing a surface.

Next: Geo Maps and Choropleths