colorbar scale is not correct when using isosurface and patch plots

Why is the color bar here giving me the values of the transparency "alpha" and not my data values that I am plotting?
Code:
Nx = 128;
Ny = 128;
Nz = 128;
Lx =128;
Ly = 128;
Lz = 128;
xi = (0:Nx-1)/Nx*2*pi;
xi_x = 2*pi/Lx;
x = xi/xi_x;
yi = (0:Ny-1)/Ny*2*pi;
yi_y = 2*pi/Ly;
y = yi/yi_y;
zi = (0:Nz-1)/Nz*2*pi;
zi_z = 2*pi/Lz;
z = zi/zi_z;
dx = Lx/Nx;
dy = Ly/Ny;
dz = Lz/Nz;
[X,Y,Z] = meshgrid(x,y,z);
%Sizing
fontSize = 16;
frames2Plot = [1 2 3 4 5 6];
numF = length(frames2Plot);
mB = 0.12;
mT = 0.05;
mL = 0.9;
sV = 0.005;
wC = 0.02;
sC = 0.02;
mC = 0.03;
mR = wC + sC + mC;
sH = 0.01;
sV = 0.01;
W = (1 - mL - mR - (numF/2-1)*sH) / (numF/2);
H = (1 - mT - mB - sV) / 2;
filelocation1 = 'C:\Users\Data\';
n = reshape(readmatrix([filelocation1,'Data',num2str(100),'.txt']),128,128,128);
Error using readmatrix
Unable to find or open 'C:\Users\Data\Data100.txt'. Check the path and filename or file permissions.
f = figure('Position',[14 63 851 600],'Renderer','Painters');
i = 1.5e11;
surf1 = isosurface(X,Y,Z,n,i);
p1 = patch(surf1);
isonormals(X,Y,Z,n,p1);
set(p1,'FaceColor','black','EdgeColor','none','FaceAlpha',0.1);
daspect([1,1,1])
view(3);
camlight; lighting gouraud
i = 2.5e11;
surf2 = isosurface(X,Y,Z,n,i);
p2 = patch(surf2);
isonormals(X,Y,Z,n,p2);
set(p2,'FaceColor','#7E2F8E','EdgeColor','none','FaceAlpha',0.2);
i = 3.5e11;
surf3 = isosurface(X,Y,Z,n,i);
p3 = patch(surf3);
isonormals(X,Y,Z,n,p3);
set(p3,'FaceColor','red','EdgeColor','none','FaceAlpha',0.3);
i = 4.5e11;
surf4 = isosurface(X,Y,Z,n,i);
p4 = patch(surf4);
isonormals(X,Y,Z,n,p4);
set(p4,'FaceColor','yellow','EdgeColor','none','FaceAlpha',1);
grid on;
xlabel('x(m)'); ylabel('y(m)'); zlabel('z(m)');
c = colorbar;
for i = 1:numF
hAxis(i).Position = [mL, (numF - i) * (sV + H) + mB , W, H];
end
c.Position = [mL + W + sC, mB, wC, 1 - mB - mT];
c.Label.String = 'values';
The color bar should be showing the value i and not "alpha", is there a way to fix this?? Thanks

3 Comments

can you provide the file Data100.txt please
I attached a zip file with one text file data. Now, if I don't use patch, just isosurface then my colorbar values are correct. Why is that?
figure;
i = 2.5e11;
isosurface(X,Y,Z,n,i);
alpha(0.0008)
hold on;
i = 3.5e11;
isosurface(X,Y,Z,n,i);
alpha(0.2)
hold on;
i = 4.5e11;
isosurface(X,Y,Z,n,i);
view(3)
colorbar;
unzip('ne231.zip')
Nx = 128;
Ny = 128;
Nz = 128;
Lx =128;
Ly = 128;
Lz = 128;
xi = (0:Nx-1)/Nx*2*pi;
xi_x = 2*pi/Lx;
x = xi/xi_x;
yi = (0:Ny-1)/Ny*2*pi;
yi_y = 2*pi/Ly;
y = yi/yi_y;
zi = (0:Nz-1)/Nz*2*pi;
zi_z = 2*pi/Lz;
z = zi/zi_z;
dx = Lx/Nx;
dy = Ly/Ny;
dz = Lz/Nz;
[X,Y,Z] = meshgrid(x,y,z);
%Sizing
fontSize = 16;
frames2Plot = [1 2 3 4 5 6];
numF = length(frames2Plot);
mB = 0.12;
mT = 0.05;
mL = 0.9;
sV = 0.005;
wC = 0.02;
sC = 0.02;
mC = 0.03;
mR = wC + sC + mC;
sH = 0.01;
sV = 0.01;
W = (1 - mL - mR - (numF/2-1)*sH) / (numF/2);
H = (1 - mT - mB - sV) / 2;
n = reshape(readmatrix('ne231.txt'),128,128,128);
f = figure('Position',[14 63 851 600],'Renderer','Painters');
i = 2.5e11;
isosurface(X,Y,Z,n,i);
alpha(0.0008)
hold on;
i = 3.5e11;
isosurface(X,Y,Z,n,i);
alpha(0.2)
hold on;
i = 4.5e11;
isosurface(X,Y,Z,n,i);
view(3)
colorbar;

