Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
178 views
in Technique[技术] by (71.8m points)

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.
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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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:

  1. Creating the triangle using 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.

  3. Generating the grid of points using meshgrid.

  4. Plotting the points using plot3.

  5. Making the text visible using text.

  6. 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 is R, the second column is P and the third column is 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. There are three attributes we need to be concerned about:

  1. Vertices consists of the points that define our shape
  2. Faces asks you to specify which points to draw in the right order. By specifying Faces = 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.
  3. FaceColor allows you to specify what the colour of the triangle is. I made this cyan.

As such, we get this first:

enter image description here

... 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 the x and y directions reversed like the plot. We also use set and using the gca or Getting the Current Axes in focus of the plot, and setting the x (xdir) and y directions (ydir) in reverse. I also make a grid and set the alpha property to make the FaceColor transparent.

This is what we get:

enter image description here

... 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 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. You can first generate your meshgrid of points, then set any values less than 0 to NaN 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 use plot3 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:

enter image description here

... 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 of x, y and z 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:

enter image description here

... 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');

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...