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

9 次查看(过去 30 天)
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?

回答(1 个)

Srivardhan Gadila
Srivardhan Gadila 2020-10-2
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

类别

Help CenterFile Exchange 中查找有关 Orange 的更多信息

产品


版本

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by