数学建模之matlab作图汇总(共19种)

数学建模之matlab作图汇总(共19种)

文章目录

1.二维曲线2.二维散点图3.二维渐变图4.条形图5.填充图6.多y轴图7.二维场图8.三维曲线图9.三维散点图10.三维伪彩图11.裁剪伪彩图12.等高线图13.三维等高线图14.等高线填充图15.三维矢量场图16.伪彩图+投影图17.热图18.分子模型图19.分形图(装逼用)

1.二维曲线

clear;clc;close all;

x=linspace(1,200,100);%均匀生成数字1~200,共计100个

y1=log(x)+1;%生成函数y=log(x)+1

y2=log(x)+2;%生成函数y=log(x)+2

figure;%表示开始作图,开启一个作图窗口

plot(x,y1);%作图y=log(x)+1

hold on;%多图共存在一个窗口上

plot(x,y2,'LineWidth',2);%作图y=log(x)+2,LineWidth指线型的宽度,粗细尺寸2

hold off;%关闭多图共存在一个窗口上

legend('y1','y2');%生成图例y1和y2

matlab中

plot(x,y,'color',se,'LineWidth',1.5)

%'color',se——color是颜色,se是颜色变量,如se='r'

%r(红)、g(绿)、b(蓝)、c(蓝绿)、m(紫红)、y(黄)、k(黑)、w(白)

%LineWidth指线型的宽度,粗细尺寸1.5

2.二维散点图

figure;

y3=y1+rand(1,100)-0.5;

plot(x,y1,'LineWidth',2,'color',[0.21,0.21,0.67]);

hold on;

%设置数据点的形状、数据点的填充颜色、数据点的轮廓颜色

plot(x,y3,'o','LineWidth',2,'color',[0.46,0.63,0.90],'MarkerFaceColor',[0.35,0.90,0.89],'MarkerEdgeColor',[0.18,0.62,0.17]);

hold off;

matlab中

%MarkerFaceColor用于设置内部填充颜色

%MarkerEdgeColor用于设置外部边框颜色

3.二维渐变图

x=linspace(0,3*pi,200);%生成200个0~3pi的数据

y=cos(x)+rand(1,200);%随机生成1*200,位于[0,1]的数字

sz=25;%尺寸为25

c=linspace(1,10,length(x));

scatter(x,y,sz,c,'filled');

matlab中

%scatter函数用法

%scatter(x,y)在向量 x 和 y 指定的位置创建一个包含圆形标记的散点图。

%要绘制一组坐标,请将 x 和 y 指定为等长向量。

%要在同一组坐标区上绘制多组坐标,请将 x 或 y 中的至少一个指定为矩阵。

%scatter (x,y,sz) 指定圆大小。要对所有圆使用相同的大小,请将 sz 指定为标量。要绘制不同大小的每个圆,请将 sz 指定为向量或矩阵。

%scatter (x,y,sz,c) 指定圆颜色。您可以为所有圆指定一种颜色,也可以更改颜色。例如,您可以通过将c指定为'red'来绘制所有红色圆。

%scatter(___,'filled')填充圆。可以将'filled' 选项与前面语法中的任何输入参数组合一起使用。

scatter函数用法

%创建散点图

x=linspace(0,3*pi,200);

y=cos(x)+rand(1,200);

scatter(x,y)

%改变圆圈大小

%使用大小不同的圆圈创建一个散点图。

%以平方磅为单位指定大小

x=linspace(0,3*pi,200);

y=cos(x)+rand(1,200);

sz=linspace(1,100,200);

scatter(x,y,sz)

%改变圆圈颜色

%创建一个散点图并改变圆圈的颜色

x=linspace(0,3*pi,200);

y=cos(x)+rand(1,200);

c=linspace(1,10,length(x));

scatter(x,y,[],c)

%填充标记

%创建一个散点图并填充标记。scatter使用标记边的颜色填充每个标记

