绘图基础
plot(x,y)%如果只给一个值,那默认x为1,2,3…
hold on%保留之前的图像
hold off%不保留之前的图像
plot(x,y,‘str’)%通过str来改变图形属性
str:
Data markers | Line types | Color |
---|---|---|
‘.’ ‘*’ ‘X’ ‘o’ ‘+’ ‘s’ ‘d’ ‘p’ ‘v’ ‘^’ ‘<’ ‘>’ ‘H’ | ‘-’ ‘–’ ‘-.’ ‘:’ | ‘k’ ‘b’ ‘c’(cyan) ‘g’ ‘m’(magenta) ‘r’ ‘w’ ''y |
若一个图中有多条曲线,使用legend(‘name1’,‘name2’,…)为每条曲线添加标注
title()%添加绘图标题
xlabel()%添加x轴标签
ylabel()%添加y轴标签
zlabel()%添加z轴标签
如果在标题或标签中要用特殊格式需要用{},例如pi,e^{x}
在绘图中添加文本,需要使用LaTex格式:
eg.
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 \ sinx dx $$';
text(0.25,2.5,str,'Interpreter','latex');%前面两个参数表示文本的开始位置
annotation('arrow','X',[0.32,0.5],'Y',[0.6,0.4]);%显示箭头,X,Y分别表示箭头起始和中止的位置
绘图属性调整:
点击绘图中相关对象会自动显示其属性
h=plot(x,y)%创建该绘图的指针,以后可以通过直接调用指针来修改绘图
Utility function:
function | purpose |
---|---|
gca | return the handle of the “current” axes |
gcf | return the handle of the “current” figure |
allchild | find all children of specified objects |
ancestor | find ancestor of graphics object |
delete | delete an object |
findall | find all graphics objects |
get(h)%返回绘图h的属性
set(gca,‘XLim’,[0,2pi])%设置绘图h的gca返回属性的XLim为[0,2pi]
也可以直接xlim([1,2*pi])
set(gca,‘FontSize’,25)%设置字体为25
set(h,‘LineStyle’,’-.’,‘LineWidth’,7.0,‘Color’,‘g’)%简单易懂
x=rand(20,1);set(gca,'FontSize',18);
plot(x,'-md','LineWidth',2,'MarkerEdgeColor','k','MarkerFaceColor','g','MarkerSize',10)
%'-md'表示显示标记
xlim([1,20])
当需要画两个绘图对象时加figure:
figure,plot(x,y1);figure,plot(x,y2);
figure(‘Position’,[left,bottom,width,height]);%指定figure的位置与大小
在一个figure中画多图:
subplot(m,n,1)%m行n列的第一个图
eg.
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
operation | purpose |
---|---|
grid on/off | make the grid visible or invisible |
box on/off | make the box visible or invisible |
axis on/off | make the axis visible or invisible |
axis normal | automatically adjust the aspect ratio of the axes and the relative scaling of the data units |
axis square | make the current axes region square |
axis equal | set 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 the 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 |
保存绘图:
saveas(gcf,'<filename>','<formattype>');
formattype option:
option | bitmap image format |
---|---|
‘jpeg’ | JPEG 24-bit |
‘png’ | PNG 24-bit |
‘tiff’ | TIFF 24-bit(compressed) |
‘bmpmono’ | BMP Monochrome |
‘bmp’ | BMP 24-bit |
‘bmp256’ | BMP 8-bit(256 color,use a fixed colormap) |
option | vector graphics format |
---|---|
‘pdf’ | full page portable |
‘eps’ | encapsulated postscript(EPS) level 3 black and white |
‘epsc’ | encapsulated postscript(EPS) level 3 color |
‘meta’ | enhanced Metafile(Windows only) |
‘svg’ | SVG(scalable vector graphics) |
‘ps’ | full-page postscript(ps) level 3 black and white |
‘psc’ | full-page postscript(ps) level 3 color |
绘图进阶
x=logspace(-1,1,100);%产生100个10-1至101的数
semilogx(x,y);%用10x做x轴坐标绘图,类似有semilogy
loglog(x,y);%x轴,y轴均用10x
plotyy(x,y1,x,y2);%在一个绘图中绘制左右两个y轴
hist(y,n)%统计直方图,n表示有几个条,统计y的整体情况
bar(x);%统计每个数值的情况,横轴为从0~n
bar3(y);%3D展示
eg.
x=[1 2 5 4 8];y=[x;1:5];
subplot(1,3,1);bar(x);title('one');
subplot(1,3,2);bar(y);title('two');
subplot(1,3,3);bar3(y);title('three');
barh(y);%横向展示
Pie chart
eg.
a=[10 5 20 30];
subplot(1,3,1);pie(a);
subplot(1,3,2);pie(a,[0,0,0,1]);%1表分割
subplot(1,3,3);pie3(a,[0,0,0,1]);
Polar chart
eg.
x=1:100;theta=x/10;r=log10(x);
subplot(1,4,1);polar(theta,r);
theta=linspace(0,2*pi);r=cos(4*theta);
subplot(1,4,2);polar(theta,r);
theta=linspace(0,2*pi,6);r=ones(1,length(theta));
subplot(1,4,3);polar(theta,r);
theta=linspace(0,2*pi);r=1-sin(theta);
subplot(1,4,4);polar(theta,r);
Stairs and Stem Charts%类似于离散
eg.
x=linspace(0,4*pi,40);y=sin(x);
subplot(1,2,1);stairs(y);
subplot(1,2,2);stem(y);
Boxplot%箱形图
ErroeBar%误差图
eg.
x=0:pi/10:pi;y=sin(x);
e=std(y)*ones(size(x));%std(y)表示y的标准差
errorbar(x,y,e);
fill();%填充图形颜色
eg.
t=(1:2:15)'*pi/8;x=sin(t);y=cos(t);
fill(x,y,'r');axis square off;
text(0,0,'STOP','Color','w','FontSize',80,...
'FontWeight','bold','HorizontalAlignment','center');
颜色配置
[R G B]%0~255
colorbar;%若是颜色表示数据的图,显示颜色数值条
colormap(hot/cool/gray/[name]);%使整个色图偏暖色系/冷色系/灰色
colormap(prism);%prism是颜色图数组
也可以用户自己定义颜色数组,eg.
a=ones(256,3);
colormap(a);
3D
plot3(x,y,z);
xlabel(‘str’);ylabel(‘str’);zlabel(‘str’);
如果需要绘制函数z=f(x,y);需要先生成x,y的矩阵网格
再使用mesh()或是surf()绘制,mesh()和surf()的区别见效果图
x=-3.5:0.2:3.5;y=-3.5:0.2:3.5;
[X,Y]=meshgrid(x,y);
Z=X.*exp(-X.^2-Y.^2);
subplot(1,2,1);mesh(X,Y,Z);
subplot(1,2,2);surf(X,Y,Z);
contour();%3D转2D,类似于等高线图和电位图eg.
x=-3.5:0.2:3.5;y=-3.5:0.2:3.5;
[X,Y]=meshgrid(x,y);
Z=X.*exp(-X.^2-Y.^2);
subplot(1,3,1);contour(Z,[-.45:.05:.45]);axis square;%指定等差间隔
subplot(1,3,2);[C,h]=contour(Z);
clabel(C,h);axis square;%标记等高线数值
subplot(1,3,3);contourf(Z);axis square;%contourf=contour fill,填充色彩
meshc()和surfc()%再mesh和surf的基础上在图下绘制contour
view(x,y);%观察三维图的角度,x表示偏离y轴负半轴的角度,y表示偏离z轴正半轴的角度的余角
light();%给3D图加光
patch();%空间绘制多边立体图
sao话一下
愿天下有情人终成眷属