Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to plot 3D grid (cube) in Matlab

Hi I would like to plot transparent cube-shaped grid with lines in it. Something like this: enter image description here

However, I managed only to draw a 2D grid:

[X,Y] = meshgrid(-8:.5:8);
Z = X+1;
surf(X,Y,Z)

I use Matlab R2009b. If it is impossible to plot this in matlab could you recommend me a software I could use.

like image 572
Func Avatar asked Sep 05 '11 14:09

Func


People also ask

How do you plot a grid in Matlab?

Call the tiledlayout function to create a 2-by-1 tiled chart layout. Call the nexttile function to create the axes objects ax1 and ax2 . Plot data into each axes. Then display grid lines in the bottom plot by passing ax2 to the grid function.

How do you plot a 3 dimensional graph in Matlab?

plot3( X , Y , Z ) plots coordinates in 3-D space. To plot a set of coordinates connected by line segments, specify X , Y , and Z as vectors of the same length. To plot multiple sets of coordinates on the same set of axes, specify at least one of X , Y , or Z as a matrix and the others as vectors.

Can we have multiple 3d plots in Matlab?

Can we have multiple 3d plots in MATLAB? Explanation: The plot3() function is a pre-defined function in MATLAB. So, it will allow the use to generate multiple 3d plots. This is inherent to the system.


2 Answers

Consider this vectorized solution. It has the advantage that it creates a single graphic object:

%# these don't all have to be the same
x = -8:2:8; y = -8:2:8; z = -8:2:8;

[X1 Y1 Z1] = meshgrid(x([1 end]),y,z);
X1 = permute(X1,[2 1 3]); Y1 = permute(Y1,[2 1 3]); Z1 = permute(Z1,[2 1 3]);
X1(end+1,:,:) = NaN; Y1(end+1,:,:) = NaN; Z1(end+1,:,:) = NaN;
[X2 Y2 Z2] = meshgrid(x,y([1 end]),z);
X2(end+1,:,:) = NaN; Y2(end+1,:,:) = NaN; Z2(end+1,:,:) = NaN;
[X3 Y3 Z3] = meshgrid(x,y,z([1 end]));
X3 = permute(X3,[3 1 2]); Y3 = permute(Y3,[3 1 2]); Z3 = permute(Z3,[3 1 2]);
X3(end+1,:,:) = NaN; Y3(end+1,:,:) = NaN; Z3(end+1,:,:) = NaN;

%#figure('Renderer','opengl')
h = line([X1(:);X2(:);X3(:)], [Y1(:);Y2(:);Y3(:)], [Z1(:);Z2(:);Z3(:)]);
set(h, 'Color',[0.5 0.5 1], 'LineWidth',1, 'LineStyle','-')

%#set(gca, 'Box','on', 'LineWidth',2, 'XTick',x, 'YTick',y, 'ZTick',z, ...
%#  'XLim',[x(1) x(end)], 'YLim',[y(1) y(end)], 'ZLim',[z(1) z(end)])
%#xlabel x, ylabel y, zlabel z
axis off
view(3), axis vis3d
camproj perspective, rotate3d on

screenshot

like image 131
Amro Avatar answered Oct 12 '22 10:10

Amro


If you don't mind a few for loops, something like this will work:

clf
figure(1)
for g = 0:.2:2
for i = 0:.2:2

   plot3([g g], [0 2], [i, i])
   hold on
end
end

for g = 0:.2:2
for i = 0:.2:2

   plot3([0 2], [g g], [i, i])
   hold on
end
end

for g = 0:.2:2
for i = 0:.2:2

   plot3([i i], [g g], [0 2])
   hold on
end
end

You will just need to make the grid transparent by probably changing line properties, I don't think you can change alpha values to accomplish this. Hope that is helpful.

like image 23
St-Ste-Ste-Stephen Avatar answered Oct 12 '22 10:10

St-Ste-Ste-Stephen