2D line plot xy with color from z variable

Hi everyone,
I have an abundance of data in excel (556873 rows). Data can be get here Data 365 days .
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

@Ann Where is the z variable here?
Ann
Ann on 9 Feb 2021
Edited: Ann on 9 Feb 2021
Hi @KALYAN ACHARJYA, Z refer to PRN as stated above.

Sign in to comment.

Answers (3)

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

Ann
Ann on 9 Feb 2021
Edited: Ann on 9 Feb 2021
Hi @Mathieu NOE, thanks for the response. The graphically looks good but It turns out in one color as below and not as expected in vary colors as the colorbar.
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)
Ann
Ann on 9 Feb 2021
Edited: Ann on 9 Feb 2021
Basically there will be too many lines but I want it to vary in color by the variable Z (PRN).
The data file is big you may donwload it here Data 365 days .
I am expecting the graph is going to be something like in S_4 graph below (ignore the other subplots):
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
@Mathieu NOE Woah! Yours is perfectly perfect for my daily observations. Maybe for my question it is kinda impossible to do a year observation in one graph. Thanks for your help, bud!
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 ? );

Sign in to comment.

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

Hi @Iuliu Ardelean , thank you for your attempt to answer. I did changes on the code:
clear
clc
data = readtable("Param_365.xlsx");
x = data.TIME;
y = data.S4;
z = data.PRN;
figure
hold on
for i = 1:32
plot(x(z==i), y(z==i), 'LineWidth',2)
end
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 off
But it resulting as below which is looks good. Basically, how am I going to make the line in the color variation as in colorbar?
Expected line in this color variation (ignore the subplot, look at the S_4 plot):
Hey Ann
I see -- I would probably try using a legend
e.g.
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:
@Iuliu Ardelean , thanks for your assistance! Your code works with my real data here: Data 365 days . But probably because of the data is too much so it looks inconvenient in the form of graph. But I sincerely thanks for the help.

Sign in to comment.

There are three approaches:
  1. use a surface plot and control edge color
  2. use a patch() and control edge color.
  3. 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

Products

Release

R2020b

Asked:

Ann
on 9 Feb 2021

Commented:

on 11 Feb 2021

Community Treasure Hunt

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

Start Hunting!