x=linspace(0,3*pi,200);%生成200个0~3pi的数据

y=cos(x)+rand(1,200);%随机生成1*200,位于[0,1]的数字

sz=25;%尺寸为25

c=linspace(1,10,length(x));

scatter(x,y,sz,c,'filled');

4.条形图

A=[60.689;87.714;143.1;267.9515];%4×1的数据

C=[127.5;160.4;231.9;400.2];%4×1的数据

B=C-A;%4×1的数据%4×1的数据

D=[A,B,C];%4×3的数据

bar1=bar(2:5:17,A,'BarWidth',0.2,'FaceColor','k');

hold on;

bar2=bar(3:5:18,B,'BarWidth',0.2,'FaceColor',[0.5 0.5 0.5]);

hold on;

bar3=bar(4:5:19,C,'BarWidth',0.2,'FaceColor','w');

ylabel('耗时/s')

xlabel('GMM阶数')

legend('训练耗时','测试耗时','总耗时');

labelID ={'8阶','16阶','32阶','64阶'};

set(gca,'xTick' ,3:5:20);

set(gca,'xTickLabel',labelID)

5.填充图

x=0.4:0.1:2*pi;

y1=sin(2*x);

y2=sin(x);

%确定y1和y2的上下边界

maxY=max([y1;y2]);

minY=min([y1;y2]);

%确定填充多边形,按照顺时针方向来确定点

%fliplr 实现左右翻转

xFill=[x,fliplr(x)];

yFill=[maxY,fliplr(minY)];

figure

fill(xFill,yFill,[0.21,0.21,0.67]);

hold on

% 绘制轮廓线

plot(x,y1,'k','LineWidth',2)

plot(x,y2,'k','LineWidth',2)

hold off

6.多y轴图

figure;

load('accidents.mat','hwydata')

ind = 1:51;

drivers =hwydata(:,5);%所有行第5列数据

yyaxis left%生成左坐标轴

scatter(ind,drivers,'LineWidth',2);%作散点图

title('Highway Data');

xlabel('States');

ylabel('Licensed Drivers (thousands)');

pop =hwydata(:,7);

yyaxis right

scatter(ind,pop,'LineWidth',2);

ylabel('Vehicle Miles Traveled (millions)');

7.二维场图

%直接把 streamline 函数的帮助文档 demo 拷贝过来

[x,y] = meshgrid(0:0.1:1,0:0.1:1);%生成格网采样点

u=x;%矢量

v=-y;%矢量

startx =0.1:0.1:0.9;

starty =ones(size(startx));%生成和startx数目一致的全是1的一组数据

%需要获取所有流线的属性

figure;

quiver(x,y,u,v);%该函数使用箭头来直观的显示矢量场小箭头来表示以该点为起点的向量(u,v),在x,y的坐标轴中

streamline(x,y,u,v, startx, starty);%绘制流线图;quiver是定义矢量,streamline是绘制;

8.三维曲线图

figure;

t=0:pi/20:10*pi;

xt=sin(t);

yt=cos(t);

plot3(xt,yt,t,'-o','Color','b','MarkerSize',10);%作三维图,'-o'即作图为圈,且将其连接起来;'MarkerSize'代表圈的大小

figure;

x=-20:10:20;

y=0:100;

% 随便生成的 5 组数据,也就是目标图上的 5 条曲线数据

z =zeros(5,101);

z(1,1:10:end) = linspace(1,10,11);

z(2,1:10:end) = linspace(1,20,11);

z(3,1:10:end) = linspace(1,5,11);

z(4,5:10:end) = linspace(1,10,10);

z(5,80:2:end) = linspace(1, 5,11);

for i = 1:5

%x方向每条曲线都是一个值,重复的长度这么多次

xx =x(i)*ones(1,101);

%z方向的值,每次取一条

zz = z(i,:);

% plot3 在 xyz 空间绘制曲线,保证z度一致即可

