matlab绘图“小仓库”

(内容空余时间再补充,下面图的代码均可用,图皆为对应生成的图形,版本为2023a)

一、二维图

1、plot

x=0:pi/1000:2*pi;
y=sin(2*x+pi/4);
plot(x,y)

17ac08d69dcdc9cf97571f9811d731ed.png

2、plot

plot(x,y,x,y+1,x,y+2)
%利用plot函数绘制矩阵数据
A=pascal(5);
plot(A)

3d19653fed684e5577fb3b2ffcc46801.png3f5fec12feb1a3ca977891abc81c4d2f.png

 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)

cd8a4089911a78360f4ebc501aa052ac.png

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')

de07f99b51f81583ecded1bad988897a.png

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)

 

0559d0d4f532e70d964b275ceec6acea.png

二、函数绘图

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]);

cb58c46210e84269a742da0e2784d9fa.png

 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])

 bfaaa7edb82a1abc8c761763f25977ea.png

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])

42c6297b8565df1f85d4f8a99503cd21.png

 三、特殊类二维绘图

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('面积图')

0ed80474834ebef82a9b4a13a492bdd3.png

 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份')

ec5661c2066493538f7d7043be58ad91.png

新版建议用:

A=randn(100000,1);
histogram(A)
polarhistogram(A)

5b98be4d83af5308b7a63e390fbcadd3.pngcab1f1290adf3ed53ef050bad6bccfcf.png

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('缺角饼图');

965095722948763af64e50c834a4d1b4.png

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('阶梯图')

00f90b8579f891c865ef0c09c58a6974.png

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('填充等值线图')

 3d4b9b1bafb2cb90734aabe04675090f.png

6、 二维图设置

(1)曲线格式设置

                                                表:绘图函数控制字符

d5d186113cfeec6ddfaaeaeda6465237.jpeg

例:分别用不同的时标、色彩、线型。

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')

b4003d19d8dffd0d56787665d942d32a.png

                                                           曲线常见属性

7ba8e9664c10098c7f44f349cdbd4ce6.jpeg

例:设置曲线的细节属性

x=-pi:pi/10:pi;
y=tan(sin(x))-sin(tan(x));
plot(x,y,'--rs','LineWidth',2,'MarkerFaceColor','g','MarkerSize',10)

920540c296d3e094a67b65be968c6672.png

(2)图形区域控制

      1)坐标轴调整

        

x=0:pi/100:pi/2;
y=tan(x);
plot(x,y,'ko')
grid on
axis([0,pi/2,0,5])

83fb11ec351dc72d2ac06fcfdc77c8fd.png

 

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

62c1e9c4b6cc929ed94ce8778fdddb17.png

3)图形叠加

6ed0147e417744bbd342f49dfcd1faed.jpeg

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

7eb471a3ff9382d485c5b611660519d4.png

 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;

18cf89c265fd1ecf0da4bc5d1b7bff8b.png

 (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}');%特殊符号想加入标题可以用{};

5883f804bc4cbec52cd830b3f07c8e86.png

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

954be015f1a7ce0b81b7e8d26a8e2bdd.png

8a1b2742917c2b24039702f58535eb33.png

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')

c8ed1d62473c9b92e1df42a2f230c941.png

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');

0f822e6c491831ba50f69438391a27da.png

(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])

237ed4b714ea50366045fb5c7dee7a6c.png

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])

e31b0fac40f082a8686ac06bf0a577bd.png

(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);

c66e2b08ad57414a55ae635803f05360.png

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')

c28602a8b26d30a8f5a0f89c3a9ad153.png

设置光照模式

利用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')

812d61b9773ed44ce00dd893d6ac4175.png

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')

7c2bb6a1ceed80b7067007a24b970479.png

 

matlab涉及的图形基本都在这了,这就当属于自己的matlab图函数小库,可以查可以复习。

 

end

 

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值