简体   繁体   English

在MATLAB中绘制3D

[英]Plotting 3D in MATLAB

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. 我是编程新手,需要使用MATLAB绘制以下图像。
I have tried over and over again and can't seem to understand how MATLAB works. 我已经尝试了一遍又一遍,似乎无法理解MATLAB的工作原理。
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. 我通常不会回答我给Coodz的问题,但是绘制这张图太诱人了。 This graph combines several plot methodologies together: 此图将几种绘图方法结合在一起:

  1. Creating the triangle using patch . 使用patch创建三角形。

  2. 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 . 这是使用set

  3. Generating the grid of points using meshgrid . 使用meshgrid生成点的网格。

  4. Plotting the points using plot3 . 使用plot3绘制点。

  5. Making the text visible using text . 使用text使文本可见。

  6. Drawing the black vertical line using line and setting the axes labels. 使用line绘制黑色垂直线并设置轴标签。

Step #1 - Drawing the triangle 步骤#1-绘制三角形

The best thing to create that triangle is to use the patch function. 创建该三角形的最好的办法是使用patch功能。 patch takes in a set of vertices as well as the order of how you traverse these vertices, and optionally a face colour. patch会获取一组顶点以及遍历这些顶点的顺序,还可以选择获取脸部颜色。 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. vert是一个顶点列表。 Each row consists of a control point that the shape contains. 每行都包含一个形状所包含的控制点。 Each column is one coordinate. 每一列都是一个坐标。 The first column is R , the second column is P and the third column is S . 第一列为R ,第二列为P ,第三列为S 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 . 现在看看我如何称呼patch There are three attributes we need to be concerned about: 我们需要关注三个属性:

  1. Vertices consists of the points that define our shape Vertices由定义我们形状的点组成
  2. Faces asks you to specify which points to draw in the right order. Faces要求您指定要按正确顺序绘制的 By specifying Faces = 1:3 --> [1 2 3] , I'm drawing the first, second and third point in that order. 通过指定Faces = 1:3 --> [1 2 3] ,我按此顺序绘制了第一,第二和第三点。 This traverses the points in clockwise order which then gives us the triangle filled in. 这将以顺时针方向遍历这些点,然后为我们填充了三角形。
  3. FaceColor allows you to specify what the colour of the triangle is. FaceColor允许您指定三角形的颜色。 I made this cyan . 我做了cyan

As such, we get this first: 因此,我们首先得到以下内容:

在此处输入图片说明

... not really that impressive right? ...真的不那么令人印象深刻吧?

Step #2 - Fooling around with the figure 步骤#2-无所事事

The output will be a filled in shape but this will initially be visualized in 2D.... like we saw above. 输出将为实心形状,但最初将以2D形式可视化...。 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. 另外,我们可能希望制作一个grid并使面部颜色透明。

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. view(3)移动相机,使其成为3D图的默认MATLAB方向。 We also make the x and y directions reversed like the plot. 我们还使xy方向像图一样反转。 We also use set and using the gca or G etting the C urrent A xes in focus of the plot, and setting the x ( xdir ) and y directions ( ydir ) in reverse . 我们还使用set和使用该gcaG ETTING在情节的焦点与c urrent XES,并设置xxdir )和y方向( ydir中) reverse I also make a grid and set the alpha property to make the FaceColor transparent. 我还制作了一个grid并设置alpha属性以使FaceColor透明。

This is what we get: 这是我们得到的:

在此处输入图片说明

... alright... not bad. ...好吧...还不错。

Step #3 - Generating the points in the triangle 步骤#3-生成三角形中的点

We need to put in our points. 我们需要提出自己的观点。 You can do that with meshgrid like you did above. 您可以像上面一样使用meshgrid This plane follows the equation of z = 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. 该平面遵循z = 1 - x - y的方程,但是我们需要确保过滤掉小于0的那些值,并且不应该可视化这些值。 You can first generate your meshgrid of points, then set any values less than 0 to NaN so you don't show them. 你可以首先生成您meshgrid点,然后设置值小于0到NaN ,所以你不告诉他们。 To be absolutely sure and to escape floating-point error, I'm gonna check for less than -0.1: 为了绝对确定并避免浮点错误,我将检查小于-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. 上面将生成一个2D的点网格,其分辨率为您的图形所需的1/6

Step #4 - Plot the points 步骤#4-绘制点

Make sure you hold on so we can put more stuff on the same graph, then use plot3 to put these points up: 确保hold on以便我们可以在同一图形上放置更多内容,然后使用plot3放置以下几点:

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. 这也使标记大小的直径为32像素,因此我们可以更好地看到事物。

We now get this: 我们现在得到这个:

在此处输入图片说明

... not bad, not bad. ...还不错,还不错。

Step #5 and #6 - Adding the text box, drawing the line and adding some axes labels 步骤#5和#6-添加文本框,画线并添加一些轴标签

We now add the NE text to the middle of the plot. 现在,我们将NE文本添加到绘图的中间。 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. text通过接受(x,y,z)坐标起作用,您可以将文本放置在该位置。 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. 从技术上讲,图表的中间是(1/3,1/3,1/3) ,但是如果我将其放在此处,您不会真正看到该文本。 I decided to put it at (0.3,0.3,0.4) . 我决定将其设置为(0.3,0.3,0.4) Finally, we'll draw a line from the base to the point. 最后,我们将从基点到点画一条线。 line takes in a vector of x , y and z points. line接受xyz点的向量。 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) . 每列代表一个点,因此我在(1/2,1/2,0)的底部到(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');

You can use fill3(X,Y,Z,C) function. 您可以使用fill3(X,Y,Z,C)函数。 X, Y, and Z triplets specify the polygon vertices. X,Y和Z三元组指定多边形顶点。 C specifies color, where C is a vector or matrix of indices into the current colormap, Here you can see how create this color vector. C指定颜色,其中C是当前颜色图的向量或索引矩阵, 在这里您可以看到如何创建此颜色向量。

if you want display axes grid lines, you should use grid on. 如果要显示轴网格线,则应使用grid on。

Here a example: 这里是一个例子:

x = [0 1 1];                      
y = [0 1 0];
z = [0 1 0];
c = [0 0 1];
figure(1) % open a new figure
subplot(1,1,1) % create a subplot with 1 row and 1 column and select first
fill(x,y,z,c)
grid on
hold on
PointXYZ = 0;
plot3(PointXYZ,PointXYZ,PointXYZ,'*m')

The plot3 function display a three-dimensional plot of a set of data points, in this case, (0,0,0). plot3函数显示一组数据点的三维图,在这种情况下为(0,0,0)。 '*m' defines chart line properties. '* m'定义图表线属性。

It is a example. 这是一个例子。 To show all the points you must create coordinate vectors, and use them in plot3. 要显示所有点,必须创建坐标矢量,并在plot3中使用它们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM