Home > Mobile >  How to color multiple lines based on their value?
How to color multiple lines based on their value?

Time:04-05

I produced a plot that contains 50 curves and each of them corresponds to a specific value of a parameter called "Jacobi constant", so I have 50 values of jacobi constant stored in array:

0.300089684137397E 01
0.300089042922377E 01
0.300088401572195E 01
0.300087759725008E 01
0.300087115690923E 01
0.300086474534639E 01
0.300085827898499E 01
0.300085188022182E 01
0.300084551820425E 01
0.300083908717519E 01
0.300083267112291E 01
0.300082621633541E 01
0.300081978306838E 01
0.300081343238767E 01
0.300080689895638E 01
0.300080053945119E 01
0.300079405784651E 01
0.300078765677301E 01
0.300078137552897E 01
0.300077485059859E 01
0.300076849514638E 01
0.300076213310428E 01
0.300075556036120E 01
0.300074923392480E 01
0.300074272105419E 01
0.300073626366797E 01
0.300072988110949E 01
0.300072359251233E 01
0.300071715639932E 01
0.300071057435589E 01
0.300070439186567E 01
0.300069781324123E 01
0.300069138951169E 01
0.300068513892704E 01
0.300067849690606E 01
0.300067204496398E 01
0.300066580026063E 01
0.300065917265777E 01
0.300065276941195E 01
0.300064660586285E 01
0.300064007078422E 01
0.300063379168696E 01
0.300062714644242E 01
0.300062077361542E 01
0.300061436361235E 01
0.300060792230082E 01
0.300060145569814E 01
0.300059496993140E 01
0.300058879634480E 01
0.300058229105101E 01

I want to use a colormap to color my curves and then show in a lateral bar the legend that show the numerical values corresponding to each color of orbit. enter image description here

By considering my example image, I would want to add the array of constants in the lateral bar and then to color each curve according the lateral bar.

% Family of 50 planar Lyapunov orbits around L1 in dimensionless unit
fig = figure;

for k1 = 1:(numel(files_L1_L2_Ly_prop)-2)
    plot([Ly_orb_filt(1).prop(k1).orbits.x],[Ly_orb_filt(1).prop(k1).orbits.y],...
        "Color",my_green*1.1); hold on  %"Color",my_green*1.1
    colorbar()
end
axis equal


% 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,0-0.001,'$Earth   Moon$','Interpreter',"latex"); 

grid on;
xlabel('$x$','interpreter','latex','fontsize',12);
ylabel('$y$','interpreter','latex','FontSize',12);

How can I color each line based on its Jacobi constant value?

CodePudding user response:

You can use any colour map to produce a series of RGB-triplets for the plotting routines to read (Or create an m-by-3 matrix with elements between 0 and 1 yourself):

n = 10;  % Plot 10 lines
x = 1:15;
colour_map = jet(n);  % Get colours. parula, hsv, hot etc.

figure;
hold on
for ii = 1:n
    % Plot each line individually
    plot(x, x ii, 'Color', colour_map(ii, :))
end

colorbar  % Show the colour bar.

Which on R2007b produces:

enter image description here

Note that indexing into a colour map will produce linearly spaced colours, thus you'll need to either interpolate or calculate a lot to get the specific ones you need. Then you can (need to?) modify the resulting colour bar's labels by hand to reflect your input values. I'd simply use parula(50), treat its indices as linspace(jacobi(1), jacobi(end), 50) and then my_colour = interp1(linspace(jacobi(1), jacobi(end), 50), parula(50), jacobi).

So in your code, rather than using "Color",my_green*1.1 for each line, use "Color",my_colour(kl,:), where my_colour is whatever series of RGB triplets you have defined.

  • Related