How to plot the maximum regions given several functions in a 2D plot?
이 질문을 팔로우합니다.
- 팔로우하는 게시물 피드에서 업데이트를 확인할 수 있습니다.
- 정보 수신 기본 설정에 따라 이메일을 받을 수 있습니다.
오류 발생
페이지가 변경되었기 때문에 동작을 완료할 수 없습니다. 업데이트된 상태를 보려면 페이지를 다시 불러오십시오.
이전 댓글 표시
I have three functions A(i,j), B(i,j), and C(i,j) which are functions of i and j , where a<i<b and a<j<b.
I want to distinguish and plot regions where each of these functions has the maximum value. Can someone please help me get started?

채택된 답변
idx = max(cat(3, A, B, C), [], 3);
cmap = [.3 0 0; %light red
0 .5 0; %light green
0 0 .8]; %blue
pcolor(idx); colormap(cmap)
xlabel('i'); ylabel('j')
fake(1) = plot(nan, nan, 'DisplayName', 'A', 'Color', cmap(1));
fake(2) = plot(nan, nan, 'DisplayName', 'B', 'Color', cmap(2));
fake(3) = plot(nan, nan, 'DisplayName', 'C', 'Color', cmap(3));
legend(fake)
댓글 수: 10
Thank you!!! So fast!
As I am trying to learn, may I ask why plot has nan in two of its arguments?
pcolor() creates a surface plot internally.
If you were to create an automatic legend for a surface plot, there would be exactly one legend entry for the entire surface plot. We want to created one legend entry for each of the functions we are dealing with.
MATLAB has no direct way of saying, "Add additional legend entries with these properties."
So, instead we use a combination of tricks:
- We create one extra graphics object for each legend entry we wish to add
- We pass only the handles for those exttra graphics objects to legend(). When you pass legend() a series of graphics objects, it will create legend entries only for the handles you pass in -- in particular in this case, it would not create a legend entry for the surface created by pcolor, just for the plot handles stored in the fake vector
- We give Nan coordinates for the extra graphics objects that we are creating just for the purpose of creating legend entries. Graphics objects such as lines and surfaces are only visible if they have some finite coordinates (and in some cases at least multiple adjacent finite coordinates.) You could use inf or -inf for the coordinates instead of NaN, but NaN tends to be used as a matter of informal convention (using NaN instead of -inf or inf makes it easier to do some calculations on the data.)
- We do not simply use graphics objects with regular finite coordinates but set Visible off for them, because the finite coordinates can influence what MATLAB picks for the tick positions and how much data is to be visible; non-finite coordinates are ignored for the purpose of calculating such things
So we plot() NaN to create invisible graphics objects that do not disturb other graphics properties, and we do that because the invisible graphics objects still get legend entries.
Thank you very much! Very detailed and informative. I learned a lot. :)
Bruno Luong
2023년 7월 21일
편집: Bruno Luong
2023년 7월 21일
Have you tested the Walter's code? It looks like there are few bugs, it sould be this
A=rand(20);
B=rand(20);
C=rand(20);
[~,idx] = max(cat(3, A, B, C), [], 3); % BUG
cmap = [.3 0 0; %light red
0 .5 0; %light green
0 0 .8]; %blue
pcolor(idx); colormap(cmap);
xlabel('i'); ylabel('j')
hold on % Add
fake(1) = plot(nan, nan, 'DisplayName', 'A', 'Color', cmap(1,:)); % BUG
fake(2) = plot(nan, nan, 'DisplayName', 'B', 'Color', cmap(2,:)); % BUG
fake(3) = plot(nan, nan, 'DisplayName', 'C', 'Color', cmap(3,:)); % BUG
legend(fake)

Oh Thanks. I did try out the code and it did give me an error.
Thank you for fixing it.
Bruno Luong
2023년 7월 21일
편집: Bruno Luong
2023년 7월 21일
Notice that the pcolor plot 19x19 rectangular colored patches, but the data (idx) is 20 x 20.
I don't know how pcolor does the reduction (I rarely use it)
EDIT the color patch are average of the 4 surrounding values, so it not very directly represents the max index.
I also never use pcolor for just that reason. I'd recommend using image or imshow instead.
True, imagesc() would be better than pcolor() here.
It is still not clear to me how pcolor graphical result has 3 colors, and not more as I assume it takes the average.
A=rand(5);
B=rand(5);
C=rand(5);
[~,idx] = max(cat(3, A, B, C), [], 3)
idx = 5×5
3 2 3 2 1
2 3 2 1 1
2 3 3 1 3
1 2 3 1 1
1 1 2 3 2
idx3 = cat(3,idx(1:end-1,1:end-1), ...
idx(1:end-1,2:end), ...
idx(2:end,2:end), ...
idx(2:end,1:end-1));
mean(idx3,3)
ans = 4×4
2.5000 2.5000 2.0000 1.2500
2.5000 2.7500 1.7500 1.5000
2.0000 2.7500 2.0000 1.5000
1.2500 2.0000 2.2500 1.7500
min(idx3,[],3)
ans = 4×4
2 2 1 1
2 2 1 1
1 2 1 1
1 1 1 1
max(idx3,[],3)
ans = 4×4
3 3 3 2
3 3 3 3
3 3 3 3
2 3 3 3
pcolor(idx);

It must have some odd algorithm to decide the color.
But never mind I rarely (never) use pcolor.
Thank you all for the feedback on pcolor. Appreciate it. :)
추가 답변 (0개)
카테고리
도움말 센터 및 File Exchange에서 Data Distribution Plots에 대해 자세히 알아보기
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!웹사이트 선택
번역된 콘텐츠를 보고 지역별 이벤트와 혜택을 살펴보려면 웹사이트를 선택하십시오. 현재 계신 지역에 따라 다음 웹사이트를 권장합니다:
또한 다음 목록에서 웹사이트를 선택하실 수도 있습니다.
사이트 성능 최적화 방법
최고의 사이트 성능을 위해 중국 사이트(중국어 또는 영어)를 선택하십시오. 현재 계신 지역에서는 다른 국가의 MathWorks 사이트 방문이 최적화되지 않았습니다.
미주
- América Latina (Español)
- Canada (English)
- United States (English)
유럽
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
