MATLAB 中的图形绘制

发布于:2025-05-01 ⋅ 阅读:(22) ⋅ 点赞:(0)

一、线图

plot 函数用来创建x和y值的简单线图。

x = 0 : 0.05 : 30;   %从0到30,每隔0.05取一次值
y = sin(x);
plot(x,y,'LineWidth',2)     %若(x,y,'LineWidth',2)可变粗
xlabel("横轴标题")
ylabel("纵轴标题")
grid on     %显示网格
axis([0 20 -1.5 1.5])  %设置横纵坐标的范围,前两个是x,后两个是y

多组函数显示在同一张图

y1 = sin(x);
y2 = cos(x);
plot(x,y1,x,y2)
axis([0 20 -2 2])

二、条形图

bar 函数创建垂直条形图

barh 函数用来创建水平条形图

t = -3:0.5:3;   %横坐标
p = exp(-t.*t);  %指数函数,e的-t平方
bar(t,p)
barh(t,p)

三、极坐标图

polarplot 函数用来绘制极坐标图

theta = 0 : 0.01 : 2 * pi;  %角度
% abs 求绝对值或复数的模
radi = abs(sin(7*theta).*cos(10*theta));
polarplot(theta,radi)  %括号内是弧度和半径

四、散点图

scatter 函数用来绘制x和y值的散点图

Height = randn(1000,1);  %1000行一列,randn随机函数调用,横坐标
Weight = randn(1000,1);  %纵坐标
scatter(Height,Weight)
xlabel('Height')
ylabel('Weight')

 五、三维曲面图

surf 函数可用来做三维曲面图。一般是展示函数z = z(x,y)的图像。

首先需要用meshgrid创建好空间上(x,y)点

[X,Y] = meshgrid(-2:0.2:2);
%Z = X.^2 + Y.^2
Z = X.*exp(-X.^2-Y.^2); %.*就是相乘
surf(X,Y,Z);
colormap hsv    %图形的固定颜色
%colormap 设置颜色,可跟winter、summer等
%colorbar   %颜色条柱代表的区间

六、内嵌子图

使用subplot函数可以在同一窗口的不同子区域显示多个绘图

theta = 0:0.01:2*pi;
radi = abs(sin(2*theta).*cos(2*theta));
Height = randn(1000,1);
Weight = randn(1000,1);

subplot(2,2,1);surf(X.^2);title('1st');  %两行两列,第一个图是三维曲面图
subplot(2,2,2);surf(Y.^3);title('2nd');
subplot(2,2,3);polarplot(theta,radi);title('3rd');
subplot(2,2,4);scatter(Height,Weight);title('4th');


网站公告

今日签到

点亮在社区的每一天
去签到