2D line plot xy with color from z variable

72 views (last 30 days)
Ann
Ann on 9 Feb 2021
Commented: Mathieu NOE on 11 Feb 2021
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.

Answers (3)

Mathieu NOE
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
Ann
Ann on 11 Feb 2021
@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!
Mathieu NOE
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 ? );

Sign in to comment.


Iuliu Ardelean
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
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
Ann on 11 Feb 2021
@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.


Walter Roberson
Walter Roberson on 11 Feb 2021
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

Find more on 2-D and 3-D Plots in Help Center and File Exchange

Products


Release

R2020b

Community Treasure Hunt

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

Start Hunting!