how do i plot 3 sets of data using different colors based on one data set
显示 更早的评论
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
采纳的回答
更多回答(5 个)
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 个评论
Image Analyst
2022-7-19
@Adam Jurhs's "Answer" moved here:
hi Image Analyst,
Using your code my "real" data only gives 2 data colors, but there are 7 unique values in data2. notice that your plot only gives 3 unique colors but there are 4 unique values in data2. can you please try using the following data2 which has 7 unique values like my "real" data does:
data2=[34,189,34,56,24,42,10087,6,56,34,189,189,10087,24,34,6,189,56,189,189];
Thanks!
Sorry, but with the data2 you gave, there are only 4 unique colors, not 7. And my code makes 4 colors, not 3. Here is proof:
data2=[34,189,34,56,24,56,189,34,56,34,189,189,189,24,34,189,189,56,189,189];
fprintf('The number of unique colors in data2 is %d.\n', length(unique(data2)))
However two of the dark blues are close in color. Let's try it with random colors.
% 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 = rand(max(data2), 3);
% 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')
Image Analyst
2022-7-19
@Adam Jurhs's "Answer" moved here:
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
Try this to get 7 unique colors:
% Demo by Image Analyst
% Initialization Steps.
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
clear; % Erase all existing variables. Or clearvars if you want.
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
fontSize = 20;
% 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,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)))
numUniqueColors = length(unique(data2));
% Assign an index, or group number to each integer so we know which color number it is.
colorIndex = findgroups(data2)
% Make a colormap
cmap = turbo(numUniqueColors);
% 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.
thisColorIndex = colorIndex(k);
thisColor = cmap(thisColorIndex, :);
% 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')
Adam Jurhs
2022-7-21
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
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:
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
2022-7-19
编辑:Adam Jurhs
2022-7-19
0 个投票
2 个评论
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.
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" ;
4 个评论
Image Analyst
2022-7-19
Who was this "Answer" supposed to actually be a "Comment" to?
Adam Jurhs
2022-7-21
编辑:Adam Jurhs
2022-7-21
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
2022-7-22
类别
在 帮助中心 和 File Exchange 中查找有关 Historical Contests 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!




