在 MATLAB 中绘制 3D [英] Plotting 3D in MATLAB
问题描述
大家好,对不起,如果这个问题看起来很愚蠢.我对编程完全陌生,需要使用 MATLAB 绘制以下图像.
我一遍又一遍地尝试,似乎无法理解 MATLAB 的工作原理.
有没有人可以帮助编写代码来绘制这样的图片并向我解释编程的工作原理?
我通常不回答给我一些很酷的问题,但是画这个图太诱人了,无法通过.此图结合了多种绘图方法:
使用
...没有那么令人印象深刻吧?
第 2 步 - 玩弄数字
输出将是一个填充的形状,但这最初将在 2D 中可视化......就像我们在上面看到的那样.接下来我们需要做的是旋转相机,使其正确地可视化三角形.此外,轴是反转,不像传统的轴.我们也必须扭转这些.另外,我们可能想制作一个
grid
,并使脸部颜色透明.您可以在此处使用此代码执行此操作:
view(3)设置(gca,'xdir','反向')设置(gca,'ydir','反向')网格;阿尔法('颜色')
view(3)
移动相机,使其成为 3D 绘图的默认 MATLAB 方向.我们还使x
和y
方向像绘图一样反转.我们还使用set
并使用gca
或 G 设置 Current Axes 在情节的焦点上,并在x
(xdir
) 和y
方向 (ydir
) 中设置代码>反向代码>.我还制作了一个grid
并设置alpha
属性以使FaceColor
透明.这是我们得到的:
...好吧...还不错.
第 3 步 - 生成三角形中的点
我们需要提出我们的观点.你可以像上面那样用
meshgrid
做到这一点.这个平面遵循z = 1 - x - y
等式,但我们需要确保我们过滤掉那些小于 0 的值,我们不应该将它们可视化.您可以首先生成点的meshgrid
,然后将任何小于 0 的值设置为NaN
,这样就不会显示它们.为了绝对确定并避免浮点错误,我将检查是否小于 -0.1:[X,Y] = meshgrid(0:(1/6):1);Z = 1 - X - Y;Z(Z <-0.01) = NaN;
以上生成了一个二维点网格,其分辨率为您的图形所需的
1/6
分辨率.第 4 步 - 绘制点
确保你
hold住
,这样我们就可以在同一张图上放更多的东西,然后用plot3
把这些点放在一起:坚持住;plot3(X, Y, Z, 'b.', 'MarkerSize', 32);
这将绘制我们的点并使它们变成蓝色.这也使标记的直径为 32 像素,以便我们可以更好地看到事物.
我们现在得到这个:
...不错,不错.
步骤 #5 和 #6 - 添加文本框,绘制线条并添加一些轴标签
我们现在将
NE
文本添加到图的中间.我们还想从底部到图的中间画一条线:text(0.3,0.3,0.4, 'NE');线([1/2 1/3], [1/2 1/3], [0 1/3], '颜色', '黑色');
text
通过接受(x,y,z)
坐标起作用,您可以在该位置放置文本.图表的中间技术上是(1/3,1/3,1/3)
,但如果我放在这里,你真的看不到文字.我决定把它放在(0.3,0.3,0.4)
.最后,我们将画一条从基地到点的线.line
接受x
、y
和z
点的向量.每列代表一个点,所以我画了一条从(1/2,1/2,0)
的底部到(1/3,1/3,1/3)
.我也把这条线变黑了.最后,我要为轴添加一些标题:
xlabel('R');ylabel('P');zlabel('S');
我们现在得到:
...我喜欢!
<小时>为了您复制和粘贴的乐趣,以下是您可以用来运行并获得上图的完整代码:
vert = [0 1 0;1 0 0;0 0 1];补丁('顶点',垂直,'人脸',1:3,'人脸颜色','青色')视图(3)设置(gca,'xdir','反向')设置(gca,'ydir','反向')网格阿尔法('颜色')[X,Y] = meshgrid(0:(1/6):1);Z = 1 - X - Y;Z(Z <-0.01) = NaN;坚持,稍等;plot3(X, Y, Z, 'b.', 'MarkerSize', 32);文本(0.3,0.3,0.4,'NE');线([1/2 1/3], [1/2 1/3], [0 1/3], '颜色', '黑色');xlabel('R');ylabel('P');zlabel('S');
Hi guys, sorry if this question seems really dumb. I am completely new to programming, and is needing to draw the following image using MATLAB.
I have tried over and over again and can't seem to understand how MATLAB works.
Is there anyone that can help to write a code to draw a picture like this and explain to me how the programming works?解决方案I usually don't answer give me teh coodz questions, but drawing this graph was too tempting to pass. This graph combines several plot methodologies together:
Creating the triangle using
patch
.Setting the axes properties to reverse the axes order and setting the properties of the colours to make the triangle transparent. This is using
set
.Generating the grid of points using
meshgrid
.Plotting the points using
plot3
.Making the text visible using
text
.Drawing the black vertical line using
line
and setting the axes labels.
Step #1 - Drawing the triangle
The best thing to create that triangle is to use the
patch
function.patch
takes in a set of vertices as well as the order of how you traverse these vertices, and optionally a face colour. As such, the first bit of code is this:vert = [0 1 0; 1 0 0; 0 0 1]; patch('Vertices', vert, 'Faces', 1:3, 'FaceColor', 'cyan');
vert
is a list of vertices. Each row consists of a control point that the shape contains. Each column is one coordinate. The first column isR
, the second column isP
and the third column isS
. Because there are three points in our triangle, there are three rows of points. Each row consists of one corner point for each triangle. Take note of the way I specifically ordered the points. You need to define the points in a clockwise (or counter-clockwise... up to you) order because this is going to be used later.Now have a look at how I called
patch
. There are three attributes we need to be concerned about:Vertices
consists of the points that define our shapeFaces
asks you to specify which points to draw in the right order. By specifyingFaces = 1:3 --> [1 2 3]
, I'm drawing the first, second and third point in that order. This traverses the points in clockwise order which then gives us the triangle filled in.FaceColor
allows you to specify what the colour of the triangle is. I made thiscyan
.
As such, we get this first:
... not really that impressive right?
Step #2 - Fooling around with the figure
The output will be a filled in shape but this will initially be visualized in 2D.... like we saw above. The next thing we need to do is rotate the camera so that it's visualizing triangle correctly. Also, the axes are reversed and not like conventional axes. We'll have to reverse these too. In addition, we probably want to make a
grid
and make the face colour transparent.You can do that with this code here:
view(3) set(gca, 'xdir', 'reverse') set(gca, 'ydir', 'reverse') grid; alpha('color')
view(3)
moves the camera so that it is the default MATLAB orientation for 3D plots. We also make thex
andy
directions reversed like the plot. We also useset
and using thegca
or Getting the Current Axes in focus of the plot, and setting thex
(xdir
) andy
directions (ydir
) inreverse
. I also make agrid
and set thealpha
property to make theFaceColor
transparent.This is what we get:
... alright... not bad.
Step #3 - Generating the points in the triangle
We need to put in our points. You can do that with
meshgrid
like you did above. This plane follows the equation ofz = 1 - x - y
, but we need to make sure we filter out those values that are less than 0 and we shouldn't visualize those. You can first generate yourmeshgrid
of points, then set any values less than 0 toNaN
so you don't show them. To be absolutely sure and to escape floating-point error, I'm gonna check for less than -0.1:[X,Y] = meshgrid(0:(1/6):1); Z = 1 - X - Y; Z(Z < -0.01) = NaN;
The above generates a 2D grid of points that are
1/6
resolution as desired by your figure.Step #4 - Plot the points
Make sure you
hold on
so we can put more stuff on the same graph, then useplot3
to put these points up:hold on; plot3(X, Y, Z, 'b.', 'MarkerSize', 32);
This will plot our points and make them blue. This also makes the marker size 32 pixels in diameter so we can see things better.
We now get this:
... not bad, not bad.
Step #5 and #6 - Adding the text box, drawing the line and adding some axes labels
We now add the
NE
text to the middle of the plot. We also will want to draw a line from the lower base to the middle of the plot:text(0.3,0.3,0.4, 'NE'); line([1/2 1/3], [1/2 1/3], [0 1/3], 'color', 'black');
text
works by accepting a(x,y,z)
coordinate and you can place text at that spot. The middle of the graph is technically(1/3,1/3,1/3)
, but if I put it here, you don't really see the text well. I decided to put it at(0.3,0.3,0.4)
. Finally, we'll draw a line from the base to the point.line
takes in a vector ofx
,y
andz
points. Each column represents one point, so I'm drawing a line from the base at(1/2,1/2,0)
to the middle at(1/3,1/3,1/3)
. I've also made the line black.Finally, I'm gonna add in some titles to the axes:
xlabel('R'); ylabel('P'); zlabel('S');
We now get:
... I like!
For your copying and pasting pleasure, here's the full code that you can use to run and get the above figure:
vert = [0 1 0; 1 0 0; 0 0 1]; patch('Vertices', vert, 'Faces', 1:3, 'FaceColor', 'cyan') view(3) set(gca, 'xdir', 'reverse') set(gca, 'ydir', 'reverse') grid alpha('color') [X,Y] = meshgrid(0:(1/6):1); Z = 1 - X - Y; Z(Z < -0.01) = NaN; hold on; plot3(X, Y, Z, 'b.', 'MarkerSize', 32); text(0.3,0.3,0.4, 'NE'); line([1/2 1/3], [1/2 1/3], [0 1/3], 'color', 'black'); xlabel('R'); ylabel('P'); zlabel('S');
这篇关于在 MATLAB 中绘制 3D的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!