Learning matlab (9)-drawing (three-dimensional)

table of Contents

(1) plot3() function

(2) peaks() function

(3) Mesh() function

(4) surf() function

(5) Cylinder diagram

(6) Spherical graph

(7) Three-dimensional contour map

(8) Histogram

(9) Pie chart

(10) Scatter chart

(11) Matchstick diagram

(12) Three-dimensional vector field diagram

(13) Comet chart

(14) Fill in the map

 (15) Three-dimensional ribbon map

(16) Non-network data drawing

(17) Three-dimensional implicit function drawing

(18) Other functions

(19) Graphical output method


In MATLAB, three-dimensional graphics include three-dimensional curves, three-dimensional grid graphs and three-dimensional surface graphs, which are drawn using the functions plot3( ), mesh() and surf( ), respectively. This chapter will also introduce some special 3D graphics rendering, focusing on the control tools such as perspective, color and lighting of 3D graphics, as well as the printing and output of graphics. Introduce separately below. MATLAB language provides three-dimensional drawing functions, these functions have many similarities with the drawing of two-dimensional graphics, for example, the attribute settings of the curve are exactly the same. The most commonly used three-dimensional drawings include three-dimensional curve graphs, three-dimensional grid graphs, and three-dimensional surface graphs. The complete three-dimensional drawing process usually includes drawing data, setting the graphics window, the effects of the viewing angle, color and lighting of the graphics, as well as the setting of coordinate axes and graphic annotations.

x=-10:0.1:10;
y=-10:0.1:10;
[X,Y]=meshgrid(x,y);
z=X.^2+Y.^2;
surf(x,y,z);
view([50 70])
colormap('cool');
shading interp;
light('Position',[1 0.4 0.4]);
axis square;
xlabel('x');
ylabel('y');
zlabel('z');

(1) plot3() function

In MATLAB, use the function plot3() to draw a three-dimensional curve. The calling format of this function is: plot3(x, y,z): the function draws a three-dimensional curve, and the parameters x, y and z are vectors with the same dimensions. . plot3(X, Y, Z): The parameters X, Y, and Z in this function are matrices with the same dimension. Each column of the parameters X, Y, and Z is drawn with one curve, and multiple curves are drawn at the same time. plot3(X, Y, Z, s): This function is similar to the two-dimensional plotting function plot(), you can set the line type and color of the curve, as well as the mark of the data point.
 

t=linspace(0,20*pi,500);
x=t.*sin(t);
y=t.*cos(t);
z=t;
plot3(x,y,z);
xlabel('x');
ylabel('y');
zlabel('z');

(2) peaks() function

In MATLAB, a peaks() function is provided, which can generate a concave-convex surface with three local maxima and three local minima. peaks: This function draws a three-dimensional surface, the default size is 49*49. peaks(n): This function draws a three-dimensional surface, and the three-dimensional surface data is n*n. z=peaks: Generate a matrix with a default size of 49*49, without drawing a three-dimensional surface. z=peaks(x, y): This function calculates the value of the equation whose input parameters are x and y, and does not draw a three-dimensional surface. [x, y, z]=peaks: This function generates 3 matrices x, y, and z, all of which are 49*49 in size, and no three-dimensional surface is drawn. [x, y, z]=peaks(n): This function generates 3 matrices x, y, and z, all of which are n*n in size, and no three-dimensional surface is drawn.

[a,b,c]=peaks(20);
peaks

(3) Mesh() function

In MATLAB, the function meshgrid() is used to generate the grid coordinate matrix in the plane area.

x=-2*pi:2*pi;
y=-2*pi:2*pi;
[X,Y]=meshgrid(x,y);
plot(X,Y,'o');

In MATLAB, the function mesh() is used to draw a three-dimensional grid. This function connects the points on the surface with lines, and the surface area corresponding to the grid is displayed as blank. The calling format of this function is: mesh(X, Y, z, c): This function draws a colorful three-dimensional grid graph, where the matrices X and Y control the x-axis and y-axis, the matrix Z is the z-axis data, and the color of the graph Set with parameter c. In MATLAB, the function meshc() is used to draw a three-dimensional grid with contour lines, and the function meshz() is used to draw a three-dimensional grid with a base. The usage of these two functions is similar to the function mesh(). In addition, the function waterfall() can also be used to draw a three-dimensional grid.

