# How do I plot the surface of a 4D plot?

I am trying to plot the wave function for a particle in a 3D box. This requires me to plot 4 variables: x, y, z axes and the probability density function.

The probability density function is:

abs((np.sin((p*np.pi*X)/a))*(np.sin((q*np.pi*Y)/b))*(np.sin((r*np.pi*Z)/c)))**2


I am using np.arange() for the X, Y and Z.

I have read that to do this you need to plot the surface of a 4D plot. Here is what it is supposed to look like:

• How about using a color to represent the probability density? Dec 4, 2013 at 22:20
• I would imagine opacity would work well for this sort of plot. You may need to provide different perspectives of each plot, but making the plot more opaque where the particle is likely to be would visualize this data well. Dec 4, 2013 at 22:54
• Since it looks like you're using numpy, you could use mayavi to do the actual plotting. The docs have an example of plotting scalar data in 3D. Dec 5, 2013 at 18:16

This is not really 4D data. As Geoff said, it's 3D scalar data, i.e. you're visualizing a scalar function of three variables: $f(x,y,z)$.

There are several ways to visualize this kind of data, and many tools that will help you. I'll show you a few styles of plots you can make.

1. Contour plot showing one or more $f(x,y,z) = \text{(const.)}$ surfaces, possibly with transparency.

In Mathematica,

ContourPlot3D[
Abs[Sin[\[Pi] x] Sin[\[Pi] y] Sin[\[Pi] z]]^2 == 1/2,
{x, -1, 1}, {y, -1, 1}, {z, -1, 1}]


Show the surfaces of constant probability 0.2, 0.5 and 0.8:

ContourPlot3D[
Abs[Sin[\[Pi] x] Sin[\[Pi] y] Sin[\[Pi] z]]^2,
{x, -1, 1}, {y, -1, 1}, {z, -1, 1}, Contours -> {0.2, 0.5, 0.8},
ContourStyle -> (Directive[#, Opacity[0.25]] & /@ {Yellow, Orange, Red}),
Lighting -> "Neutral", Mesh -> None]


2. You can do some type of volume visualization, possibly with cutouts and slicing. You'll be able to assign a colour and an opacity to each point in 3D. More advanced tools will also let you choose a transfer function.

imgdata =
Table[Abs[Sin[\[Pi] x] Sin[\[Pi] y] Sin[\[Pi] z]]^2,
{x, -1., 1, .01}, {y, -1., 1, .01}, {z, -1., 1, .01}];

img = Image3D[imgdata, ClipRange -> {{150, 200}, {0, 100}, {0, 200}}]


Slicing often helps, especially if you can interactively control which slice to display.

Image3DSlices[img, Range[1, 200, 10]]


These examples were meant as ideas for what types of visualizations you can try to create. There are many different free and commercial tools that you can use to make the plots.

The traditional approach for scalar field-based data (temperature, velocity magnitude, pressure, density, etc.) plotted over two or three space dimensions uses color. It's important to note that choice of color scheme can distort your impressions of the data. For this reason, do not use a rainbow color scheme. (For why, see here, here, here, and here.) Unfortunately, rainbow is the default color scheme in MATLAB and matplotlib.

If you're trying to highlight changes in intensity, using a scheme that varies in saturation works well, like one that ranges from white (zero density) to black (maximum density). Transparency can also work well. A tricky problem with 3-D plots when using color is that you'll need to look at the data from multiple perspectives to get a fuller picture of trends and features; you may also need to plot slices.

• Yes, a colour plot won't do, you're right. Any idea what sort of function I would use for this then? Dec 5, 2013 at 19:52
• Do you mean a function in Python? There are a few libraries out there that work well for plotting. Matplotlib is usually used for 2D plots rather than 3D plots; ggplot and Bokeh have similar use cases. Mayavi2, as jorgeca suggested, is good for 3D plotting. I tend to prefer software packages for visualization of 3D data, like ParaView or VisIt. You could also look at yt (which is Python-based), and does a good job of plotting 3D data. Any worthwhile visualization library should give you multiple options for color scales, opacity, saturation, etc. Dec 5, 2013 at 20:03