一:Basic plotting——plot
- plot(x,y)
- plot(y) 横坐标默认为1,2,3,4,5…
plot(cos(0:pi/20:2*pi));
hold on%实现在一个图框里面绘制多张图
plot(cos(0:pi/20:2*pi));
plot(sin(0:pi/20:2*pi));
hold off
- Plot style
plot(x,y,‘dataLineColor’)
plot(cos(0:pi/20:2*pi),'d:m');
plot(cos(0:pi/20:2*pi),'p-r');
- plot在同一个坐标轴画多个图
plot(x,y,str1,x,y2,str2,x,y3,str3....)
等价于
hold on
plot(x,y,str1)
plot(x,y2,str2)
plot(x,y3,str3)
...
hold off
5.注解function
- legend(‘L1’,‘L2’,‘L3’…)
给图像标注并引入图例——顺序遵循plot画图的顺序
- title(str)
- xlabel(str)
- ylabel(str)
- zlabel(str)
x=0:0.1:2*pi;
y1=sin(x);
y2=exp(-x);
plot(x,y1,'p--g',x,y2,'*:m');
xlabel('t = 0 to 2\pi');
ylabel('values of sin(t) and e^{-x}');
title('Function Plots of sin(t) and e^{-x}');
legend('sin(t)','e^{-x}');
- text()
text(x位置,y位置,str,‘Interpreter’,‘latex’);
- annotation()
annotation(‘arrow’,‘X’,[start1,end1],‘Y’,[start2,end2])
数字表示占整张图片的百分比
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])
练习
t=1:0.01:2;
f=t.^2;
g=sin(2*pi*t);
plot(t,f,'-b',t,g,'or');
xlabel('Time(ms)');
ylabel('f(t)');
title('Mini Assignment #1');
legend('t^2','sin(2\pi t)');
二:Graphical object properties
1.object hierarchy
2.手动右击图片编辑打开属性检查器,可以看到不同object的属性列表
3.代码修改不同object的属性
先绘个简单的图
x=0:pi/20:2*pi;
y=sin(x);
plot(x,y);
ylabel('f(t)=sin(t)');
xlabel('t');
- Identify the handle(id) of each object
object | handle |
---|---|
axis | gca |
figure | gcf |
line | h=plot() |
other functions of object
allchild | 返回一个object的所有孩子 |
---|---|
ancestor | 返回一个object的所有祖先 |
delete (handle) | 删除一个object |
findall | 返回所有objects |
- Fetch or alter the object’s properties
function | meaning |
---|---|
get(handle) | 返回handle指向的object的所有properties |
set(handle,property,属性值) | 更改handle指向的object的指定property的值 |
get(h);
get(gca);
get(gcf);
set(gca,'XLim',[0,2*pi]);
set(gca,'YLim',[-1.2,1.2]);
4. 练习
a.修改Axis的 font (字体)和 tick(记号)
set(gca,'FontSize',25);
set(gca,'XTick',0:pi/2:2*pi);%短横线位置
set(gca,'XTickLabel',0:90:360);%短横线内容
set(gca,'FontName','TeX');
set(gca,'XTickLabel',{'0','\pi/2','\pi','3\pi/2','2\pi'});
set(h,'LineStyle','-.','LineWidth',7.0,'Color','g');
b.plot()时,指定marker
x=rand(20,1);
set(gca,'FontSize',18);
plot(x,'md','LineWidth',2,'MarkerEdgeColor','k','MarkerFaceColor','g','MarkerSize',10);
set(gca,'XTick',0:5:20);
set(gca,'YTick',0:0.2:1);
c.实现下图转换
t=1:0.01:2;
f=t.^2;
g=sin(2*pi*t);
hold on
h1=plot(t,f);
h2=plot(t,g);
xlabel('Time(ms)');
ylabel('f(t)');
title('Mini Assignment #1');
legend('t^2','sin(2*\pi t)');
set(gca,'FontSize',16);
set(h1,'LineWidth',2);
set(h2,'Marker','o');
5.figure
1.画多幅图在不同的figure
figure,plot(x,y);
figure,plot(y1);
在没有指定情况下,gca ,gcf等所有操作,指代的都是cuttent figure
2.分区画图,一个figure有很多子图
subplot(m,n,d); %1<=d<=m*n
plot(x,y);
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;
6.保存figure
saveas(gcf,'<filename>','<formattype>');