[X,Y]=meshgrid(-3:0.1:3);
Z=5*X.^2-Y.^2+3;
subplot(121);
plot3(X,Y,Z);
subplot(122);
mesh(X,Y,Z);
xlabel('x');
ylabel('y');
zlabel('z');

(4) surf() function

In MATLAB, the function surf() is used to draw three-dimensional surface graphs. The calling format of this function is: surf(X, Y, Z, c): This function draws a colorful three-dimensional surface graph, where the matrices X and Y control the x-axis and y-axis, the matrix Z is the z-axis data, and the color of the graph is Parameter c is set. In MATLAB, the function surfc() is used to draw a three-dimensional surface map with contour lines, and the lighting effect of the three-dimensional surface is added through the function surfl(). The calling method is the same as surf( ).

[X,Y]=meshgrid(-3:0.1:3);
Z=peaks(X,Y);
subplot(121);
surf(X,Y,Z);
subplot(122);
surfc(X,Y,Z);
xlabel('x');
ylabel('y');
zlabel('z');

(5) Cylinder diagram

In MATLAB, the cylinder graph data can be generated by the function cylinder( ), and then the three-dimensional rotating body can be drawn conveniently by the function mesh( ).

t=0:pi/20:3*pi;
r=5+cos(t);
[x,y,z]=cylinder(r,30);
mesh(x,y,z);
xlabel('x');
ylabel('y');
zlabel('z');

 

(6) Spherical graph

In MATLAB, the spherical graph can be drawn through the function sphere(). The calling format of this function is: [X, Y, Z]=sphere(N): This function produces 3 (N+1)*(N+1) matrices, using the function surf(×,Y,Z) Unit sphere can be produced. [X,Y,Z]=sphere: This function generates 3 matrices, the default value. sphere(N): This function only draws a spherical graph without returning any value.

subplot(121);
sphere(30);
subplot(122);
[x,y,z]=sphere;
surf(10*x,10*y,10*z);
axis('equal');
colormap('hot');

 

(7) Three-dimensional contour map

In MATLAB, you can draw a three-dimensional contour map through the function contour3(). The calling format of this function is very similar to the contour() function for drawing two-dimensional contours. The user can enter in the command line window: helpcontour3 to query the detailed usage of this function.

Z=peaks;
subplot(121);
contour(Z,20);
subplot(122);
contour3(Z,20)
axis([-inf inf -inf inf -inf inf])

(8) Histogram

