2D line plot xy with color from z variable
Show older comments
Hi everyone,
x=TIME, y=S4, z=PRN.
I am trying to make a 2D line plot but I want it to vary in color by z variable. FYI, z variable contain a number from 1-32. Meaning that the data will be as example below:
DAY TIME PRN S4
1 0.00138888888888889 4 0.0668452919508921
1 0.00138888888888889 2 0.0559732347198194
1 0.00208333333333333 4 0.0491661308727868
1 0.00208333333333333 28 0.0379869911285429
1 0.00208333333333333 10 0.0203279197164885
1 0.00208333333333333 2 0.0556284592749356
Here is what I had try but it turns out as one color line plot because I don't know how to custom the line color based on z (PRN).
clear
clc
%___Read data___%
data = readtable("Param_365.xlsx");
%___Define variable___%
%DAY = data.DAY;
TIME = data.TIME;
S4 = data.S4;
PRN = data.PRN;
%___Plot scatter 2-D___%
plot(TIME, S4)
%___Axes properities___%
title('Time variation of the S_4 index in 2014');
datetick('x', 'HH');
xlabel('Coordinated Universal Time, UTC (hr)');
ylabel('Amplitude scintillation, S_4');
hold all
I did scatter before and it works but
clear
clc
%___Read data___%
data = readtable("Param_365.xlsx");
%___Define variable___%
%DAY = data.DAY;
TIME = data.TIME;
S4 = data.S4;
PRN = data.PRN;
%___Plot scatter 2-D___%
scatter(TIME, S4, 10, PRN,'filled')
colormap(jet(32))
caxis([1 32]);
colorbar('YTick', [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32]);
%___Axes properities___%
title('Time variation of the S_4 index in 2014');
datetick('x', 'HH');
xlabel('Coordinated Universal Time, UTC (hr)');
ylabel('Amplitude scintillation, S_4');
hold all
But my data will be more appropriate if in the form of line plot as below:

I hope anyone could help me on this because I tried few tricks but it failed. Thank you in advanced.
2 Comments
KALYAN ACHARJYA
on 9 Feb 2021
@Ann Where is the z variable here?
Answers (3)
Mathieu NOE
on 9 Feb 2021
hello
my 2 cents suggestion
%___Read data___%
data = readtable("Classeur1.xlsx");
%___Define variable___%
%DAY = data.DAY;
TIME = str2double(data.TIME);
S4 = str2double(data.S4);
PRN = data.PRN;
% %___Plot scatter 2-D___%
% scatter(TIME, S4, 10, PRN,'filled')
%
% colormap(jet(32))
% caxis([1 32]);
% colorbar('YTick', [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32]);
% %___Axes properities___%
% title('Time variation of the S_4 index in 2014');
% datetick('x', 'HH');
% xlabel('Coordinated Universal Time, UTC (hr)');
% ylabel('Amplitude scintillation, S_4');
% hold all
% // modified jet-colormap
n = length(PRN);
cd = [uint8(jet(n)*255) uint8(ones(n,1))].' %'
p = plot(TIME, S4, 'LineWidth',4);
title('Time variation of the S_4 index in 2014');
datetick('x', 'HH');
xlabel('Coordinated Universal Time, UTC (hr)');
ylabel('Amplitude scintillation, S_4');
caxis([1 32]);
colormap(jet(32));
cbv=colorbar('v');
set(cbv,'YTick',[1:32],'TickLabels',cellstr(num2str((1:32)')))
drawnow
set(p.Edge, 'ColorBinding','interpolated', 'ColorData',cd)
6 Comments
Mathieu NOE
on 9 Feb 2021
hello
maybe I misunderstood the problem
do you intend to plot multiple lines and one line = one color
or , as I guessed from the first input data, you wanted to plot one single line and the sgements between the points must have gradually changing color according to a z value ?
NB the initial data given only shows one x, y pair for one PRN value so I cannot draw a line with that single point
DAY TIME PRN S4
1 0.00138888888888889 4 0.0668452919508921
1 0.00138888888888889 2 0.0559732347198194
1 0.00208333333333333 4 0.0491661308727868
1 0.00208333333333333 28 0.0379869911285429
1 0.00208333333333333 10 0.0203279197164885
1 0.00208333333333333 2 0.0556284592749356
maybe you should share the actual data to display (from fig above)
Mathieu NOE
on 10 Feb 2021
hello
see my code below ; I plotted each day separatly (1 figure per day) otherwise the plot is unreadable
this is the visual result :

but I limited myself to the first 7 days otherwise my matlab would take forever to plot + 300 figures;
%___Read data___%
data = readtable("Param_365.xlsx");
%___Define variable___%
DAY = data.DAY;
TIME = data.TIME;
S4 = data.S4;
PRN = data.PRN;
N = 32; % color axis max level (colorbar)
for ci = 1: 7 %max(DAY)
ind =find(DAY == ci);
x = TIME(ind); % to get it in hours
y = S4(ind);
z = PRN(ind);
figure(ci),
h = plot(x,y,'-'); % capture the line handle when you plot it
datetick('x', 'HH');
xlabel('Coordinated Universal Time, UTC (hr)');
ylabel('Amplitude scintillation, S_4');
title([' Day : ' num2str(ci) ]);
caxis([0 N]);
cd = colormap(jet(N+1)); % take your pick (doc colormap)
cd = interp1(linspace(0,N,length(cd)),cd,z); % map color to z values
cd = uint8(cd'*255); % need a 4xN uint8 array
cd(4,:) = 255; % last column is transparency
cbv=colorbar('v');
set(cbv,'YTick',[0:N],'TickLabels',cellstr(num2str((0:N)')))
drawnow
set(h.Edge,'ColorBinding','interpolated','ColorData',cd)
end
Ann
on 11 Feb 2021
Mathieu NOE
on 11 Feb 2021
Glad it helps !
I don't have that much merit , simply used the info I found from Yair's undocumented-matlab site
now maybe you could do this plot for a longer period in one graph but this needs first to do some averaging vs time (1 hour basis ? );
Iuliu Ardelean
on 9 Feb 2021
Try this:
x = 1:100;
y = rand(1, 100);
z = randsample(32, 100, true);
figure
hold on
for i = 1:32
plot(x(z==i), y(z==i),'-') % you can assign colors here as you want
% you can also try different markers maybe, if lines are no good?
end
hold off
4 Comments
Iuliu Ardelean
on 10 Feb 2021
Hey Ann
I see -- I would probably try using a legend
e.g.
Iuliu Ardelean
on 10 Feb 2021
Edited: Iuliu Ardelean
on 10 Feb 2021
Or did you mean, how are you going to find the colors from inside S4 subplot?
If that's what you meant then it's pretty easy. Type this in command window:
>> jet(32)
So, your code will look like this:
x = 1:100;
y = rand(1, 100);
z = randsample(32, 100, true);
colors = jet(32);
figure
hold on
for i = 1:32
plot(x(z==i), y(z==i), 'LineWidth',2, 'Color', colors(i,:)) % you can assign colors here as you want
% you can also try different markers maybe, if lines are no good?
end
hold off
And your image will look like this:

Ann
on 11 Feb 2021
Walter Roberson
on 11 Feb 2021
0 votes
There are three approaches:
- use a surface plot and control edge color
- use a patch() and control edge color.
- use some advanced and obscure internal undocumented properties
There are a couple of File Exchange contribution you can use. One of them is https://www.mathworks.com/matlabcentral/fileexchange/19476-colored-line-or-scatter-plot
Yair's undocumented-matlab site shows how to use the internal undocumented properties.
Categories
Find more on Data Distribution Plots 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!



