2D line plot xy with color from z variable
38 次查看(过去 30 天)
显示 更早的评论
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:
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/513867/image.png)
I hope anyone could help me on this because I tried few tricks but it failed. Thank you in advanced.
2 个评论
回答(3 个)
Mathieu NOE
2021-2-9
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 个评论
Mathieu NOE
2021-2-11
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
2021-2-9
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 个评论
Iuliu Ardelean
2021-2-10
编辑:Iuliu Ardelean
2021-2-10
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:
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/515437/image.png)
Walter Roberson
2021-2-11
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.
0 个评论
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!