how do i plot 3 sets of data using different colors based on one data set

21 ビュー (過去 30 日間)
Adam Jurhs
Adam Jurhs 2022 年 7 月 18 日
コメント済み: Adam Jurhs 2022 年 7 月 22 日
hi,
i read a csv file into matlab using csvread and i assign each column (vector) of data with a specific name like time, data1, data2 (data1 and data2 are integers, time data is not). i know how to do a simple plot(time,data1,'.') but what i would like to do is "colorize" data1 according to the values in data2.
thanks for your help!
Todd

採用された回答

Adam Jurhs
Adam Jurhs 2022 年 7 月 19 日
編集済み: Adam Jurhs 2022 年 7 月 19 日
Image Analyst, i'm confused. in your plot i see 4 unique colors. i don't know how to apply your cmap line of code to my data2. can you please re-write that line using the array
data2=[34,189,34,56,24,42,10087,6,56,34,189,189,10087,24,34,6,189,56,189,189];
so that there are 7 unique colors
  4 件のコメント
Image Analyst
Image Analyst 2022 年 7 月 20 日
Did you see my answer that had 7 unique colors?
Adam Jurhs
Adam Jurhs 2022 年 7 月 21 日
yep and that solved the problem, thanks!!!

サインインしてコメントする。

その他の回答 (5 件)

Image Analyst
Image Analyst 2022 年 7 月 19 日
Try this:
% Assign variables.
t=[0, 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19] ;
data1=[2,3,1,5,8,12,8,5,3,8,8,15,8,5,9,12,3,2,5,5] ;
data2=[34,189,34,56,24,56,189,34,56,34,189,189,189,24,34,189,189,56,189,189];
% Make a colormap
cmap = jet(max(data2));
% Let's say data2 is the row of the colormap which represents the color for
% the marker that we want to draw data 1 in.
for k = 1 : numel(data1)
% Get a color (1x3 array) from the row corresponding to data2(k).
% We will plot data1 with that color.
thisColor = cmap(data2(k), :);
% Plot a marker for this point of data 1 in this color.
plot(t(k), data1(k), '.', 'Color', thisColor, 'MarkerSize', 50); % Adjust marker size as needed.
hold on;
end
grid on;
xlabel('Time')
ylabel('Signal')
  6 件のコメント
Adam Jurhs
Adam Jurhs 2022 年 7 月 21 日
this solution does answer the question, what i don't know is how to mark it as so
Image Analyst
Image Analyst 2022 年 7 月 21 日
There is a link beside each answer. You saw it and clicked the one next to your answer instead of the one next to mine. You can still click the "Vote" icon to award any answerers "Reputation points". You can accept only one answer (and you can unaccept them and accept different ones if you want) though you can "Vote" for as many as you want. So that's how it works. Anyway, glad my solution works, and note it works for however many unique values you have in data2 -- it doesn't have to be 4 or 7, it can be anything so it's really general.

サインインしてコメントする。


Image Analyst
Image Analyst 2022 年 7 月 18 日
Don't call a variable time since I think that is a built in function.
So, try this:
% Read in data
data = readmatrix(fileName);
% Get variables from columns.
t = data(:, 1); % Time is column 1
data1 = data(:, 2); % data1 is in column 2.
data2 = data(:, 3); % data1 is in column 3.
% Make a colormap
cmap = jet(length(data1));
% Let's say data2 is the row of the colormap which represents the color for
% the marker that we want to draw data 1 in.
for k = 1 : numel(data1)
% Get a color (1x3 array) from the row corresponding to data2(k).
% We will plot data1 with that color.
thisColor = cmap(data2(k), :);
% Plot a marker for this point of data 1 in this color.
plot(t(k), data1(k), '.', 'Color', thisColor, 'MarkerSize', 20); % Adjust marker size as needed.
hold on;
end
grid on;
xlabel('Time')
ylabel('Signal')
If you have any more questions, then attach your data and code to read it in with the paperclip icon after you read this:

