How to draw heatmap colors in 3D in matplotlib

I use Matplotlib 3D to build three dimensions of my dataset, as shown below: enter image description here

But now I also want to visualize the 4-dimensional dimension (which is a scalar value from 0 to 20) as a heat map. So basically, I want each point to have a color based on this 4th dimension value.

Is there such a thing in Matplotlib? How to convert a bunch of numbers between [0-20] into thermal print colors?

I took the code here: http://matplotlib.org/mpl_examples/mplot3d/scatter3d_demo.py

+4
python matplotlib plot heatmap
source share
1 answer

Yes, something like this:

update is a version with a color bar.

import numpy as np from pylab import * from mpl_toolkits.mplot3d import Axes3D import matplotlib.pyplot as plt def randrange(n, vmin, vmax): return (vmax-vmin)*np.random.rand(n) + vmin fig = plt.figure(figsize=(8,6)) ax = fig.add_subplot(111,projection='3d') n = 100 xs = randrange(n, 23, 32) ys = randrange(n, 0, 100) zs = randrange(n, 0, 100) colmap = cm.ScalarMappable(cmap=cm.hsv) colmap.set_array(zs) yg = ax.scatter(xs, ys, zs, c=cm.hsv(zs/max(zs)), marker='o') cb = fig.colorbar(colmap) ax.set_xlabel('X Label') ax.set_ylabel('Y Label') ax.set_zlabel('Z Label') plt.show() 

as follows:

colbar

update Here is a clear example of coloring your data points using the 4th dimension attribute.

 import numpy as np from pylab import * from mpl_toolkits.mplot3d import Axes3D import matplotlib.pyplot as plt def randrange(n, vmin, vmax): return (vmax-vmin)*np.random.rand(n) + vmin fig = plt.figure(figsize=(8,6)) ax = fig.add_subplot(111,projection='3d') n = 100 xs = randrange(n, 0, 100) ys = randrange(n, 0, 100) zs = randrange(n, 0, 100) the_fourth_dimension = randrange(n,0,100) colors = cm.hsv(the_fourth_dimension/max(the_fourth_dimension)) colmap = cm.ScalarMappable(cmap=cm.hsv) colmap.set_array(the_fourth_dimension) yg = ax.scatter(xs, ys, zs, c=colors, marker='o') cb = fig.colorbar(colmap) ax.set_xlabel('X Label') ax.set_ylabel('Y Label') ax.set_zlabel('Z Label') plt.show() 

4dcols

+11
source share

All Articles