필터 지우기
필터 지우기

Plotting a figure, with the color of the plot based on a secondary variable

조회 수: 1 (최근 30일)
Hello,
I'd like to plot a dataset against an angle theta, where the dataset is a 91x2 double which is inside of a struc, with the first column being the datapoint and the second column being a number corresponding to the datapoints nature. For my case I'm examing the failure of a composite lamina and the number in the second column corresponds to failure due to tension/shear/compression. The number in the first column is the strength of the lamina at failure
This is being plotted against an angle theta ranging from 0 to 90 degrees for 91 points total.
I want the plot line to be red when the second column of my dataset is equal to a value, lets say 115, and blue when the second column of my dataset equals 116.
This is part of a subplot where r and c are 5 and 4 respectively.
subplot(r,c,13)
hold on
for i = 1:91
if fail.sigmault(i,2) == 115
plot(theta,fail.sigmault(i,1),'r')
elseif fail.sigmault(i,2) == 116
plot(theta,fail.sigmault(i,1),'b')
end
end
When running the above, I get an empty graph and I'm not sure whats wrong here, I'd appreciate the help.

채택된 답변

Walter Roberson
Walter Roberson 2023년 4월 24일
plot() does not have the ability to plot an individual line with different colors for different vertices. I suggest
plot(theta, fail.sigmault(:,1));
hold on
markersize = 15;
markercolor = repmat([1 0 0], numel(theta), 1);
mask = fail.sigmault(:,2) == 116;
markercolor(mask,:) = repmat([0 0 1], nnz(mask), 1);
scatter(theta, fail.sigmault(:,1), markersize, markercolor, 'fill');
hold off
This particular code assumes the marker color should be red except where it is blue. The code would be slightly different for the case where you wanted red for 115, blue for 116, and no marker for other colors.
  댓글 수: 3
Walter Roberson
Walter Roberson 2023년 4월 24일
This version will only mark the vertices that have one of the special values. It would not be difficult to have a default color for the entries that do not match the ones in the list.
plot(theta, fail.sigmault(:,1));
hold on
markersize = 15;
special_values = [99, 115, 116];
colortable = [0 1 0; %green
1 0 0; %red
0 0 1]; %blue
[markthis, colidx] = ismember(fail.sigmault(:,2), special_values);
nummatches = nnz(markthis);
markercolor = colortable(colidx(markthis), :);
scatter(theta(markthis), fail.sigmault(markthis,1), markersize, markercolor, 'fill');
hold off

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Axis Labels에 대해 자세히 알아보기

제품


릴리스

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by