天天看点

第二十一章 绘图基础

line函数

% x=1:10;
x=1:0.3:10;
y=sin(x);
line(x,y);
           
第二十一章 绘图基础

plot 函数

语法

plot(x)

plot(x,y)

plot(x1,y1,x2,y2,…,xn,yn)

>> x=[43 91 18 26 15]

x =

    43    91    18    26    15

>> plot(x)
           
第二十一章 绘图基础
x=0:0.1:2*3.14;
y=sin(x);
plot(x,y);
           
第二十一章 绘图基础
x=1:5;
y=rand(4,5);
plot(x,y);
           
第二十一章 绘图基础
x1=0:0.1:2*3.14;
y1=sin(x1);
x2=3.14:0.1:3*3.14;
y2=cos(x2);
plot(x1,y1,x2,y2);
           
第二十一章 绘图基础
x=0:0.1:2*3.14;
y1=sin(x1);
y2=cos(x2);
plot(x,y1,x,y2);
           
第二十一章 绘图基础

polar函数

极坐标下的绘图函数

语法:polar(theta,rho) 对应于polar(θ,ρ)

a=-2*3.14:0.01:2*pi;
b=sin(a);
polar(a,b);
           
第二十一章 绘图基础
a=-2*3.14:0.01:2*pi;
b=sin(a);
c=cos(a);
d=(1-sin(a));
polar([a,a,a],[b,c,d]);

           
第二十一章 绘图基础

线性控制

  • 线的格式

    plot(x,y,线的格式)

    第二十一章 绘图基础
x=0:0.01:2*pi;
y=sin(x);
plot(x,y,'-.');
% plot(x,y,'linestyle','none');
           
第二十一章 绘图基础

线条颜色控制

第二十一章 绘图基础
a=0:0.1:2*pi;
b=sin(a);
plot(x,y,'-.','color','red');
%plot(x,y,'-.r');
           
第二十一章 绘图基础

数据点格式

marker

plot(x,y,数据点的格式)

第二十一章 绘图基础
a=0:0.1:2*pi;
b=sin(a);
plot(a,b,'marker','v');
plot(a,b,'-ro');%可以隐形合起来用,没有顺序之分
           
第二十一章 绘图基础

曲线其他格式

plot(x,y,属性,格式)

第二十一章 绘图基础
x=0:0.1:2*pi;
y=sin(x);
plot(x,y,'linewidth',6);
plot(x,y,'o','markeredgecolor','m');
plot(x,y,'o','markerfacecolor','r');
plot(x,y,'o','markersize',12);
           

结果省略

子图绘制subplot

语法:subplot(x,y,i)

x=0:0.1:2*pi;
y1=sin(x);
y2=cos(x);
y3=x;
y4=x.^2;
subplot(2,2,1),plot(x,y1);
subplot(2,2,2),plot(x,y2);
subplot(2,2,3),plot(x,y3);
subplot(2,2,4),plot(x,y4);
           
第二十一章 绘图基础

叠加绘图模式hold

语法: hold on 和 hold off

x=0:0.1:2*pi;
y1=sin(x);
y2=cos(x);
y3=1-sin(x);
plot(x,y1);
hold on;%开启叠加绘图模式
plot(x,y2);
plot(x,y3);
%hold off;%关闭叠加绘图模式
%plot(x,y3);
           
第二十一章 绘图基础

设置坐标轴axis

坐标轴范围

axis([X轴范围,Y轴范围])

axis auto

axis manual

axis tight

坐标轴比例

axis equal

axis square

axis normal

关闭坐标轴

axis off

% % 坐标轴范围
% % axis([X轴范围,Y轴范围])
% % axis auto
% % axis manual
% % axis tight
% % axis off

% x=0:0.1:4*pi;
% y=sin(x);
% plot(x,y);axis([-100 100 -100 100]);
% x2=-100:100;
% y2=cos(x2);
% plot(x,y);axis manual;%用原先xy的范围

% 坐标轴比例
% axis equal 
% axis squarev 方形
% axis normal 默认情况

% x=0:0.1:4*pi;
% y=sin(x);
% plot(x,y); axis equal;%横纵坐标比例相同
% plot(x,y); axis tight normal;

% 关闭坐标轴
x=0:0.1:4*pi;
y=sin(x);
plot(x,y); axis off;
           

坐标轴刻度

set(gca,‘xTick’,刻度)

set(gca,‘xTickLabel’,刻度)

x=0:0.1:2*pi;
y=sin(x);
plot(x,y);
set(gca,'xTick',[1 pi/2 pi 3*pi/2 2*pi]);
set(gca,'xTickLabel',{'0''pi/2' 'pi' '3*pi/2' '2*pi'});
           
第二十一章 绘图基础

对数坐标系

  • 设置对数坐标

    semilogx x轴使用对数

    semilogy y轴使用对数

    loglog xy轴均使用对数

x=0:0.1:10;
y=exp(x);
subplot(2,2,1),plot(x,y);
subplot(2,2,2),semilogx(x,y);
subplot(2,2,3),semilogy(x,y);
subplot(2,2,4),loglog(x,y);

           
第二十一章 绘图基础

双纵轴绘图

语法:plotyy(x1,y1,x2,y2)

[ax,h1,h2]=plotyy(参数)

ax ax(1)左侧坐标句柄,ax(2)右侧坐标句柄

h1 左侧坐标图线句柄

h2右侧坐标图线句柄

x=1:100;
y1=rand(1,100)*1000;
y2=x.^2;
% plotyy(a,y1,x,y2);
[ax,ha,hb]=plotyy(x,y1,x,y2);
set(ha,'color','g');
set(hb,'color','m');
ylabel(ax(1),'随机数');%设置左边标签
ylabel(ax(2),'平方计算'); %右边标签
           
第二十一章 绘图基础

绘图开关

axis on/axis off 坐标轴

box on/box off 边界线

grid on/gird off 网格线

x=0:0.1:2*pi;
y=sin(x);
subplot(2,2,1),plot(x,y);
subplot(2,2,2),plot(x,y),axis off;
subplot(2,2,3),plot(x,y),box off;
subplot(2,2,4),plot(x,y),grid on;
           
第二十一章 绘图基础