matlab initial drawing

matlab initial drawing

 plot(cos(0:pi/20:2*pi))

Insert picture description here

Matlab will clear the previous image and keep the latest image

plot(cos(0:pi/20:2*pi));
plot(sin(0:pi/20:2*pi));

what can we do to solve this problem?

hold on
plot(cos(0:pi/20:2*pi));
plot(sin(0:pi/20:2*pi));
hold off

La la la la

Curve nature

plot(x,y,“str”)

hold on
plot(cos(0:pi/20:2*pi),"d-b");
plot(sin(0:pi/20:2*pi),"d--g");
% plot(x,y,'-.g',...'LineWidth', 7.0); 线宽多长
% 就是针对Data markers用颜色填充里面空心的部分
plot(x,'-md','LineWidth', 2, 'MarkerEdgeColor','k',...'MarkerFaceColor','g','MarkerSize', 10);
hold off

Matlab画图plot(X1,Y1,‘b -’,x1,y1,‘ro’,‘MarkerFaceColor’,‘r’)

Insert picture description here

The following 2 pictures compare the Data markers part

Insert picture description hereInsert picture description here

more properties

Insert picture description here

the title of lines

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^-');
legend('sin(x)','cos(x)','Sigmoid','Gauss function');

Insert picture description here

title and label

x = 0:0.1:2*pi; y1 = sin(x); y2 = exp(-x);
plot(x, y1, '--*', x, y2, ':o');
xlabel('t = 0 to 2\pi');   % 斜杠 转义字符
ylabel('values of sin(t) and e^{-x}')  %大括号会把-x当成一个整体 小括号只会把(当成一个整体
title('Function Plots of sin(t) and e^{-x}');
legend('sin(t)','e^{-x}');
Insert picture description here

text and annotation

x = linspace(0,3); y = x.^2.*sin(x); plot(x,y); %0到3的等差数列 n=100
line([2,2],[0,2^2*sin(2)]); %对应的就是画x=2那条直线
str = '$$ \int_{0}^{2} x^2\sin(x) dx $$'; %积分符号latex代码
text(0.25,2.5,str,'Interpreter','latex');  % 积分的坐标位置
annotation('arrow','X',[0.32,0.5],'Y',[0.6,0.4]); 

Mathematical formula code

Insert picture description here

exercise

x=0:0.01:2;
y1=power(x,2);
y2=sin(2*pi*x);
plot(x,y1,'-k',x,y2,'or');
xlabel('Time(ms)');
ylabel('f(t)');
title('Mini Assignment #1')
hleg1=legend('x^2','sin(2/pix)');
set(hleg1,'Location','NorthWest')

Getting Object Properties

Get the attributes of h

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

Insert picture description hereInsert picture description here

gcf refers to figure, gca refers to coordinate axis

set(gca,'XLim', [0, 2*pi]);   %修改X轴的上下限
set(gca,'YLim', [-1.2, 1.2]); %修改Y轴的上下限
% xlim([0, 2*pi]);  等同上面的
% ylim([-1.2, 1.2]);
set(gca,'XTick', 0:pi/2:2*pi);  %修改X轴的上下限
set(gca,'XTickLabel', 0:90:360);  %X轴上对应的数值用 0:90:360表式
set(gca,'FontName', 'latex');
set(gca,'XTickLabel', {'0', '\pi/2', '\pi', '3\pi/2', '2\pi'});
Insert picture description here

Multiple Figures

x = -10:0.1:10;
y1 = x.^2 - 8;
y2 = exp(x);
figure, plot(x,y1);
figure, plot(x,y2);
% figure('Position', [left, bottom, width, height]);
% 设定figurs的位置 
% left, bottom调整位置
% width, height调整图的大小

Matlab drawing position management
Insert picture description here

Several Plots in One Figure

subplot(m, n,1)
%与python中一致m*n个图,1就是代表第一张图,一般用循环for来操纵第i张图
Insert picture description here
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
% axis square/将当前坐标系图形设置为方形。横轴及纵轴比例是1:1
subplot(2, 2, 3); plot(x, y); axis equal   
% axis equal刻度是等长的
subplot(2, 2, 4); plot(x, y); axis equal tight
% 数据范围就是坐标范围
Insert picture description here

The difference between axis square and axis equal in matlab

Summary of axis function usage in Matlab

Insert picture description here
% 附上代码有意者删除%可以测试观察
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
% grid on  %加网格线
% box off  %取消上层和右边的轴
% axis on %取消XY轴

Saving Figures into Files

saveas(gcf,'<filename>','<formattype>');
Vector意思就是无限放大照片照样清晰
Image照片由若干个像素点构成,放大之后像素点稀疏,照片比较模糊
Insert picture description here [PPT in the course of teacher Guo Yanfu, extraction code 7777](https://pan.baidu.com/s/1l2bcuIYlpAEgeD1-eYn0fA) Thank you for reading

Guess you like

Origin blog.csdn.net/qq_46458164/article/details/110748951
Recommended