How can I plot a 3D matrix as a cube with different transparent colors for the values?
21 次查看(过去 30 天)
显示 更早的评论
Hello,
I have a 3D-Matrix consisting only of values 0,1,2 and 3. Is it possible to plot the matrix so it looks like a big, transparent rubic´s cube, where every value gets its own colour? Like imagesc(...) but only in 3D.
Thanks a lot!
5 个评论
Korosh Agha Mohammad Ghasemi
2025-1-7
Steps:
- Use patch to create colored and semi-transparent cubes for each element in the 3D matrix.
- Assign colors based on the values in the matrix.
- Use transparency to visualize overlapping cubes.
% Example 3D matrix
matrix = randi([0, 3], 5, 5, 5); % Replace with your matrix
% Define colors for each value
colors = [
1, 1, 1; % White for 0
1, 0, 0; % Red for 1
0, 1, 0; % Green for 2
0, 0, 1 % Blue for 3
];
% Create figure
figure;
hold on;
axis equal;
xlabel('X'); ylabel('Y'); zlabel('Z');
view(3);
% Iterate over matrix
[nx, ny, nz] = size(matrix);
for x = 1:nx
for y = 1:ny
for z = 1:nz
value = matrix(x, y, z);
if value >= 0 && value <= 3
% Draw cube with specified color
drawCube([x-0.5, y-0.5, z-0.5], colors(value+1, :), 0.5);
end
end
end
end
hold off;
% Function to draw a single cube
function drawCube(center, color, alpha)
% Define cube vertices
vertices = [
0 0 0; 1 0 0; 1 1 0; 0 1 0; % Bottom face
0 0 1; 1 0 1; 1 1 1; 0 1 1 % Top face
];
vertices = vertices - 0.5; % Center around (0,0,0)
vertices = vertices + center; % Shift to desired center
% Define cube faces
faces = [
1 2 6 5; % Bottom
2 3 7 6; % Right
3 4 8 7; % Top
4 1 5 8; % Left
1 2 3 4; % Front
5 6 7 8 % Back
];
% Draw cube
patch('Vertices', vertices, 'Faces', faces, ...
'FaceColor', color, 'FaceAlpha', alpha, ...
'EdgeColor', 'black');
end
Explanation:
- Matrix values to colors: Each value (0, 1, 2, 3) is mapped to a specific RGB color.
- Transparent cubes: The FaceAlpha property in the patch function sets transparency.
- Cubic layout: The cubes are centered at the (x, y, z) indices of the matrix.
The script creates a 3D plot with a cube for each matrix element, colored based on its value, and transparent enough to see overlapping layers.
采纳的回答
darova
2019-7-24
Since you have matrix of 0,1,2,3 and don't have coordinates you can create them
Assume A - is you matrix (N x N x N)
% (:) - colon operator (make vector from matrix of length N*N*N)
[X,Y,Z] = meshgrid(1:N);
scatter3(X(:),Y(:),Z(:),5,A(:))
3 个评论
darova
2019-7-24
Create one cube using patch() (or fill3()) then copy it using for loop
clear,clc,cla
x1 = [1 1 1 1]'/2; % right wall
y1 = [1 1 -1 -1]'/2;
z1 = [1 -1 -1 1]'/2;
x2 = [1 1 -1 -1]'/2; % top wall
y2 = [-1 1 1 -1]'/2;
z2 = [1 1 1 1]'/2;
x3 = x2; % bottom wall
y3 = y2;
z3 = -z2;
X = [x1 x2 x3];
Y = [y1 y2 y3];
Z = [z1 z2 z3];
patch(X,Y,Z,'b')
alpha(0.5)
axis equal
I succeded
![img.png](https://www.mathworks.com/matlabcentral/answers/uploaded_files/231135/img.png)
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Surface and Mesh Plots 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!