Matlab tutorial (7) - basic drawing function

1. Create the drawing

The plot function has different forms depending on the input arguments.
• If y is a vector, plot(y) produces a segmented line plot of y elements and y element indices.
• plot(x,y) produces a plot of y versus x if two vectors are specified as arguments .
Create a vector of x values ​​from 0 to 2 π using the colon operator , compute the sine of these values, and plot the result.
x = 0:pi/100:2*pi;
y = sin(x);
plot(x,y)

Add axis labels and titles. The character \pi in the xlabel function is used to create the symbol π . The FontSize property in the title function is used to increase the size of the text used for the title.
xlabel('x = 0:2\pi')
ylabel('Sine of x')
title('Plot of the Sine Function','FontSize',12)
 

2 draw multiple graphics

        With one call to plot , multiple xy pair arguments create multiple graphs. MATLAB® uses a different color for each line. For example, the following statements plot three correlated functions of x :
x = 0:pi/100:2*pi;
y = sin(x);
y2 = sin(x-.25);
y3 = sin(x-.5);
plot(x,y,x,y2,x,y3)

The legend function provides an easy way to identify individual lines:
legend('sin(x)','sin(x-.25)','sin(x-.5)')

3 Specify line type and color

When plotting data with the plot command, you can specify the color, line style, and markers (such as plus signs or circles):
plot(x,y,'color_style_marker')
color_style_marker contains one to four characters (included in single quotes) that are constructed from the color, line style, and marker type. For example,
plot(x,y,'r:+')
Plot the data using red dotted lines and place a + mark at each data point. color_style_marker consists of a combination of the following elements.

 

4 Draw lines and markers

If you specify a marker type but not a line type, MATLAB creates the figure using only the markers, not the lines. For example,
plot(x,y,'ks')
Draws black squares at each data point, but does not connect markers with lines. sentence
plot(x,y,'r:+')
Draw a red dotted line and place a plus sign at each data point.

5 Place markers at every tenth data point

This example shows how to draw a marker using fewer data points than a line. It plots the data twice using a point-line plot and a marker plot (each with a different number of data points):
x1 = 0:pi/100:2*pi;
x2 = 0:pi/10:2*pi;
plot(x1,sin(x1),'r:',x2,sin(x2),'r+')

6 Plotting imaginary and complex data

MATLAB ignores the imaginary part when multiple complex values ​​are passed as arguments to plot , except when a single complex argument is passed. For this particular case, this command is a shortcut for plotting the imaginary part versus the real part. therefore,
plot(Z)
where Z is a complex vector or matrix, equivalent to
plot(real(Z),imag(Z))
The following statement draws a 20-sided polygon with a small circle at each vertex.
t = 0:pi/10:2*pi;
plot(exp(1i*t),'-o')
axis equal

The axis equal command makes the tick mark increments on the x and y axes the same length, which makes the plot look more rounded.

7 Adding plots to existing graphics

The hold command is used to add plots to an existing graph. When you type hold on , MATLAB does not replace the existing graph when you issue additional plotting commands. Instead, MATLAB merges the new graph with the current graph. For example, the following statement first creates a surface plot of the peaks function and then overlays a contour plot of the same function:
[x,y,z] = peaks;
% Create surface plot
surf(x,y,z)
% Remove edge lines a smooth colors
shading interp
% Hold the current graph
hold on
% Add the contour graph to the pcolor graph
contour3(x,y,z,20,'k')
% Return to default
hold off

8 figure window

If a figure window has not been created, the plotting function automatically opens a new figure window. If multiple figure windows are open, MATLAB uses the figure window designated as the "current figure" (usually the last used figure).
To make an existing figure window the current figure, place the pointer in the window and click, or type
figure(n)
where n is the number in the figure title bar. To open a new figure window and make it the current figure, type
figure
9 Clear the figure to create a new plot
If a figure already exists, most plotting commands clear the axes and create a new plot using the figure. However, these commands do not reset figure properties such as background color or colormap. If figure properties have been set in a previous plot, you can first use the clf command with the reset option .
clf reset
Then create a new plot to restore the figure's properties to their default values.

9 Display multiple plots in one figure

