- 2020-08-06 18:37
*views 47*- MATLAB note
- Matlab

Need to use recently Matlab Draw some three-dimensional surface drawings , And mark it . So these smallpox took some effort to sort out again about Matlab

Some points for attention in 3D surface drawing , Record here , Easy to use and find in the future .

<> Task requirements ： Known function form , Draw a 3D surface

This time, the following functions are taken as examples , introduce Matlab Drawing method of 3D surface

z = 1 − x 2 + ( y − 1 ) 2 z = 1-\sqrt{x^{2}+(y-1)^{2}} z=1−x2+(y−1)2

* The finished effect drawing is as follows ：

<> Drawing explanation ：

<>1. Draw 3D image

* [X,Y] = meshgrid(x,y);： take x and y Gridding , Get the meshed variables X and Y

The so-called gridding refers to the original one-dimensional coordinate axis x and y combination , Become a two-dimensional plane X and Y. Some people may feel that it is not easy to understand , I can't describe it clearly in words . But open it in the work area X and Y

You can understand it after viewing its internal specific values .

* Fig = mesh(X,Y,Z);： with X,Y and Z Draw 3D surfaces for data , And assign this surface to Fig

In the beginning, I used mesh(X,Y,Z) Without its return value , Later, it was found that it was easier to adjust parameters with return values （ The details will be explained later ）

After mastering these two codes , Can draw a basic three-dimensional surface , The code is as follows ：

clear;clc; % Clear pre data % Data preprocessing x = linspace(0,1,50); % set up x Range of axes y = x; % set up y Axis range [X,Y]

= meshgrid(x,y); % Put it x,y Axis gridding Z = 1-sqrt((X).^2+(Y-1).^2); % Direct calculation % Draw a surface Fig =

mesh(X,Y,Z); % Draw 3D surface

From this, we can get a very simple three-dimensional surface , Such a surface is still a long way from our requirements .

<>2. Coordinate axis setting

The most obvious feeling in the above picture is space , So we need to add a coordinate axis to it ：

* xlabel('x');： set up x The variable of the axis is x

* ylabel('$y$','interpreter','latex');： take y The variable of the axis is set to LaTeX Formatted y（ The code here and LaTeX The formula code is completely consistent ）

* zlabel('$z$','interpreter','latex','FontSize',18);： In addition to the above functions , set up z The font size of the axis variable is 18pt