Sign in to comment.

Answers (1)

Is this how it should be?
unzip('ne231.zip')
Nx = 128;
Ny = 128;
Nz = 128;
Lx =128;
Ly = 128;
Lz = 128;
xi = (0:Nx-1)/Nx*2*pi;
xi_x = 2*pi/Lx;
x = xi/xi_x;
yi = (0:Ny-1)/Ny*2*pi;
yi_y = 2*pi/Ly;
y = yi/yi_y;
zi = (0:Nz-1)/Nz*2*pi;
zi_z = 2*pi/Lz;
z = zi/zi_z;
dx = Lx/Nx;
dy = Ly/Ny;
dz = Lz/Nz;
[X,Y,Z] = meshgrid(x,y,z);
%Sizing
fontSize = 16;
frames2Plot = [1 2 3 4 5 6];
numF = length(frames2Plot);
mB = 0.12;
mT = 0.05;
mL = 0.9;
sV = 0.005;
wC = 0.02;
sC = 0.02;
mC = 0.03;
mR = wC + sC + mC;
sH = 0.01;
sV = 0.01;
W = (1 - mL - mR - (numF/2-1)*sH) / (numF/2);
H = (1 - mT - mB - sV) / 2;
cmap = [ ...
0 0 0; ... 'black'
126 47 142; ... '#7E2F8E'
255 0 0; ... 'red'
255 255 0; ... 'yellow'
]/255;
% filelocation1 = 'C:\Users\Data\';
n = reshape(readmatrix('ne231.txt'),128,128,128);
f = figure('Position',[14 63 851 600],'Renderer','Painters');
i = 1.5e11;
surf1 = isosurface(X,Y,Z,n,i);
p1 = patch(surf1);
isonormals(X,Y,Z,n,p1);
set(p1,'FaceColor',cmap(1,:),'EdgeColor','none','FaceAlpha',0.1);
daspect([1,1,1])
view(3);
camlight; lighting gouraud
i = 2.5e11;
surf2 = isosurface(X,Y,Z,n,i);
p2 = patch(surf2);
isonormals(X,Y,Z,n,p2);
set(p2,'FaceColor',cmap(2,:),'EdgeColor','none','FaceAlpha',0.2);
i = 3.5e11;
surf3 = isosurface(X,Y,Z,n,i);
p3 = patch(surf3);
isonormals(X,Y,Z,n,p3);
set(p3,'FaceColor',cmap(3,:),'EdgeColor','none','FaceAlpha',0.3);
i = 4.5e11;
surf4 = isosurface(X,Y,Z,n,i);
p4 = patch(surf4);
isonormals(X,Y,Z,n,p4);
set(p4,'FaceColor',cmap(4,:),'EdgeColor','none','FaceAlpha',1);
grid on;
xlabel('x(m)'); ylabel('y(m)'); zlabel('z(m)');
c = colorbar;
colormap(cmap)
n_colors = size(cmap,1);
caxis([1 n_colors+1]*1e11);
c.YTick = ((1:n_colors)+0.5)*1e11;
for i = 1:numF
hAxis(i).Position = [mL, (numF - i) * (sV + H) + mB , W, H];
end
c.Position = [mL + W + sC, mB, wC, 1 - mB - mT];
c.Label.String = 'values';

Asked:

on 6 Dec 2022

Commented:

on 8 Dec 2022

Community Treasure Hunt

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

Start Hunting!