matlab——基础绘图
Basic Plotting
plot()
plot(cos(0:pi/20:2*pi));
hold on/off
hold on %保留图形
plot(cos(0:pi/20:2*pi));
plot(sin(0:pi/20:2*pi));
hold off %消除图形
plot style
-
可在command window中输入 help linespec 查询
hold on %保留图形
plot(cos(0:pi/20:2pi),‘or–’);
plot(sin(0:pi/20:2pi),‘xg–’);
hold off %消除图形
legend()Add legend to graph
Position adjustment
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');
![在这里插入图片描述](https://img-blog.csdnimg.cn/20190416170251576.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzM4OTIwMTU2,size_16,color_FFFFFF,t_70)
title() and ?label()
- title()
- xlabel()
- ylabel()
- zlabel()
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}')
title('Funciton Plots of sin(t) and e^{-x}');
legend('sin(t)','e^{-x}');
![在这里插入图片描述](https://img-blog.csdnimg.cn/20190416171507984.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzM4OTIwMTU2,size_16,color_FFFFFF,t_70)
text() and annotation()
- text with mathematical expression using LaTex
x = linspace(0,3);y = x.^2.*sin(x); plot(x,y);
line([2,2],[0,2^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]);
exercise
t=1:0.01:2;
f=t.^2;g=sin(2*pi*t);
plot(t,f,'k-',t,g,'ro');
legend('t^2','sin(2\pit)');
xlabel('Time (ms)');
ylabel('f(t)');
title('Mini Assignment #1')
Figure Adjustment
- Several properties:
- Font
- Font size
- Line width
- Axis limit
- Tick position
- Tick label
Graphical Objects
- Figure object
- Axes object
- Line object
Figures→Edit→Figure properties→选中figure/axes/line object→more properties
Modifying Properties of An Object
- Strategy:
1.Identify the “handle” of an object
2.Fetch or modify the object’s properties
1.Identify the “handle” of an object
Utility functions:
gca: return the handle of the “current” axes
gcf : return the handle of the “current” figure
allchild : find all children of specified objects
delete : delete an object
findall : find all graphic objects
2.Fetch or modify the object’s properties
getting object properties: get()
x = linspace(0,2*pi,1000);
y = sin(x) ; plot(x,y);
h = plot(x,y) ; get(h)
get(gca) % axes object的一系列信息
![在这里插入图片描述](https://img-blog.csdnimg.cn/20190416213657789.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzM4OTIwMTU2,size_16,color_FFFFFF,t_70)
setting axes limits
set(gca,'XLim',[0, 2*pi]);
set(gca,'YLim',[-1.2, 1.2]);
![在这里插入图片描述](https://img-blog.csdnimg.cn/20190416213747153.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzM4OTIwMTU2,size_16,color_FFFFFF,t_70)
Setting Font and Tick of Axes
set(gca,'Fontsize',25); %坐标轴的坐标点字号变大
set(gca,'XTick',0:pi/2:2*pi);
set(gca,'XTickLabel',0:90:360);
set(gca,'FontName','symbol');
set(gca,'XTicklabel',{'0','p/2','p','3p/2','2p'})
![在这里插入图片描述](https://img-blog.csdnimg.cn/20190416215202177.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzM4OTIwMTU2,size_16,color_FFFFFF,t_70)
Line Specification
Line style and width
set(h,'LineStyle','-.','LineWidth',7.0,'Color','g');
![在这里插入图片描述](https://img-blog.csdnimg.cn/20190416220450517.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzM4OTIwMTU2,size_16,color_FFFFFF,t_70)
Try : delete(h)
结果是: line消失,Axes仍在
![在这里插入图片描述](https://img-blog.csdnimg.cn/20190416220629833.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzM4OTIwMTU2,size_16,color_FFFFFF,t_70)
Marker Specification
Multiple Figures
create a figure window by calling figure
x = -10:0.1:10;
y1 = x.^2-8;
y2 = exp(x);
figure, plot(x,y1);
figure, plot(x,y2);
Be careful when using the gcf handle where there exists muliple figures
(同时画出两个figure,gcf和gca只指向第二个,第一个的properties已经找不到了)
Figure Position and Size
figure(‘Position’,[left,bottom,width,height]);
-![在这里插入图片描述](https://img-blog.csdnimg.cn/20190417093549460.jpg?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzM4OTIwMTU2,size_16,color_FFFFFF,t_70)
Several Plots in One Figure
-
several small plots “in a figure”
subplot(m,n,1);
m:number of rows
n: number of columns
1:先行后列第一个
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 %表示x轴和y轴的单位长度相同
subplot(2,2,4); plot(x,y) ; axis equal tight %设置坐标轴的范围为数据的范围且横轴纵轴单位长度相同
Control of Grid,Box,and Axis
grid on/off Make the grid visible or invisible
box on/off Make the box visible or invisible
axis on/off Make the axes visible or invisible
axis normal Automatically adjust the aspect ratio of axes and the relative scaling of the data units
axis squal Make the aspect ratio so that the data units are the same in every direction
axis equal tight Set the axis limits to the range of data
axis image Let the plot box fits tightly around the data
axis ij Place the origin of the coordinate system in the upper left corner
axis xy Place the origin in the lower left corner
Saving Figures into Files
saveas(gcf,’< filename>’,’< formattype>’);