* title('$z =

1-\sqrt{x^{2}+(y-1)^{2}}$','interpreter','latex','FontWeight','bold');： Name title and bold

There are many functions for setting the coordinate axis , For example, replace fonts , You can use help documents （ Enter at the command line help text see ）

notes ： here help Yes text, Only in this way can there be a sufficiently detailed description of the options

We use the following code to format the coordinate axis ：（ there L(i) Same as above Fig Can be removed ）

L(1) = xlabel('x'); L(2) = ylabel('$y$','interpreter','latex'); L(3) =

zlabel('$z$','interpreter','latex','FontSize',18); L(4) = title('$z =

1-\sqrt{x^{2}+(y-1)^{2}}$','interpreter','latex','FontWeight','bold');

Now the picture is like this ：

Here you can experience the following LaTeX Difference between formula font and ordinary font , It looks a little better with the axis , But there is still room for improvement . Next, we set the color and color bar of the picture .

<>3. Surface colors and stripes

* colormap winter;： Set the surface color to winter format

winter by matlab A self-contained surface color format , There are other optional formats available through help colormap see

* colorbar;： Add next to the surface colorbar

colorbar The location of can be through colorbar('Position',Location); Make adjustments , among Location Is its coordinate relative to the picture scale （ Non graph coordinates ）

Add code as ：

% Here, the text format of the above coordinate axis is unified colormap winter; % set up colormap Format of colorbar; % Add color bar

At this time, the picture situation becomes ：

It looks like the picture is basically formed , But it's not good-looking , We can rotate , Set the range of coordinate axis and other effects , Optimize pictures .

<>4. Rotation and scaling of pictures

* view(az,el);： Set the viewing angle as (az,el)

* axis([xmin xmax ymin ymax zmin zmax]);： Set the range of the coordinate axis

* set(gcf,'Units','centimeters','Position',[Start_x Start_y Length Width]);

： Set the location of the picture

among Start_x and Start_y Respectively represent the starting position of the image on the screen （ Can be set arbitrarily ）

Length and Width The specific values representing the length and width of the image respectively （ Set according to your own needs ）

So the following code is added later ：

view([50,20]); % Set the viewing angle axis([min(x) max(x) min(y) max(y)... % Set coordinate range

min(min(Z)) max(max(Z))]); % Here because Z It is two-dimensional and requires two-level maximum function

set(gcf,'Units','centimeters','Position',[6 6 20 15]); % Set picture size

The latest pictures are as follows , It's a little like that

notes ： How to determine when rotating az and el Value of ?

* After completing the drawing, click the rotation icon as shown below , Rotation of image can be realized

* When rotating the picture to the desired position , Record its az,el value

<>5. Saving pictures

After the picture is drawn , Can be saved in various formats , Originally, I saved the pictures by clicking manually . But then I realized the happiness of writing code and saving it , Never bother to save it . Share here

Matlab Code to save the image in ：

* saveas(Fig,'Example.png');： Picture Fig（ Is in mesh Value returned when ）, Save as Example.png file

It can also be saved as eps,pdf etc. Matlab File formats that can be saved

notes ： If it appears, save as eps Loss of color in format pictures , Available codes saveas(Fig,'Example.eps','psc2');

<> Complete code ： Complete the complete code of this figure

% Call function file % % function [ Z ] = Func_Sur( X, Y ) % % % Surface functions to be drawn % % Z =

1-sqrt((X).^2+(Y-1).^2); % % end clear;clc; % Clear pre data % Data preprocessing x =

linspace(0,1,50); % set up x Range of axes y = x; % set up y Axis range [X,Y] = meshgrid(x,y); % Put it x,y Axis gridding Z =

1-sqrt((X).^2+(Y-1).^2); % Direct calculation %Z = Func_Sur(X,Y); % Calculate the corresponding Z coordinate It is suggested to call in function form % Relevant parameters

FontS = 16; % Size is 12pt FontW = 'bold'; % Thickness is bold [ Use without bold normal Or default ] az = 50; el = 20;

% Rotation angle setting Length = 20; Width = 15; % Set picture length and width Start_x = 6; Start_y = 6; % Set the starting position of the picture

% Image rendering figure(1) % Define the drawn image as Figure First of Fig = mesh(X,Y,Z); % Draw 3D surface colormap winter;

% set up colormap Format of colorbar; % Add color bar % Image adjustment view([az,el]); % Set the viewing angle axis([min(x) max(x)

min(y) max(y)... % Set coordinate range min(min(Z)) max(max(Z))]) % Here because Z It is two-dimensional and requires two-level maximum function

set(gcf,'Units','centimeters','Position',[Start_x Start_y Length Width]);

% Set picture size % Coordinate adjustment （ Set to LaTeX text format ） L(1) =

xlabel('$x$','interpreter','latex','FontSize',FontS,'FontWeight',FontW); L(2) =

ylabel('$y$','interpreter','latex','FontSize',FontS,'FontWeight',FontW); L(3) =

zlabel('$z$','interpreter','latex','FontSize',FontS,'FontWeight',FontW); L(4) =

title('$z =

1-\sqrt{x^{2}+(y-1)^{2}}$','interpreter','latex','FontSize',FontS,'FontWeight',FontW);

% Image saving saveas(Fig,'Example.png'); % Save as .png format %eps Format save ： Need to increase 'psc2' Otherwise, the image is black and white

%saveas(Fig,'Example.eps','psc2');

Technology

- Java296 blogs
- Python265 blogs
- Vue125 blogs
- C Language122 blogs
- Algorithm108 blogs
- MySQL96 blogs
- Flow Chart84 blogs
- JavaScript79 blogs
- More...

©2020-2024 ioDraw All rights reserved