plot3(xx,y,zz,'LineWidth', 2);

hold on

end

hold off

legend('linel','line2', 'line3', 'line4', 'line5');

9.三维散点图

figure;

[X,Y,Z] = sphere(16);%绘制一个16×16的球面,把点的坐标赋值给[X,Y,Z]

x =[0.5*X(:); 0.75*X(:); X(:)];

y =[0.5*Y(:); 0.75*Y(:); Y(:)];

z =[0.5*Z(:); 0.75*Z(:); Z(:)];

S = repmat([70,50,20],numel(X),1);%repmat复制,numel(X)计算X的元素,把[70,50,20]复制成numel(X)行,1列的一组数

C = repmat([1, 2,3], numel(X), 1);

s = S(:);

c = C(:);%size(c)=C行*列

h = scatter3(x,y,z,s,c);%生成三维散点图,s代表尺寸,c代表颜色

h.MarkerFaceColor =[0 0.5 0.5];%填充的颜色

x = linspace(1,200,100);

y1 = log(x) + 1;

y2 = log(x) + 2;

y3 = y1 + rand(1,100) - 0.5;

figure;

scatter3(x, y2, y3, x, x, 'filled');%'filled',圈是实体的

10.三维伪彩图

[x,y,z] = peaks(30);%peaks函数

figure;

plot1 = subplot(1,2,1);%subplot生成1行2列的图形窗口,第一个图

surf(x,y,z);

%获取第一幅图的 colormap,默认为 parula

plot2 = subplot(1,2,2);%subplot生成1行2列的图形窗口,第二个图

surf(x,y,z);

%下面设置的是第二幅图的颜色

colormap(hot);

%或者设置图颜色显示为 parula

%colormap(parula);

%一个坐标轴

figure;

h1 = surf(x, y, z);

hold on

h2 = surf(x, y, z + 5);

hold off

colormap(hot);

11.裁剪伪彩图

figure;

n=300;

[x,y,z] = peaks(n);

subplot(2,2,[1,3])%将1,3图合并

surf(x,y,z);

shading interp%渲染

view(0,90)

for i= 1:n

for j = 1:n

if x(i, j)^2 +2*y(i, j)^2 > 6 &&2*x(i,j)^2 + y(i, j)^2 <6

z(i,j) = NaN;%设置为空值

end

end

end

subplot(2,2,2)

surf(x,y,z);

shading interp

view(0,90)

subplot(2,2,4)

surf(x,y,z);

shading interp

12.等高线图

figure;

[X,Y,Z] = peaks;

subplot(2,2,1);

contour(X,Y,Z,20,'LineWidth',2);%生成等高线

subplot(2,2,2);

contour(X,Y,Z,'--','LineWidth',2);

subplot(2,2,3);

v=[1,1];

contour(X,Y,Z,v,'LineWidth', 2);

x=-2:0.2:2;

y=-2:0.2:3;

[X,Y] = meshgrid(x,y);

Z=X.*exp(-X.^2-Y.^2);

subplot(2,2,4);

contour(X,Y,Z,'ShowText','on','LineWidth',2);

13.三维等高线图

figure('Position',[0,0,900,400]);

subplot(1,3,1);

[X,Y,Z] = sphere(50);

contour3(X,Y,Z,'LineWidth', 2);

[X,Y] = meshgrid(-2:0.25:2);

Z=X.*exp(-X.^2-Y.^2);

subplot(1,3,2);

contour3(X,Y,Z,[-0.2 -0.1 0.1 0.2],'ShowText','on','LineWidth',2)

[X,Y,Z]= peaks;

subplot(1, 3,3);

contour3(X,Y,Z,[2 2],'LineWidth', 2);

14.等高线填充图

figure;

subplot(2,2,1);

[X,Y,Z] = peaks(50);

contourf(X,Y,Z);

subplot(2,2,2);

contourf(X,Y,Z,'--');

% 限定范围

