Matlab drawing basics (title: draw a rhombus, fill it with yellow inside, and a WAIT word filled with blue in the middle.)

Lesson 5 Drawing Basics

1. Drawing instructions: plot() has the same function name

You can check the function
plot(x, y) through the Linspace webpage : draw each point (x, y)
plot(y): draw each point (x, y), x is [1……n],
for example : N = length(y), x increases by 1 by default

2. Graphic annotation

legend(): Used as a curve identification icon, when there are many curves, it can mark which style is which function curve. Will automatically appear in the upper right corner.

3. Title, XYZ coordinate name

title(): Used to label the title of the graph, displayed at the top of the graph
xlabel()/ylabel()/zlabel(): the name of the XYZ axis

4. Limit the specified range of coordinates

linspace: is an instruction in Matlab, used to generate a specified number of points in a specified range, adjacent data have the same span, and return a row vector.
Calling method: linspace(x1,x2,N)

Function: used to generate N-point row vectors between x1 and x2, adjacent data spans are the same. Where x1, x2, and N are the starting value, ending value, and the number of elements, respectively. If the
default is N, the default number of points is 100.

5. Add function expressions to the graph

First write the function expression to be added:

str = '$$\int_{0}^{2} x^2\sin(x) dx $$';
text(0.25,2.5,str,'interpreter','latex');

Use the text() function to add symbols
interpreter: add characters to graphics
latex: add functions

6. Add an arrow to the graph: Annotation

annotation('arrow','x',[0.32,0.5],'Y',[0.6,0.4]);
Specify the position of the start point and end point of the arrow
A
example:

clear;
clc;
x = linspace(0,3);
y = x.^2.*sin(x);
plot(x,y);
line([2,2],[0,2^2*sin(2)]);%x从22,y从02^2*sin(2)
str = '$$\int_{0}^{2} x^2\sin(x) dx $$';
text(0.25,2.5,str,'interpreter','latex');
annotation('arrow','x',[0.32,0.5],'Y',[0.6,0.4]);

result:Insert picture description here

7. Modify the properties of the curve or axis

x = linspace(0,2*pi,1000);...
y = sin(x);  plot(x, y);...
h = plot(x, y);  
get(h)

(1) get(h)
h: is the attribute set of the curve, all the attributes of the curve can be obtained through the statement get(h)
get(gca)
gca: get the attributes of the graph
set(gca,'XLim',[0,2 *pi]);
set(gca,'YLim',[-1.2,1,2]); change x,y coordinates

(2) Set the coordinate system attribute
set(gca,'XLim', [0, 2*pi]);% through the set function to set the range of the X axis from 0 to 2π
set(gca,'YLim', [-1.2, 1.2]);% to set the Y axis range from -1.2 to 1.2

(3) Enlarge the graph.
Use set and fontSize
set(gca,'FontSize',25);% Both the horizontal and vertical coordinates are enlarged.
Set(gca,'XTickLabel',0:90:360)% only changes the horizontal coordinate

8. Axis normal/square/equal/equal tight
axis off can turn
off the axis, box off, and remove the extra part in xy

9. The resolution of print is much higher than that of saves.

Lesson 6 Advanced Drawing

1、logarithm plots

logspace(-1,1,100) means that there are 100
semilogs from the -1 power of 10 to the 1st power of 10, with 100 semilogs taking log on the x axis
semilogy taking log on the y axis
loglog taking log on both axes

2. There are two y-axes: plotyy()

x = 0:0.01:20;
y1 = 200*exp(-0.05*x).*sin(x);
y2 = 0.8*exp(-0.05*x).*sin(10*x);
[AX,H1,H2] = plotyy(x,y1,x,y2);
set(get(AX(1),'Ylabel'),'String','Left Y-axis');
set(get(AX(2),'Ylabel'),'String','Reft Y-axis');
title('Labeling plotyy');
set(H1,'linestyle','--');
set(H2,'linestyle',':');

Effect picture:Insert picture description here

3. 3D graphics drawing function

plot3 (X1, Y2, Z3...)
mesh (x, y, z) commonly used network line map call format
surf (x, y, z) commonly used surface map call format
contour (x, y, z) commonly used contour lines Call format

Effect picture:
Insert picture description here

4. Draw a rhombus or hexagon

fill() fills up the graphics.
Title: Draw a rhombus, fill it with yellow inside, and a WAIT word filled with blue in the middle.
Code:

clear;
hold on;
t = (0:3)'*pi/2
x = cos(t);
y = cos(t+pi/2);
fill(x,y,'y');
axis square off;
text(0,0,'WAIT','Color','b','FontSize',60,...
   'FontWeight','bold','HorizontalAlignment','center');
hold off;

Effect picture:
Insert picture description here

Guess you like

Origin blog.csdn.net/qq_42005540/article/details/108779408