colorbar not working?

3 次查看(过去 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 中查找有关 Red 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by