필터 지우기
필터 지우기

How to plot points with corresponding colour

조회 수: 1 (최근 30일)
Hannah
Hannah 2024년 4월 23일
댓글: Hannah 2024년 4월 26일
Given I have an excel data where a1 is the first row of data, a2 is the second row of data and b is the corresponding label. B has either blue or red. When I plot it plots it all as a scatter plot but in blue. I want to make the points red when b says red for that label. How do i do this??

채택된 답변

Dyuman Joshi
Dyuman Joshi 2024년 4월 23일
(Assuming there are only red and blue colors for the plot) Use logical indexing -
%Check which elements of B are red
idx = strcmp(B, 'red');
f = figure;
hold on
%plot data with red color
scatter(a1(idx), a2(idx), [], 'r')
%plot data with blue color
scatter(a2(~idx), a2(~idx), [], 'b')
hold off
  댓글 수: 1
Hannah
Hannah 2024년 4월 26일
I tried that and it is not really working properly. I do not know if its my file that is the problem.
After uploading my excel dataset, this is how my codes look like
Dataset.Y(strcmp(Dataset.Y, 'red')) = {'-1'}
Dataset.Y(strcmp(Dataset.Y, 'blue')) = {'1'}
Dataset.Y = str2double(Dataset.Y)
data = table2array(Dataset)
%I think I am going wrong when plotting it or when turning the data into a
%matrix asthe number of rows do not match the height of the table

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 2-D and 3-D Plots에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by