简体   繁体   中英

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?

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:

在此处输入图片说明

... 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 G etting the C urrent A xes 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:

在此处输入图片说明

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

在此处输入图片说明

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

在此处输入图片说明

... 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. X, Y, and Z triplets specify the polygon vertices. 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.

if you want display axes grid lines, you should use 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). '*m' defines chart line properties.

It is a example. To show all the points you must create coordinate vectors, and use them in plot3.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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