Color Code Plotted Data According to Value in Un-Plotted Column

28 views (last 30 days)
Camille Woicekowski
Camille Woicekowski on 26 Sep 2020
I have a 53x3 matrix of data and I would like to plot the 2nd and 3rd columns color-coded with a colormap according to the value in the first column. The first column has 13 unique values and each unique value appears in varying frequency in the rows.
For example, the matrix
data=[100,4.1,3.2;100,1.9,2.4;101,3.5,2.1;102,5.4,3.2;102,7.1,8.2;102,5.3,2.3]
would plot six lines in three different colors, two lines (in color 1) corresponding to 100, one line (in color 2) corresponding to 101, and three lines (in color 3) corresponding to 102. Again, values in the first column would not be plotted but would appear in a legend.
What is the best way to go about doing this?

Answers (1)

Srivardhan Gadila
Srivardhan Gadila on 2 Oct 2020
Refer to the documentation of plot & legend.
Also the following code may help you (Note that you may have to make changes according to your usage):
data=[100,4.1,3.2;100,1.9,2.4;101,3.5,2.1;102,5.4,3.2;102,7.1,8.2;102,5.3,2.3];
sz = size(data);
figure
for i = 1:sz(1)
plot(data(i,2),data(i,3),'-o','Color',getColor(data(i,1)))
hold on
end
hold off
legend(num2str(data(:,1)))
function color = getColor(data)
switch data
case 100
color = [1 0 0];
case 101
color = [0 1 0];
case 102
color = [0 0 1];
end
end

Products


Release

R2020a

Community Treasure Hunt

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

Start Hunting!