How do I make a color bar of a loglog plot represent various exponents of plotted curves?

5 views (last 30 days)
I have a loglog plot on which I have plotted various curves with different exponential values (noted in the legend). My goal is to not have the legend, but instead, have a colour bar that associates the colour of the curve with values of the powers of ten. I.e., I have curves plotted for each value of 10^x, where x = [-3, ..., 3] by integer, and I would like to have, say, red be the the colour of the line associated with 10^3 and blue be associated with the line 10^-3, with a gradient in between.

Accepted Answer

Benjamin Kraus
Benjamin Kraus on 14 Feb 2018
Edited: Benjamin Kraus on 14 Feb 2018
I'm not 100% sure I understood your question. If I understood correctly, you still want 7 lines, with 7 different colors (so the plot itself won't change), but instead of a legend you want a colorbar.
The first thing I think you will need to change to get that working is the colors. The default 7 colors come from the axes ColorOrder, and are not part of a colormap. In fact, they are 7 colors picked to be distinct from one another, instead of part of a gradient.
To pick 7 colors from a gradient, you can use something like the parula colormap:
colors = parula(7);
Now you can plot your 7 lines, and specify your 7 colors. Note: It is up to you to make sure lines are colored in the correct order based on the data and ticks you select later.
madeupdata = magic(7);
p = loglog(madeupdata);
for ii = 1:7
p(ii).Color = colors(ii,:);
end
% set(p,{'Color'},mat2cell(colors,ones(1,7))) % Same as for loop above.
Once that is done, you need to create a colorbar with the same colormap, and manually set the ticks:
set(gca,'CLim',[-3 3]);
c = colorbar;
c.Colormap = parula;
c.Limits = [-3 3];
c.Ticks = -3:3;
Does that get you close to what you are trying to do?

More Answers (0)

Products

Community Treasure Hunt

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

Start Hunting!