# How to manage colormap and colorbar in a matlab multi-curves plot?

34 views (last 30 days)
Giuseppe on 10 Apr 2022
Commented: DGM on 10 Apr 2022
Hi to everyone,
I have a plot with 50 curves that are the orbits of 50 periodic orbits, each orbit correspond to a value of a constant called "Jacobi constant" (it is an energy value) , so I have a vector of 50 elements called "jacobi_cst" that stores the followings 50 scalars:
3.000576748353570
3.000583342259840
3.000590018845460
3.000596554947660
3.000603171772120
3.000609870077710
3.000616425935580
3.000623060310100
3.000629657944820
3.000636330415510
3.000642857256680
3.000649560630930
3.000656116141060
3.000662738391370
3.000669315388860
3.000675958537960
3.000682547292110
3.000689196186970
3.000695791284050
3.000702431180730
3.000709016645110
3.000715644446600
3.000722309400740
3.000728906460450
3.000735532899990
3.000742084743060
3.000748750854930
3.000755335851910
3.000761928862980
3.000768609354090
3.000775192199620
3.000781845363950
3.000788475204760
3.000795067776960
3.000801695858850
3.000808265737810
3.000814922863360
3.000821489088600
3.000828099771820
3.000834723697250
3.000841330136040
3.000847948043080
3.000854592397690
3.000861196034850
3.000867791975870
3.000874419173230
3.000881028967010
3.000887643313580
3.000894276927840
3.000900891023230
So we have that to each orbit is associated one of these scalar (in increasing order). Can you show me how to color each orbit by using the "colormap" command and then how to show the colors in a lateral bar by using the command "colorbar"? Can you also indicate as display an accetable number of the scalars (surely, the first one and the last one and for example 10 values in total, by using at least 6 decimal digits) on the lateral colorbar?
Here is the code fragment that I use to plot orbits:
fig = figure;
for k1 = 1:50
plot([Ly_orb_filt(1).prop(k1).orbits.x],[Ly_orb_filt(1).prop(k1).orbits.y],...
"Color",my_green); hold on
end
% Info about number of Lyapunov orbits
dim = [.168 .85 .275 .06];
str = '\$50 Planar\ Lyapunov\ orbits\ \$';
annotation('textbox',dim,'String',str,'Interpreter',"latex",'FitBoxToText','off',...
'BackgroundColor',[1 1 1],'FontSize',9);
% Plot L1 point
plot(Ly_orb_filt_sys_data(1).x,Ly_orb_filt_sys_data(1).y,'.',...
'color',[0,0,0],'MarkerFaceColor',my_green,'MarkerSize',10);
text(Ly_orb_filt_sys_data(1).x-0.00015,Ly_orb_filt_sys_data(1).y-0.0008,'L_{1}');
%Primary bodies plots
plot(AstroData.mu_SEM_sys -1,0,'.',...
'color',my_blue,'MarkerFaceColor',my_blue,'MarkerSize',20);
text(AstroData.mu_SEM_sys-1.0028,0-0.001,'\$Earth + Moon\$','Interpreter',"latex","FontSize",9);
grid on; hold off
xlabel('\$x\$','interpreter','latex','fontsize',12);
ylabel('\$y\$','interpreter','latex','FontSize',12);
Sorry if my example is not reproducible but I avoid to load many data files and code lines.
To reply my question you can use an example with a simple set of 50 random curves by using my vector "jacobi_cst".
As final result, I want to get something like the following image taken on internet (in my case I have only one set of curves and not two as shown in the image below):
I would also add the label on the lateral colorbar and show on it, as I have already said above, 10 values of constants (the first value, the last one and then 8 values evenly spaced) with 6 decimal digits.

KSSV on 10 Apr 2022
Say you have 30 curves approximately. I will show these with circles.
R = 1:30 ;
th = linspace(0,2*pi) ;
cmap = jet(30) ;
figure
hold on
for i = 1:30
x = R(i)*cos(th) ;
y = R(i)*sin(th) ;
plot(x,y,'color',cmap(i,:))
end
colorbar
colormap(cmap)
DGM on 10 Apr 2022
% this is a linear series
jc = [3.00057674835357;3.00058334225984;3.00059001884546;3.00059655494766;3.00060317177212;
3.00060987007771;3.00061642593558;3.0006230603101;3.00062965794482;3.00063633041551;
3.00064285725668;3.00064956063093;3.00065611614106;3.00066273839137;3.00066931538886;
3.00067595853796;3.00068254729211;3.00068919618697;3.00069579128405;3.00070243118073;
3.00070901664511;3.0007156444466;3.00072230940074;3.00072890646045;3.00073553289999;
3.00074208474306;3.00074875085493;3.00075533585191;3.00076192886298;3.00076860935409;
3.00077519219962;3.00078184536395;3.00078847520476;3.00079506777696;3.00080169585885;
3.00080826573781;3.00081492286336;3.0008214890886;3.00082809977182;3.00083472369725;
3.00084133013604;3.00084794804308;3.00085459239769;3.00086119603485;3.00086779197587;
3.00087441917323;3.00088102896701;3.00088764331358;3.00089427692784;3.00090089102323];
R = 1:numel(jc);
th = linspace(0,2*pi);
cmap = flipud(jet(numel(jc))); % flip the CT
hold on
for i = 1:numel(R)
x = R(i)*cos(th) ;
y = R(i)*sin(th) ;
plot(x,y)
end
hcb = colorbar;
% this is one method. the other method works fine too
set(gca,'colororder',cmap,'colormap',cmap)
% since jc is linear, it should suffice to just use caxis
% if it's not linear, everything will be more complicated
caxis([min(jc) max(jc)])
% set ticklabel length
tl = split(sprintf('%.6f\n',hcb.Ticks));
hcb.TickLabels = tl(1:end-1);
% set cb title
title(hcb,'aklsdjflakjf')
Note that the ticklabels are not sampled from jc. They're a conveniently selected linear series with at most 5 decimal places. If you want them to be something else, you'd have to also set hcb.Ticks. Similarly, if you want the full 15 decimal places, you'll likely have to adjust axes position properties, otherwise the numbers will all get cut off.

### Categories

Find more on Orange in Help Center and File Exchange

R2022a

### Community Treasure Hunt

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

Start Hunting!

Translated by