Abderrahim. B
Abderrahim. B 2022 年 7 月 18 日
編集済み: Abderrahim. B 2022 年 7 月 18 日
Hi!
Since you did not share your data to test .. I am creating some dummy !
clear
close all
% Create some sample data:
time = 0:99; % X data
data1 = 2*time + 0.1; % Y data
data2 = 1:100; % "Color" data
% Plot data:
figure
surf([time(:) time(:)], [data1(:) data1(:)], [data2(:) data2(:)], ...
'EdgeColor', 'interp', ...
'LineWidth', 2);
view(2);
% Annotate the plot
xlabel("Time")
ylabel("Data1")
% Add a colorbar
c = colorbar;
c.Label.String = "Data2" ;
Hope this helps

Adam Jurhs
Adam Jurhs 2022 年 7 月 19 日
編集済み: Adam Jurhs 2022 年 7 月 19 日
hi Abderrahim,
your 1st code runs, and i think it is on the right track, but it only plots out the first set of data assigning only one color to that first set. the actual data exist on a different system and i'm not able to transfer that data from one system to another, so here's some "dummy" data
t=[0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20]
data1=[2,3,1,5,8,12,8,5,3,8,8,15,8,5,9,12,3,2,5,5]
data2=[34,189,34,56,24,56,189,34,56,34,189,189,189,24,34,189,189,56,189,189]
i want to plot t vs. data1 and "colorize" data1 according to the data in data 2. so the first data point in data1 (the number 2) would get a color assigned to it because it is associated with the number 34 in data2. then the number 3 in data1 would get a different color because it is associated with the number 189 in data2, then the number 1 in data1 would get the same color associated with the number 34 from data2, then the number 5 in data1 would get a different color because it is associated with the number 56 in data2, and the pattern repeats. so although the values in the data1 vector differ at each position in the vector, they get "colorized" according to the value of the data point in data2
thank you so much for your help!
Todd
  2 件のコメント
Image Analyst
Image Analyst 2022 年 7 月 19 日
Explain what t to use, since t is longer than either data1 or data2. So you want the first 20 elements, last 20 elements, or do you want to lengthen data1 or data2 to be the same length as t? Explain why they're different lengths.
Abderrahim. B
Abderrahim. B 2022 年 7 月 19 日
編集済み: Abderrahim. B 2022 年 7 月 19 日
Maybe scatter will work for you
clear
time =[0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19] ;
data1=[2,3,1,5,8,12,8,5,3,8,8,15,8,5,9,12,3,2,5,5] ;
data2=[34,189,34,56,24,56,189,34,56,34,189,189,189,24,34,189,189,56,189,189] ;
% Plot data:
figure
sc = scatter(time, data1, [], data2, 'filled') ;
sc.SizeData = 100;
% Annotate the plot
xlabel("Time")
ylabel("Data1")
% Add a colorbar
c = colorbar;
c.Label.String = "Data2" ;

サインインしてコメントする。


Adam Jurhs
Adam Jurhs 2022 年 7 月 19 日
編集済み: Image Analyst 2022 年 7 月 19 日
I only get 2 colors on my plot? in my "real" data I have 7 distinct colors
Notice on your plot you only get 3 colors but there are four distinct color assignments the numbers 34, 189, 56, and 24.
Let's change data 2 to have 7 color assignments so that now equals
data2=[34,189,34,56,24,42,10087,6,56,34,189,189,10087,24,34,6,189,56,189,189];
fprintf('The number of unique colors in data2 is %d.\n', length(unique(data2)))
The number of unique colors in data2 is 7.
  4 件のコメント
Image Analyst
Image Analyst 2022 年 7 月 21 日
編集済み: Image Analyst 2022 年 7 月 21 日
Here is how it works according to my understanding:
Next to each answer there should be a link that says "Accept this answer" and some icons like a thumbs up that says "Vote". If you Accept an answer, it will move to the top and on the main screen it will put a green box around the number of answers meaning that one of the answers has been accepted. You can only accept one answer. Only the original poster can accept an answer. The original poster can also unaccept an answer. Moderators/editors above some level can also unaccept an answer but only after a week.
You can "Vote" for any number of answers. Anybody, not only the original poster, can Vote for an answer.
You can both Accept and Vote for a single answer if you want.
Accepting and Voting for an answer both give the answere 2 reputation points.
Adam Jurhs
Adam Jurhs 2022 年 7 月 22 日
sounds mighty complicated for to me.
i don't see the "Accept this answer" next to your answer or i would have checked it, but i can "Vote" for it so i will

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeOrange についてさらに検索

タグ

製品


リリース

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by