필터 지우기
필터 지우기

Edit colormap based on value

조회 수: 20 (최근 30일)
Felipe Dicker
Felipe Dicker 2024년 1월 22일
답변: Angelo Yeo 2024년 1월 22일
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.

채택된 답변

Angelo Yeo
Angelo Yeo 2024년 1월 22일
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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Colormaps에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by