I have an array storing pressure coefficient values for each triangular face from a 3D geometry, and I am trying to obtain the RGB index values from a jet colormap based on the min and max from this array.
I have done this on Matlab like this
cmap = jet(40) * 255; cvars_map = (cvar - min(cvar))/(max(cvar)-min(cvar)) * (40 - 1) + 1; cvars_map = round(cvars_map); cmap = cmap(cvars_map(1:end), :);
where cvar is the array storing the values.
How can I achieve this on Python? Ultimately I will write a PLY file for the geometry with the RGB values for each face.
Advertisement
Answer
We may use NumPy, the syntax is almost the same as MATLAB:
import numpy as np # MATLAB code: # cvar = sin(-pi:0.1:pi)*10 + 17; % Fill cvar with arbitrary data (for example). # cmap = jet(40) * 255; # cvars_map = (cvar - min(cvar))/(max(cvar)-min(cvar)) * (40 - 1) + 1; # cvars_map = round(cvars_map); # cmap = cmap(cvars_map(1:end), :); # writematrix(cmap, 'matlab_cmap.txt') % Write to text file for testing cvar = np.sin(np.arange(-np.pi, np.pi, 0.1))*10 + 17 # Fill cvar with arbitrary data (for example). # NumPy array that has the same content as MATLAB jet(40) jet40 = np.array([[ 0, 0, 0.6000], [ 0, 0, 0.7000], [ 0, 0, 0.8000], [ 0, 0, 0.9000], [ 0, 0, 1.0000], [ 0, 0.1000, 1.0000], [ 0, 0.2000, 1.0000], [ 0, 0.3000, 1.0000], [ 0, 0.4000, 1.0000], [ 0, 0.5000, 1.0000], [ 0, 0.6000, 1.0000], [ 0, 0.7000, 1.0000], [ 0, 0.8000, 1.0000], [ 0, 0.9000, 1.0000], [ 0, 1.0000, 1.0000], [0.1000, 1.0000, 0.9000], [0.2000, 1.0000, 0.8000], [0.3000, 1.0000, 0.7000], [0.4000, 1.0000, 0.6000], [0.5000, 1.0000, 0.5000], [0.6000, 1.0000, 0.4000], [0.7000, 1.0000, 0.3000], [0.8000, 1.0000, 0.2000], [0.9000, 1.0000, 0.1000], [1.0000, 1.0000, 0], [1.0000, 0.9000, 0], [1.0000, 0.8000, 0], [1.0000, 0.7000, 0], [1.0000, 0.6000, 0], [1.0000, 0.5000, 0], [1.0000, 0.4000, 0], [1.0000, 0.3000, 0], [1.0000, 0.2000, 0], [1.0000, 0.1000, 0], [1.0000, 0, 0], [0.9000, 0, 0], [0.8000, 0, 0], [0.7000, 0, 0], [0.6000, 0, 0], [0.5000, 0, 0]]) # cmap = jet(40) * 255; cmap = jet40*255 # cvars_map = (cvar - min(cvar))/(max(cvar)-min(cvar)) * (40 - 1) + 1; cvars_map = (cvar - np.min(cvar))/(np.max(cvar)-np.min(cvar)) * (40 - 1); # No need to add 1 (NumPy arrays first index is 0) # cvars_map = round(cvars_map); cvars_map = np.round(cvars_map).astype(np.int32) # Round and convert to int32 type (NumPy indices must be integers) # cmap = cmap(cvars_map(1:end), :); cmap = cmap[cvars_map] np.savetxt('python_cmap.txt', cmap, fmt='%.1f') # Save to text file for testing