Using a color scale on a plot

7 views (last 30 days)
Tobias Dehn Nielsen
Tobias Dehn Nielsen on 26 Oct 2022
Answered: Voss on 26 Oct 2022
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")

Answers (1)

Voss
Voss on 26 Oct 2022
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();

Categories

Find more on Graphics Object Properties in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!