How can I add more colors to semilogy?

4 views (last 30 days)
Agathe Sadeghi
Agathe Sadeghi on 13 Oct 2021
Commented: DGM on 13 Oct 2021
I have a matrix with dimension 14x11 and when I use the code bellow, I get some of the colors twice. How can I have more colors in this?
GTech_fig = figure;
subplot1 = subplot(1,2,1,'Parent',GTech_fig,'YScale','log','YMinorTick','on');
semilogy(x,y,'Parent',subplot1);

Answers (2)

DGM
DGM on 13 Oct 2021
Edited: DGM on 13 Oct 2021
The colors which are automatically used when plotting things with no explicitly-defined colors are specified by the axes 'colororder' property. This property holds a Nx3 colormap; the default being the 'lines' colormap, which has only 7 unique entries (they repeat).
You can change this to another colormap; however, there are considerations:
N = 14;
x = linspace(0,1,100);
g = [1:N/2 1./(1:N/2)].';
y = x.^g;
plot(x,y,'linewidth',2); % using default 'lines' color table
plot(x,y,'linewidth',2);
set(gca,'colororder',jet(N)) % using 'jet' instead
As you can see, I specified a new color table with enough unique entries that no colors are repeated. The drawback is now that the adjacent colors are very similar to each other. If you were looking at the plot with a legend, it would be hard to tell which is which.
This is why distinct colormaps are often useful. There are a bunch to be found on the web and on the File Exchange.
Consider the example using Stephen's maxdistcolor():
plot(x,y,'linewidth',2);
% this is the line used
%mycolortable = maxdistcolor(N,@sRGB_to_CIELab,'Lmin',0.4,'Lmax',0.75)
% but i'm just going to use the literal result
% instead of attaching all the files and trying to run it here
mycolortable = [0.4444 0.07874 1;1 0 0.127;0 0.8346 0;1 0.02362 0.5079;1 0.5669 0;0.7143 0.7559 0;0.06349 0.7244 0.5238;1 0.5827 0.5397;0.3651 0.3701 0.4127;0 0.3307 0.8413;0.4762 0.3622 0.04762;1 0.2913 0.9524;0 0.7638 1;0.8571 0.6299 0.9365];
set(gca,'colororder',mycolortable)
This example could be improved with some tweaking, but you get the idea.
Ultimately, you may want to take a look at how many things you are trying to cram into a plot and ask if there is a better way to do the visualization. You also might want to consider how your plots are used. If these plots ever are to be printed, the subtle differences between the colors may get altered enough that they become ambiguous again.
See also the built-in colormaps:
  4 Comments
DGM
DGM on 13 Oct 2021
If everything (maxdistcolor, sRGB_to_CIELab, etc) is in one folder (good), then just run
pathtool
and click "Add Folder". After that, hit Save and then close the dialog. You should be able to check that items in that folder are on the path like so
which maxdistcolor

Sign in to comment.


Chunru
Chunru on 13 Oct 2021
plot(rand(30,20)+(1:20)); % plot 20 lines
legend
% The following line will change the default ColorOrder (7 colors)
% change jet(20) to other values
set(gca, 'ColorOrder', jet(20))

Community Treasure Hunt

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

Start Hunting!