Matlab初阶绘图(2022b)
绘图函数
plot( ) & others
-
plot(x,y) plots each vector pairs (x,y)
-
plot(y) plots each vector pairs (x,y), where x=[1 2 … n]
Example:
plot(cos(0:pi/10:2*pi));
-
if you want to draw 2 or plots, use
hold on/off
to have both plots in one figure.hold on plot(cos(0:pi/20:2*pi)); plot(sin(0:pi/20:2*pi)); hold off
-
about plot style:
plot(x,y,'str')
plots each vector pairs (x,y) using the format defined instr
.hold on plot(cos(0:pi/20:2*pi),'or--'); plot(sin(0:pi/20:2*pi),'^b:'); hold off % codes below are wrong! --> plot(cos(0:pi/20:2*pi),'or--',sin(0:pi/20:2*pi),'^b:') %错误使用 plot %数据必须为单个 y 值输入或者一对或多对 x 和 y 值。
POLISHMENT
-
Add legend to graph
legend('L1',...)
x=0:0.5:4*pi; y=sin(x); h=cos(x); w=1./(1+exp(-x)); g=(1/(2*pi*2)^0.5).*exp((-1.*(x-2*pi).^2)./(2*2^2)); plot(x,y,'bd-',x,h,'gp:',x,w,'ro-',x,g,'c^-');
Lack ‘name’, title, xlable, ylable …
Make a little adjustment !
legend('sin(x)','cos(x)','Sigmoid','Gauss function'); xlabel('x=0 to x=4\pi'); ylabel('values of different functions'); title('Function Plots'); % 引号内可以使用Markdown写法
-
text( ) & annotation( )
x = linspace(0,3); % x的取值范围 y = x.^2.*sin(x); plot(x,y); line([2,2],[0,2^2*sin(2)]); % 括号内前者为line的x坐标范围,后者同理,为y取值范围 str = '$$\int_{0}^{2} x^2\sin(x) \textup dx$$'; text(0.25,2.5,str,'Interpreter','latex'); % Latex写法好麻烦啊,$$转义,'\test'貌似不能用 % 前两个数为公式起始点坐标(自变量的值) annotation('arrow','X',[0.32,0.45],'Y',[0.6,0.4]); % XY后的范围貌似指的是在图窗中的比例(即横纵都为1,起点与坐标原点重合)
-
Excercise
t=linspace(1,2); % t=1:0.1:2; % 找不同 f=t.^2; % 从矩阵运算角度理解为啥要用点乘幂。。。 g=sin(2*pi*t); plot(t,f,'k-',t,g,'ro'); legend('t^2','sin2\pit'); xlabel('Time(ms)'); ylabel('f(t)'); title('Mini Assignment #1');
Figure Adjustment
Several properties:
Font & Size, Line width, Axis limit, Tick position, Tick label
Identify the Handle of An Object
When create
h = plot(x,y);
‘h’ is the handle of this ‘line’ of function.
To fetch properties, use
get ( )
; To modify properties, useset ( )
Setting examples:
Axis:
x = linspace(0, 2*pi, 1000);
% 1000为点的数量,不输默认100
y = sin(x); h = plot(x,y);
get(h)
...
% codes above
set(gca, 'XLim', [0, 2*pi]);
set(gca, 'YLim', [-1.2, 1.2]);
% modify the length of axis
set(gca, 'FontSize', 25);
set(gca, 'XTick', 0:pi/2:2*pi);
set(gca, 'XTickLabel', 0:90:360);
% xtick 的替换
set(gca, 'FontName', 'sym');
set(gca, 'XTickLabel', {'0', '\pi/2', '\pi', '3\pi/2', '2\pi'});
% symbol(sym)
Line:
...
set(h, 'LineStyle', '-.','LineWidth', 7.0, 'Color', 'g');
% another choice ->
% plot(x,y, '-.g','LineWidth', 7.0);
Marker:
x=rand(20,1); set(gca, 'FontSize', 18);
plot(x,'-md','LineWidth', 2, 'MarkerEdgeColor', 'k','MarkerFaceColor', 'g', 'MarkerSize', 10);
xlim([1, 20]);
% equal to 'set(gca, 'XLim', [1,20]);'
Other applications
存在多图时进行图操作,操作的是当前图 (current plot), 例如最后一张图上的操作。
draw many lines in different axis:
x = -10:0.1:10;
y1 = x.^2 - 8;
y2 = exp(x);
figure, plot(x,y1);
figure, plot(x,y2);
position:
draw many plots in one window:
subplot( )
t = 0:0.1:2*pi; x = 3*cos(t); y = sin(t);
subplot(2, 2, 1); plot(x, y); axis normal
subplot(2, 2, 2); plot(x, y); axis square
subplot(2, 2, 3); plot(x, y); axis equal
subplot(2, 2, 4); plot(x, y); axis equal tight