(内容空余时间再补充,下面图的代码均可用,图皆为对应生成的图形,版本为2023a)
一、二维图
1、plot
x=0:pi/1000:2*pi;
y=sin(2*x+pi/4);
plot(x,y)
2、plot
plot(x,y,x,y+1,x,y+2)
%利用plot函数绘制矩阵数据
A=pascal(5);
plot(A)
3、plotyy
x=0:0.01:20;
y1=100*exp(-0.04*x).*sin(x);
y2=0.8*exp(-0.5*x).*sin(10*x);
plotyy(x,y1,x,y2)
4、对数坐标系绘图
semilogx()%x轴采用对数刻度的半对数坐标系函数
semilogy()%y轴采用对数刻度的半对数坐标系函数
loglog()%x轴和y轴都采用对数刻度的对数坐标系函数
%分别用这三个函数绘制y=e^-x的图形
t=0:0.01*pi:2*pi;%定义坐标轴t的范围及刻度
x=0:0.01:10;%定义x坐标轴范围及刻度
y=exp(-x);%y与x之间的函数关系
subplot(2,2,1)%设置子图
plot(x,y,'r')
title('plot')
subplot(2,2,2)
semilogx(x,y,'--k')
title('semilogx')
subplot(2,2,3)
semilogy(x,y,'-.g','LineWidth',2)
title('semilogy')
subplot(2,2,4)
loglog(x,y,':b','LineWidth',0.5)
title('loglog')
5、用polar函数极坐标系绘图(先版本建议polarplot)
thera表示各个数据点的角度向量,rho表示各个数据点的幅值向量,这两个参数的长度必须一致LineSpec是一个选项函数,与plot选项参数含义相同。极坐标绘图与plot函数绘图相似,但极坐标绘图不支持多参数输入。
t=0:0.01*pi:2*pi;%定义坐标轴的范围和刻度
r=2*sin(2*(t-pi/8)).*cos(2*(t-pi/8));%定义r与t的函数关系
polarplot(t,r)
二、函数绘图
1、fplot函数
格式
fplot(FUN,LIMS) % fplot(函数名,指定范围,指定类型)
fplot(FUN,LIMS,'LineSpec')
subplot(221)
x=0.01:0.001:0.1;
plot(x,sin(1./x));
subplot(222)
fplot('sin(1/x)',[0.01 0.1]);%警告: 在以后的版本中,fplot 将不接受字符向量或字符串输入 fplot(@(x)sin(1./x))。
subplot(223)
sn=@(x)sin(1./x);
fplot(sn,[0.01 0.1]);
subplot(224)
f=@(x,n)sin(n./x);
fplot(@(x)f(x,10),[0.01 0.1]);
2、explot函数
格式:ezplot (FUN) %FUN为绘制函数
explot (FUN2) %FUN2为隐函数,且FUN2(X,Y)=0
explot(FUNX,FUNY, [ timn,tmax] )%[tmin,tmax]绘制x=FUNX,y=FUNY的图形
explot(FUN2,[xmin,xmax,ymin,ymax])%绘制FUN2在x的区间[xmin,xmax],y区间[ymin,ymax]上的图形
subplot(221)
ezplot('x^2-2*x+1')
subplot(222)
ezplot('x.*y+x.^2-y.^2-1')
subplot(223)
ezplot('cos(5*t)','sin(3*pi)',[0,2*pi])
subplot(224)
ezplot('5*x^2+25*y^2=6',[-1.5 1.5 -1,1])
3、ezpolar函数(绘制极坐标系图)
函数表示绘制rho=FUN (theta)的极坐标图,theta为极角,rho为极径,[A,B]为thera指定区间,默认区间为[0,2*pi]
格式:
ezpolar (FUN)
ezpolar (FUN,[A,B])
figure
subplot(121)
ezpolar('sin(2*t)*cos(3*t)')
subplot(122)
ezpolar('sin(2*t)*cos(3*t)',[0 pi/2])
三、特殊类二维绘图
1、柱状图和面积图
bar:绘制二维柱状图
data=[10 2 3 5;5 8 10 3;9 7 6 1;3 5 7 2;4 7 5 3];
subplot(2,2,[1 2]);
bar(data);
title('垂直柱状图')
subplot(2,2,3);
barh(data)
title('水平柱状图')
subplot(2,2,4);
area(data)
title('面积图')
2、直方图
旧版:
A=randn(100000,1);
subplot(2,2,1);hist(A);xlabel('默认均分值')
subplot(2,2,2);hist(A,15);xlabel('均分15份')
subplot(2,2,3);rose(A);xlabel('默认均分值')
subplot(2,2,4);rose(A);xlabel('均分15份')
新版建议用:
A=randn(100000,1);
histogram(A)
polarhistogram(A)
3、饼图
A=sum(rand(5,5));
subplot(1,2,1);
pie(A);
title('完整饼图');
B=[0.18 0.22 0.35];
subplot(1,2,2);
pie(B);
title('缺角饼图');
4、离散数据图
alpha=0.01;
beta=0.5;
t=0:0.2:10;
y=exp(-alpha*t).*sin(beta*t);
subplot(121)
stem(t,y,'r');
grid on;
title('火柴杆图')
subplot(122)
stairs(t,y,'r');
grid on;
title('阶梯图')
5、等值线图
z=peaks;%绘制peaks图形
subplot(2,2,1)
contour(z);
title('peaks图形等值线图')
subplot(2,2,2)
[c,h]=contour(z,[3.8 1.5]);
clabel(c,h)
title('标注等值线图中的函数值')
subplot(2,2,3)
[c,h]=contour(z,4);
clabel(c,h)
title('标注等值线图中的函数值');
subplot(2,2,4)
contourf(z,4)
title('填充等值线图')
6、 二维图设置
(1)曲线格式设置
表:绘图函数控制字符
例:分别用不同的时标、色彩、线型。
t=0:pi/20:2*pi;
y1=sin(t);
y2=sin(t-pi/2);
y3=sin(t-pi);
plot(t,y1,'-.rv',t,y2,'--ks',t,y3,':mp')
曲线常见属性
例:设置曲线的细节属性
x=-pi:pi/10:pi;
y=tan(sin(x))-sin(tan(x));
plot(x,y,'--rs','LineWidth',2,'MarkerFaceColor','g','MarkerSize',10)
(2)图形区域控制
1)坐标轴调整
x=0:pi/100:pi/2;
y=tan(x);
plot(x,y,'ko')
grid on
axis([0,pi/2,0,5])
2)网格线设置
t=0:pi/20:2*pi;
y1=sin(t);
y2=sin(t-pi/2);
y3=sin(t-pi);
plot(t,y1,'-.rv',t,y2,'--ks',t,y3,':mp')
grid on
3)图形叠加
x=0:pi/100:2*pi;
y1=0.2*exp( -0.5.*x).*cos(4*pi*x);
plot(x,y1);
hold on
y2=2*exp(-0.5*x).*cos(pi*x);
plot(x,y2,'r');
hold off
4)子图绘制
x=0:0.1:2*pi;
figure(1);clf;
subplot(2,2,1);plot(1:10);grid on;
subplot(2,2,2);plot(x:sin(x));grid on;
subplot(2,2,3);plot(x,exp(-x),'r');grid on;
subplot(2,2,4);plot(peaks);grid on;
(3)图形标注信息
1)标题title
x=0:pi/100:2*pi;
y=2*exp(-0.5*x);
plot(x,y)
title('x from 0 to 2{\pi}');%特殊符号想加入标题可以用{};
2)图例legend
3)坐标轴标签label
4)文本注释
例:修改文本信息
x=0:0.1:2*pi;
y=sin(x);
plot(x,y)
grid on;
hold on
plot(x,exp(-x),'r:*');
%添加备注
title('2-D Plot','FontName','Arial','FontSize',16)%修改字体大小
xlabel('时间','FontName','隶书','FontSize',16)%使用中文字体
h=ylabel('Sin(t)')%获得ylabel的句柄值
set(h,'FontWeight','Bold')%修改字号
legend('Sine Wave','Decaying Exponential')
hold off
5)图形编辑器
四、三维绘图
1、三维曲线
利用plot3函数分别绘制x=sin(t)、y=cos(t)三维螺旋线,其中t属于[0,8pi]。
t=0:pi/50:8*pi;
x=sin(t);
y=cos(t);
z=t
plot3(x,y,z)
xlabel('x')
ylabel('y')
zlabel('z')
2、三维网络曲面
[X,Y]=meshgrid(-3:0.125:3);
Z=peaks(X,Y);
subplot(2,2,1);
plot3(X,Y,Z)
axis([-3 3 -3 3 -10 10]);title('plot3');
subplot(2,2,2);
meshc(X,Y,Z);
axis([-3 3 -3 3 -10 10]);title('meshc');
subplot(2,2,3);
meshz(X,Y,Z);
axis([-3 3 -3 3 -10 10]);title('meshz');
subplot(2,2,4);
mesh(X,Y,Z);
axis([-3 3 -3 3 -10 10]);title('mesh');
(3)三维阴影曲面
1)阴影曲面函数
2)带有等高线的阴影曲面绘制
x=-8:0.5:8;
y=x;
[X,Y]=meshgrid(x,y);
R=sqrt(X.^2+Y.^2)+eps;
Z=2*sin(R)./R;
surfc(X,Y,Z)
grid on
axis([-10 10 -10 10 -0.5 2])
3)带有光照效果的阴影曲面
x=-8:0.5:8;
y=x;
[X,Y]=meshgrid(x,y);
R=sqrt(X.^2+Y.^2)+eps;
Z=2*sin(R)./R;
s=[0 -1 0];
surfl(X,Y,Z)
grid on
axis([-10 10 -10 10 -0.5 2])
(4)三维图形显示控制
1)视角设置
[x,y,z]=peaks(30);
subplot(221);surf(x,y,z);axis tight;view(-37.5,30);
subplot(222);surf(x,y,z);axis tight;view([1 1 2]);
subplot(223);surf(x,y,z);axis tight;view(2);
subplot(224);surf(x,y,z);axis tight;view(3);
2)光照设置
设置光源对象
利用ligh函数维三维图形z=sin(x*pi)+cos(y*pi)设置光源对象
[x,y]=meshgrid(-1:0.1:1);
z=sin(x*pi)+cos(y*pi);
subplot(221);
surf(x,y,z);
title('no light')
subplot(222);
surf(x,y,z);
light('Color','r','Style','infinite','Position',[0 1 2])
title('red infinite light')
subplot(223);
surf(x,y,z);
light('Color','g','Style','infinite','Position',[0 1 2])
title('green infinite light')
subplot(224);
surf(x,y,z);
light('Color','r','Style','local','Position',[0 1 2])
title('red local light')
设置光照模式
利用lighting函数设置光照模式
subplot(2,2,1);
surf(peaks)
light('Color','r','Style','infinite','Position',[1 -1 2])
lighting none
title('lighting none')
subplot(2,2,2);
surf(peaks);
light('Color','r','Style','infinite','Position',[0 1 2])
lighting flat
title('lighting flat')
subplot(2,2,3);
surf(peaks);
light('Color','r','Style','infinite','Position',[0 1 2])
lighting gouraud
title('lighting gouraud')
subplot(2,2,4);
surf(peaks);
light('Color','r','Style','infinite','Position',[0 1 2])
lighting phong
title('lighting phong')
3)颜色设置
例:利用shading函数设置绘图的阴影模式
[x,y]=meshgrid(-4:0.5:4);
z=x.^2+2*sin(x*pi)+2*cos(y*pi);
subplot(2,2,1);
surf(x,y,z)
title('no shading')
subplot(2,2,2)
surf(x,y,z)
shading flat
title('shading flat')
subplot(2,2,3);
surf(x,y,z)
shading faceted
title('shading faceted')
subplot(2,2,4);
surf(x,y,z)
shading interp
title('shading interp')
matlab涉及的图形基本都在这了,这就当属于自己的matlab图函数小库,可以查可以复习。
end