Edit colormap based on value

10 vues (au cours des 30 derniers jours)
Felipe Dicker
Felipe Dicker le 22 Jan 2024
I have a surface plot of X Y and Z coordinates, and I´d like to color the surface based on the values of a fourth array. If the corresponding element of the fourth array is less than 1, the surface element should be blue; if it is greater than one, the surface element should have a color scale from yellow to red; if it is more than 1.5, the surface element should be red. I´ve tried using the colormap function but I can´t atribute the color variations to this fourth array.

Réponse acceptée

Angelo Yeo
Angelo Yeo le 22 Jan 2024
Something like this?
clear; close all; clc;
% Generate example data
[X, Y] = meshgrid(-5:0.1:5);
Z = peaks(X, Y);
C = meshgrid(linspace(0.5, 2, size(X,1))); % Fourth array with random values
% Create surface plot
surf(X, Y, Z, C, 'EdgeColor', 'none');
colorbar
clim([min(C(:)), max(C(:))])
CLIM = clim;
n_color = 100;
% finding the corresponding index for specific values like 1 and 1.5
indfor1 = cindex(1, CLIM, n_color);
indfor1p5 = cindex(1.5, CLIM, n_color);
cmap = colormap(parula(n_color));
cmap(1:indfor1,:) = repmat([0,0,1], indfor1, 1);
% transition from [1,1,0] (yellow) to [1, 0, 0] (red)
cYellow = [1, 1, 0]; cRed = [1, 0, 0];
cmap(indfor1:indfor1p5,:) = ...
[linspace(cYellow(1), cRed(1), indfor1p5-indfor1+1)', ...
linspace(cYellow(2), cRed(2), indfor1p5-indfor1+1)', ...
linspace(cYellow(3), cRed(3), indfor1p5-indfor1+1)'];
cmap(indfor1p5+1:end,:) = repmat([1,0,0], n_color-indfor1p5, 1);
colormap(cmap)
function ind = cindex(val, CLIM, n_color)
% To get the index of colormap for a specific value, val
% CLIM: colormap limits
% n_color: the number of colormap
ind = (val - CLIM(1)) / diff(CLIM) * (n_color-1) + 1;
end

Plus de réponses (0)

Catégories

En savoir plus sur Orange dans Help Center et File Exchange

Produits

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by