In MATLAB, the function bar3() is used to draw a three-dimensional histogram. The calling format of this function is very similar to the function bar(). The user can enter: help bar3 in the command line window to view the detailed introduction of the function. In addition, you can also use bar3h (to draw a horizontal three-dimensional histogram, the calling method of this function is the same as that of the function bar3().

x=[1:6;3:8;7:12];
bar3(x',0.3);

 

(9) Pie chart

In MATLAB, the function pie3() is used to draw a three-dimensional pie chart. The calling format of this function is basically the same as the two-dimensional pie chart drawing function pie(). If the user needs to view the detailed usage of the function pie3(), they can enter: help pie3 in the command line window.

x=[0.2 0.1 0.25 0.15 0.16];
y=[3 5.5 7 10.5 8.5];
subplot(121);
pie3(x);
subplot(122);
explode=[0 0 0 1 0];
pie3(y,explode);

 

(10) Scatter chart

In MATLAB, the function scatter3() is used to draw a three-dimensional scatter plot. The calling format of this function is very similar to the two-dimensional scatter plot function scatter(). If the user needs to view the detailed usage of the function scatter3( ), you can enter: help scatter3 in the command line window.

x=rand(1,20);
y=rand(1,20);
z=x+y;
subplot(121);
scatter3(x,y,z);
subplot(122);
scatter3(x,y,z,'r','filled');

(11) Matchstick diagram

In MATLAB, the function stem3() is used to draw a three-dimensional matchstick graph. The calling format of this function is very similar to the two-dimensional matchstick drawing function stem(). The user can enter: help stem3 in the MATLAB command line window to view the detailed usage of this function.

x=rand(1,20);
y=rand(1,20);
z=x+y;
subplot(121);
stem3(x,y,z);
subplot(122);
stem3(x,y,z,'r','filled');

(12) Three-dimensional vector field diagram

In MATLAB, the function quiver3() is used to draw the three-dimensional vector field diagram. The calling format of this function is very similar to the two-dimensional vector field diagram drawing function quiver(). The user can enter: help quiver3 in the command line window to view the detailed usage of the function.

[X,Y]=meshgrid(-3:0.3:3);
Z=-3*X.^2-Y.^2;
[U,V,W]=surfnorm(X,Y,Z);
subplot(121);
quiver3(X,Y,Z,U,V,W,0.2);
subplot(122);
quiver3(X,Y,Z,U,V,W);
hold on;
surf(X,Y,Z);

(13) Comet chart

In MATLAB, the function comet3() is used to draw the three-dimensional comet chart. The calling format of this function is very similar to the drawing function comet() of the two-dimensional comet chart. The user can enter: help comet3 in the MATLAB command line window to query the detailed usage of this function.

t=-pi:pi/400:pi;
x=sin(5*t);
y=cos(3*t);
z=t;
comet3(x,y,z);

(14) Fill in the map

In MATLAB, the function fill3() is used to draw a three-dimensional fill map. The calling format of this function is basically the same as the two-dimensional fill map function fil(). The user can enter: help fill3 in the MATLAB command line window to query the detailed introduction of the function.

x=rand(1,10);
y=rand(1,10);
z=x-y;
fill3(x,y,z,'b');

 (15) Three-dimensional ribbon map

In MATLAB, the function ribbon() is used to draw a three-dimensional ribbon graph. The calling format of this function is: ribbon(X, Y): This function draws a three-dimensional ribbon graph, similar to the function plot(). ribbon(Y): This function draws a three-dimensional ribbon map, X is X=1:size(Y,1) by default. ribbon(X, Y, width): This function draws a three-dimensional ribbon map, and uses the parameter width to set the width of the line.

x=0:pi/20:2*pi;
y=sin(x);
subplot(121);
ribbon(x,y,2);
subplot(122);
ribbon(cos(x));
xlabel('x');
ylabel('y');
zlabel('z');

(16) Non-network data drawing

In MATLAB, when the functions mesh() and surf() are used for drawing, the data is required to be uniformly distributed grid data. If the data is not grid data, the function meshgrid() can be used to create interpolated grid data. Zl=griddata(X, Y, Z, XI, YI): This function uses data X, Y and Z to establish an interpolation function Z=F(X, Y), and then calculates the interpolation result ZI at the data (I, YI) . Zl=griddata(X, Y,z, XI, Yl, method): This function uses the parameter method to set the interpolation algorithm, which can be:'linear' is the linear interpolation algorithm, the default value of the system; ``cubic' is cubic Interpolation,'nearest' means interpolation of neighboring points, etc.

In MATLAB, the function patch() is used to draw a three-dimensional patch map. The calling format of this function is: patch(X, Y, Z): This function creates a spatial polygon with parameters X, Y, and Z as items. patch(X,Y,z,C): The parameter C in this function is used to set the color.

(17) Three-dimensional implicit function drawing

In MATLAB, there are some simple functions for drawing three-dimensional graphics, which can draw three-dimensional graphics with implicit numbers. These functions are shown in the table. These functions are introduced separately below.

function Description
ezplot3() Draw three-dimensional curves
ezmesh3 () Draw a three-dimensional grid map
ezmeshz (( Draw a three-dimensional grid map with contour lines
ezsurf () Draw 3D curved surface
ezsurfc () Draw a three-dimensional surface map with contour lines

(18) Other functions

For three-dimensional graphics, viewing from different perspectives will result in different sides and results. In MATLAB, the function view() is used to set the perspective of the three-dimensional graphics. The calling format of this function is: view(az, el) or view([az, el]): This function sets the position of the perspective, and the azimuth is az And the elevation angle is el, they are all in degrees view([x, y, z]): This function sets the viewing angle to the direction pointed by the vector [x, y, z]. view(2): This function is the default two-dimensional viewing angle. view(3): This function is the default three-dimensional viewing angle. [az, el]=view: This function returns the current view angle az and el.

In MATLAB, the function rotate3D() is used to set the three-dimensional perspective of the graphics. The calling format of this function is: rotate3d on or rotate3d('on'): This function opens the three-dimensional perspective of the graphics, and you can conveniently use the mouse to control the change of the perspective. , And update the graphics in real time. rotate3d off or rotate3d('off'): This function turns off the 3D viewing angle function of the graph. rotate3d: This function switches between rotate3d on and rotate3d off. When using the function mesh() to draw a three-dimensional grid graph, the occluded part of the three-dimensional graph is hidden by default. The hidden() function can be used to display the enticing effect of graphics.

When drawing in MATLAB, you can use color information to make the graphics more vivid and intuitive. RGB true color and color map coloring can be used in MATLAB. RGB true color uses color mapping to process colors, that is, RGB color system. The various colors in the computer are modulated in different proportions through the three primary colors (red, green and blue). The value of each color in the color map is a vector, where R, G, and B are numbers between 0 and 1, that is, the intensity of the three colors of red, yellow, and blue, forming a specific color. In addition to true colors, color maps can also be used for coloring. There are many colormap functions built in MATLAB.

jet a variant of hsv
hsv Color saturation value
hot From black to red to yellow to white
cool The chromaticity of cyan and magenta
spring Pink to yellow
summer From cyan to yellow
autumn Red to yellow
winter Blue to cyan
gray Linear gray
bone With a little blue length
copper Linear copper chromaticity
pink Pink chromaticity
lines Linear color map

In MATLAB, the function rgbplot() can be used to draw the color map. Use the function colorbar() to add a color bar to the current graphics window and display the color map of the current axis. Use the function brighten() to brighten or darken the color map of the graph. Use the function caxis() to control the scale of the color axis, the corresponding relationship between the value and the color, and the display range of the color. The shading() function is used to control the coloring of the surface graphics. This function is used for three-dimensional grid maps, three-dimensional surface maps and three-dimensional filled maps. Use the function colordef() to set the background color of the graph.

In MATLAB, you can add lighting effects to graphics. Use the function light() to create a light source. The calling format of this function is: light: This function adds a light source to the current graphic. The attribute values ​​of the light source object are all system defaults. light(Prarm1,Value1, ..., ParamN, ValueN): This function assigns the parameter Parm1 to Value1, and so on to set the value of each attribute of the light source object. L=light(...): This function returns the handle of the light source object. After obtaining the handle, you can get and set the attribute value through the functions get() and set().

In MATLAB, use the function lighting() to set the mode of the light source. The calling format of this function is: lighting flat or lighting('flat'): this function sets the light source mode to flat, that is, the plane mode, and the grid is the basic lighting unit. This mode is the default mode of the system. lighting gouraud or lighting('gouraud'): This function sets the light source mode to gouraud, that is, point mode, with pixels as the basic unit of lighting. lighting phong or lighting('phong'): This function sets the light source mode to phong, uses pixels as the basic unit of lighting, and considers the reflection of each point. lighting none or lighting('none'): Turn off the lighting effect.

In the default graphics window, the Camera control bar is not displayed. Several typical graphics are drawn below, including the perspective effect of the graphics, the hollowing of the graphics, and the use of three-dimensional graphics to represent four-dimensional data.

[x,y,z]=sphere(40);
surf(x,y,z);
shading interp;
hold on;
mesh(2*x,2*y,2*z);
hold off;
hidden off;
axis off;

(19) Graphical output method

<1>copy figure;

<2>print() function;

<3>print preview;

Guess you like

Origin blog.csdn.net/qq_35789421/article/details/115368734