'4D plot surface does'nt display the colors of my 4th dimension

I've been trying de plot 4D figures of aerosols emissions in the atmosphere with X = Longitude, Y = latitude, Z = Injection Heigh of aerosols and cbar = Emissions quantity.

The following lines do the job but the cbar datas seems to be 0 everywhere.

fig, ax = plt.subplots(subplot_kw={'projection': '3d'})
X, Y = np.meshgrid(lons2,lats2)
Z = Inj
C=Emi
scamap = plt.cm.ScalarMappable(cmap='inferno')
fcolors = scamap.to_rgba(C)
ax.plot_surface(X, Y, Z, facecolors=fcolors, cmap='inferno')
norm = mpl.colors.Normalize(vmin=0, vmax=10e-8)
fig.colorbar(scamap,norm=norm)
plt.show()

enter image description here

As you can see, it's all black.

Do you have any suggestion, or another way to plot those data ? Maybe scatter could be a solution but I can't figure it out.

Have a good day,



Solution 1:[1]

You need to normalize the colors, like this:

import numpy as np
import matplotlib.pyplot as plt
import matplotlib.cm as cm
from matplotlib.colors import Normalize

fig, ax = plt.subplots(subplot_kw={'projection': '3d'})
x = y = np.linspace(-2, 2)
X, Y = np.meshgrid(x, y)
Z = np.cos(X**2 + Y**2)
# color values
C = np.sqrt(X**2 + Y**2)
cmap = cm.inferno
norm = Normalize(vmin=np.amin(C), vmax=np.amax(C))
# create face colors from a colormap and normalized color values
fcolors = cmap(norm(C))
scamap = cm.ScalarMappable(cmap=cmap, norm=norm)
ax.plot_surface(X, Y, Z, facecolors=fcolors, cmap=cmap)
fig.colorbar(scamap)
plt.show()

enter image description here

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Davide_sd