colorbar not working?

1 view (last 30 days)
sxh
sxh on 8 Jan 2024
Moved: Dyuman Joshi on 9 Jan 2024
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 Comments
Dyuman Joshi
Dyuman Joshi on 8 Jan 2024
Moved: Dyuman Joshi on 9 Jan 2024
"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 on 8 Jan 2024
Moved: Dyuman Joshi on 9 Jan 2024
Mesh density is not the problem here..
Thanks for the answer tho :)

Sign in to comment.

Accepted Answer

Voss
Voss on 8 Jan 2024
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)

More Answers (0)

Tags

Community Treasure Hunt

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

Start Hunting!