colorbar not working?

조회 수: 4 (최근 30일)
sxh
sxh 2024년 1월 8일
이동: Dyuman Joshi 2024년 1월 9일
clc;
close all;
clear;
x = -1:2/511:1;
[X,Y] = meshgrid(x);
R = sqrt(X.^2 + Y.^2);
Phi = atan2(X,Y);
Z = zeros(512,512);
index1 = find(Phi<pi/20 & Phi>-pi/20);
Z(index1)=6;
index2 = find(R<0.0142);
Z(index2)=50;
index3 = find(R>0.425);
Z(index3)=0;
surf(X,Y,Z)
colorbar
%view(2)
Any idea why the plot comes up all black?
Thanks
S
  댓글 수: 2
Dyuman Joshi
Dyuman Joshi 2024년 1월 8일
이동: Dyuman Joshi 2024년 1월 9일
"Any idea why the plot comes up all black?"
Because your mesh is extremely dense. I have increased the mesh density by 5 times, so to can see the corresponding result.
However, if you want a refined surface/mesh, you can hide the grid lines, as @Voss has shown.
Also, note that you should not hard code values, as that is likely to cause issues - In this case while defining Z. And you can use logical indexing instead of find(), as it is more efficient.
x = -1:2*5/511:1;
[X,Y] = meshgrid(x);
R = sqrt(X.^2 + Y.^2);
Phi = atan2(X,Y);
%Define Z according to the size of R, instead of hard-coding
Z = zeros(size(R));
%logical indexing
index1 = (Phi<pi/20 & Phi>-pi/20);
Z(index1)=6;
index2 = (R<0.0142);
Z(index2)=50;
index3 = (R>0.425);
Z(index3)=0;
surf(X,Y,Z)
colorbar
%view(2)
sxh
sxh 2024년 1월 8일
이동: Dyuman Joshi 2024년 1월 9일
Mesh density is not the problem here..
Thanks for the answer tho :)

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

채택된 답변

Voss
Voss 2024년 1월 8일
The black you see is grid lines of the surface. You can turn them off by setting the surface EdgeColor property to 'none'.
clc;
close all;
clear;
x = -1:2/511:1;
[X,Y] = meshgrid(x);
R = sqrt(X.^2 + Y.^2);
Phi = atan2(X,Y);
Z = zeros(512,512);
index1 = find(Phi<pi/20 & Phi>-pi/20);
Z(index1)=6;
index2 = find(R<0.0142);
Z(index2)=50;
index3 = find(R>0.425);
Z(index3)=0;
surf(X,Y,Z,'EdgeColor','none')
colorbar
% view(2)

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by