Custom colormaps and color bars
Show older comments
Running MATLAB R2013a
I am using a custom colormap created with the colormap command on a plot. When I add a legend, the legend is in the correct color order. However, when I add a colorbar, it uses the 'jet' colormap. How do I get it to use my custom colormap? I included the relevant code below:
%setting the line color order where varycolor is from matlab central
numpowers = 50; %(usually ~20-100)
clrmap = colormap(varycolor(numpowers)); %sets line color order
figure('OuterPosition',[100 100 1000 640],'name',strcat('RLplot',devname),'numbertitle','off');
axes('FontSize',20,'FontName','Times New Roman','ColorOrder',clrmap);
grid on;
hold all;
box on;
for i = 1:length(structkey)
%each structure element in the structure contains a dataset
%a new color is assigned for each structure element
%each structure element corresponds to a number, say 1:1:50
eval(['RL = RLdata.' char(structkey(i)) ';']);
L=RL(:,1); %#ok<NODEF>
R=RL(:,2);
% Least squared linear fit to data
x=0:3:24;
fitcoeff = polyfit(L,R,1);
fiteqn = polyval(fitcoeff,x);
%Plot data
plot(L,R,'x',x,fiteqn,'-','Color',clrmap(i,:),'LineWidth',1.5);
%Write the string that will become the legend --> how I did the legend
% tmplgd = char(structkey(i));
% tmplgd = str2double(tmplgd(2:end-2))/1000;
% legendstr(2*i-1,1) = cellstr(sprintf('%0.3f %s', tmplgd, '\muW/\mum^3')); %#ok<AGROW>
% legendstr(2*i,1) = cellstr(sprintf('%s %0.1f %s %0.1f', 'y =', ...
% fitcoeff(1,1), 'x +', fitcoeff(1,2))); %#ok<AGROW>
end
Accepted Answer
More Answers (1)
Andrew Newell
on 14 Jun 2013
Edited: Andrew Newell
on 14 Jun 2013
With the command
clrmap = colormap(...)
you only define the variable clrmap, not set the colormap. Here is a simple example of how you could make colormap work:
clrmap = colormap(lines); %sets line color order
colormap(clrmap);
figure
axes('ColorOrder',clrmap);
hold all;
for i = 1:5
plot(0:1,[0 i])
end
colorbar
Note that, having defined the color order, you don't need to refer to it explicitly in the plot.
3 Comments
Becca
on 14 Jun 2013
Andrew Newell
on 14 Jun 2013
Did my example work without change? It should have both the lines and the colorbar using colormap lines, which has rapid changes in color. You could try substituting your colormap. Explicitly including the color in the plot commands is a side issue.
Becca
on 14 Jun 2013
Categories
Find more on Color and Styling 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!