subplot(2,2,3);

contourf(X,Y,Z,[2 3],'ShowText','on');

subplot(2,2,4);

contourf(X,Y,Z,[2 2]);

15.三维矢量场图

figure;

[X,Y,Z] = peaks(30);

% 矢量场,曲面法线

[U,V,W] = surfnorm(X, Y, Z);%生成法线

% 箭头长度、颜色

quiver3(X, Y, Z, U, V, W, 0.5,'r');

hold on

surf(X,Y,Z);

xlim([-3,3]);

ylim([-3,3.2]);

shading interp%渲染

hold off

view(0,90);%查看视角

16.伪彩图+投影图

clear;clc;close all;

x = linspace(-3,3,30);

y = linspace(-4,4,40);

[X,Y] = meshgrid(x,y);

Z = peaks(X,Y);

Z(5:10,15:20) = 0;

z1 = max(Z);

z2 = max(Z,[], 2);

figure;

subplot(3,3,[1,2]);

plot(x,z1,'LineWidth', 2);

subplot(3,3,[6, 9]);

plot(z2, y,'LineWidth', 2);

subplot(3,3,[4,5,7,8]);

surf(x,y,Z);

xlim([-3,3]);

ylim([-4,4]);

view(0,90);

shading interp%平滑图像

17.热图

clear;clc;

z = rand(50);

z(z>=0.0&z<0.6) = 0.5;

z(z>=0.6&z<0.8) = 0.7;

z(z>=0.8&z<=1 ) = 0.9;

for i = 1:30

z(randi(50,1,1) : end,i) = nan;%设置空值

end

for i = 31:50

z(30 + randi(20,1,1) : end,i) = nan;

end

z(20:25,40:45) = nan;

figure;

% ax = surf(z);

ax = pcolor(z);

view(0,90);

ax.EdgeColor = [1 1 1];

18.分子模型图

clear;clc;

% 球面的坐标信息,为了看起来平滑一点,给到 100

[x,y,z]=sphere(100);%100×100的球形

% C大小

C=10;

% H大小

H=5;

figure;

% 大球

surf(C*x,C*y,C*z,'FaceColor','red','EdgeColor','none');%大球,各点坐标扩大

hold on

% 四个小球,都偏离一点位置,准确的位置需要计算,这里演示一个大概位置。

surf(H*x,H*y,H*z+10,'FaceColor','blue','EdgeColor','none');

surf(H*x+10,H*y,H*z-3,'FaceColor','blue','EdgeColor','none');

surf(H*x-4,H*y-10,H*z-3,'FaceColor','blue','EdgeColor','none');

surf(H*x-4,H*y+10,H*z-3,'FaceColor','blue','EdgeColor','none');

% 坐标轴设置

axis equal off

% 光源,看起来更有立体感

light

%lighting none,关闭光照

19.分形图(装逼用)

clear;

%不同的参数有不同的图形

a=1.7;b=1.7;c=0.6;d=1.2;%a=1.5;b=-1.8;c=1.6;d=0.9;

x=0;y=0;

n=100000;%迭代10w次

kx = zeros(1,n);

ky = zeros(1,n);

%迭代循环

for i=1:n

tempx=sin(a*y)+c*cos(a*x);

tempy=sin(b*x)+d*cos(b*y);

%存入数组

kx(i)=tempx;

ky(i)=tempy;

%重新赋值x,y

x=tempx;

y=tempy;

end

scatter(kx,ky,0.1,'green');

参考资料: 【零基础教程】老哥:数学建模算法、编程、写作和获奖指南全流程培训

相关推荐

江门电信+移动+联通宽带价格资费大全,低价套餐横向对比!看看超值套餐是哪个!
马拉多纳1986年世界杯上帝之手进球的争议与历史意义解析
西海龙王(西海龙王大太子)
beat365在线登录app

西海龙王(西海龙王大太子)

📅 10-28 👁️ 4345

友情链接