6

I want to plot a 4D heatmap in Python through matplotlib, like this 4d map.

I have already a set of 3D grid points (x,y,z) and its corresponding function value f.

I am thinking of plotting it using plot_surface with x, y, z as the three required arrays, and alter the color gradient using f.

There is a way here to use f for the color gradient, but I have trouble plotting the 3D grid, which I will emphasize that the third dimension is independent of the first two. (The second link shows otherwise.)

Or are there any way to better visualize this 4D data using matplotlib?

Community
  • 1
  • 1
jjv
  • 167
  • 1
  • 5
  • 16
  • Since you only see 3 surfaces and not the whole volume, you could just display 3 standard heat maps in 3 different planes. – Eric Duminil Feb 15 '17 at 13:04
  • 2
    I also thought of that. But I am thinking of visualizing it interactively (through `plt.show()`) where I can see all the sides of the cube. With this, it would be cumbersome to plot 6 heatmaps with custom grid arrays and checking the indices of `f` for each plane. – jjv Feb 15 '17 at 13:09

2 Answers2

1

Your data is of a slightly different form I imagine, but as long as you have a point for every thing you need to be plotted you could use something like they did here:

How to make a 4d plot using Python with matplotlib

Community
  • 1
  • 1
JP1
  • 731
  • 1
  • 10
  • 27
  • 2
    It is interesting in the example that the third dimension is a function of the first two, whereas the given data (in the problem) is that the third dimension is independent of the two. – jjv Feb 15 '17 at 15:06
  • Not answering the question as @jjv has pointed out – John Jan 16 '22 at 10:19
0

There aren't great existing ways to visualize true 4D functions (where the third dimension is independent of the first two as you described), so I wrote a small package plot4d. It should be able to help you visualize your function.

from plot4d import plotter
f = lambda x, y, z: sin(x)*y*cos(z)-x**3
z_range = np.linspace(0,2,10)
frame = plotter.Frame2D(xmin=0, xmax=1, ymin=0, ymax=1)
plotter.plot4d(f, z_range, frame=frame, func_name='f')

f.gif

Installation:

pip install plot4d
Yubin Hu
  • 21
  • 3