Using a color scale on a plot

조회 수: 33 (최근 30일)
Tobias Dehn Nielsen
Tobias Dehn Nielsen 2022년 10월 26일
답변: Voss 2022년 10월 26일
I have made an experiment and want to show the results. I want to add a color scale so the function with the value "-10.4" is blue and the value "1.3" is yellow and the others some color in between. It could also be another color or the whole color scheme. Additionality i want a color scale that shows the relationship between the colors and the values. How do i do that?
figure(1)
plot(X1,abs1)
hold on
plot(X2,abs2)
hold on
plot(X3,abs3)
hold on
plot(X4,abs4)
hold on
plot(X5,abs5)
legend("-10.4","-6","-2.3","0.3","1.3")

답변 (1개)

Voss
Voss 2022년 10월 26일
f = figure();
% use colormap parula because it goes blue -> yellow:
cmap = parula();
% define the color limits:
clim = [-10.4, 1.3];
% define the values to use for line colors:
vals = [-10.4, -6, -2.3, 0.3, 1.3];
% indices into cmap of vals, using clim as the upper and lower limits:
idx = 1+round((vals-clim(1))/(clim(2)-clim(1))*(size(cmap,1)-1));
% colors from colormap at those indices:
line_colors = cmap(idx,:);
% plot (random) lines with those colors:
hold on
for ii = 1:numel(vals)
plot(0:0.5:4.5,rand(1,10),'Color',line_colors(ii,:),'LineWidth',1.5);
end
% create the legend:
legend("-10.4","-6","-2.3","0.3","1.3")
% create the colorbar:
set(f,'Colormap',cmap);
caxis(clim)
colorbar();

카테고리

Help CenterFile Exchange에서 Color and Styling에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by