The subplot command is used to display multiple plots in the same window, or to print these plots on the same sheet. type the following command
subplot(m,n,p)
will divide the figure window into an m × n matrix of small subplots , and select the pth subplot as the current plot. The plots are numbered along the first row of the figure window, then along the second row, and so on. For example, the following statements plot data in three subregions of the figure window:
x = 0:pi/20:2*pi;
subplot(3,1,1); plot(sin(x))
subplot(3,1,2); plot(cos(x))
subplot(3,1,3); plot(sin(x).*cos(x))

10 control axes

The axis command provides a number of options for setting the scale, orientation, and aspect ratio of a graph.

10.1 Automatically change the axis range and tick marks

By default, MATLAB finds the maximum and minimum values ​​of the data and chooses the axis limits to cover this range. MATLAB chooses the range and axis tick values ​​so that it produces a graph that clearly displays the data. However, you can use the axis or xlim , ylim and zlim functions to set your own limits.
Note that changing the range of one axis may cause the other ranges to change to better represent the data. To disable automatic range setting, enter the axis manual command.

10.2 Set the axis range

The axis command is used to specify your own range:
axis([xmin xmax ymin ymax])
or for 3D graphics,
axis([xmin xmax ymin ymax zmin zmax])
Please use the command
axis auto
Re-enable automatic range selection.

10.3 Setting Axis Aspect Ratio

The axis command can also be used to specify various predefined modes. For example,
axis square
Make the x-axis and y-axis the same length.
axis equal
Make each tick mark increment the same length on the x-axis and y-axis. this means
plot(exp(1i*(0:pi/10:2*pi)))
(followed by axis square or axis equal ) will turn an ellipse into a perfect circle:
axis auto normal
Return the axis scale to its default automatic mode.

10.4 Setting axis visibility

Use the axis command to show or hide axes.
axis on
Show axes. This is the default setting.
axis off
Hides the axes.

10.5 Setting grid lines

The grid command enables and disables gridlines. statement
grid on
gridlines are enabled, while
grid off
Disable gridlines again.

10.6 Adding axis labels and titles

This example shows how to create a graph and enhance its display:
• define the range of the x and y axes ( axis )
• Add labels to the x and y axes ( xlabel , ylabel )
• Add a title ( title )
• Add text annotations ( text ) to graphics
Generate mathematical notation using LaTeX notation. For information on placing arrows, boxes, and circles in graphics, see the annotation function.

11 Save the figure

Save the figure by choosing Save from the File menu . This will write the figure to the file, including property data, figure menu, uicontrol, and all annotations (that is, the entire window). If the figure has not been saved before, the Save As dialog box appears. This dialog box provides options for saving the figure as a .fig file or exporting it to a figure format.
If the figure has been saved before, using Save again will "silently" save the figure without the Save As dialog box appearing.
To save the figure in a standard graphics format (for example, TIFF or JPG) for use in another application, choose Save As from the File menu (or Export Settings if you need additional controls ).
Note When you specify the format in which to save the figure, that file format will be used again the next time you save the figure or a new figure. If you don't want to save in the format you were previously using, use Save As and make sure the Save as type drop-down is set to the file type you want to write. It can also be saved through the following command line:
• Use the savefig function to save a figure and the graphics objects it contains to a .fig file.
• Save figures in various formats using the saveas command with any options.

11.1 Loading Figures

You can load figures into MATLAB using the following functions:
• Use the openfig function to load figures saved as .fig files.
• Use the imread function to read standard graphics files (including saved figures) into MATLAB.

11.2 Generate MATLAB code to rebuild the figure

You can generate the MATLAB code for recreating a figure and the graphics it contains by selecting Generate Code from the Figure File menu . This option is especially useful if you have created a graph using drawing tools and want to create a similar graph using the same or different data.

12 Save workspace data

Variables in the workspace can be saved by choosing Save Workspace As from the figure File menu . Saved data can be reloaded using the Import Data item on the figure's File menu. MATLAB supports several data file formats, including MATLAB data files, which have a .mat extension .

 

 

Guess you like

Origin blog.csdn.net/weixin_44209907/article/details/132051865