一、二维曲线图
反应两个变量的因果关系
clear; %清除工作空间的所有变量clc; %清除命令窗口的内容,对工作环境中的全部变量无任何影响close all; %关闭所有的Figure窗口x = linspace(1,200,100); %均匀生成数字1-200,共计100个y1 = log(x) + 1; %生成函数y = log(x) + 1y2 = log(x) + 2; %生成函数y = log(x) + 2figure; %创建一个新的窗口plot(x,y1); %作图y = log(x) + 1hold on %多图共存在一个窗口上plot(x,y2,'LineWidth',2); %作图y=log(x)+2,LineWidth指线型的宽度尺寸2hold off %关闭多图共存在一个窗口上legend('y1','y2'); %生成图例y1和y2
二、二维散点图
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]); %MarkerFaceColor设置内部填充颜色 MarkerEdgeColor设置外部边框填充颜色hold off
三、二维渐变色图
用不同的颜色、数据点大小表征不同数值,
x = linspace(0,3*pi,200);y = cos(x) + rand(1,200); %随机生成1*200,位于[0,1]的数字sz = 25; %尺寸为25c = linspace(1,10,length(x));scatter(x,y,sz,c,'filled')
四、二维条形图
A = [60.689;87.714;143.1;267.9515];C = [127.5;160.4;231.9;400.2];B = C - A;D = [A,B,C];bar1 = bar([2:5:17],A,'BarWidth',0.2,'FaceColor','k'); %画A的数据hold on;bar2 = bar([3:5:18],B,"BarWidth",0.2,'FaceColor',[0.5 0.5 0.5]);%画B的数据hold on;bar3 = bar([4:5:19],C,'BarWidth',0.2,'FaceColor','w'); %画C的数据ylabel('耗时/s'); %为Y轴增加标签 xlabel('GMM阶数'); %为Z轴增加标签legend('训练耗时','测试耗时','总耗时'); %在坐标区域上添加图例labelID = {'8阶','16阶','32阶','64阶'}; %定义刻度标签名称set(gca,'XTick',3:5:20); %XTick为设置X轴的刻度set(gca,'XTickLabel',labelID); %XTickLabel为设置X轴的刻度标签
五、二维填充图
x = 0.4:0.1:2*pi; %定于xy1 = sin(2*x); %定义y1y2 = sin(x); %定义y2%确定y1和y2的上下边界maxY = max([y1;y2]); minY = min([y1;y2]);%确定填充多边形,按照顺时针方向来确定点%fliplr实现左右翻转xFill = [x,fliplr(x)];yFill = [maxY,fliplr(minY)];figurefill(xFill,yFill,[0.21,0.21,0.67]); %填充hold on%绘制轮廓线plot(x,y1,'k','LineWidth',2) %画y1关于x的曲线plot(x,y2,'k','LineWidth',2) %画y2关于x的曲线hold off
六、多Y轴图
figure;load('accidents.mat','hwydata' ) %加载名为 "accidents.mat" 的文件,并将其中名为 "hwydata" 的变量加载到 MATLAB 的工作空间中 ind = 1:51; drivers = hwydata(:,5); %从hwydata中提取第5列的数据,并将其存储在名为drivers的变量中yyaxis left %在图形中创建一个新的Y轴,并将其放置在左侧scatter(ind,drivers,'LineWidth',2); %绘制散点图,其中ind是X轴上的数据,drivers是Y轴上的数据。'LineWidth',2参数用于设置散点图中数据点的线宽为2个单位。title('Highway Data'); %标题xlabel('States'); %X标签ylabel('Licensed Drivers(thousands)'); %Y轴标签pop = hwydata(:,7); %从名为hwydata的数据集中提取第7列的数据,并将其赋值给变量popyyaxis right %将下一个绘图操作的Y轴切换到右侧 scatter(ind,pop,'LineWidth',2); %绘制一个散点图ylabel('Vehicle Miles Traveled(millions)'); %
七、二维场图
[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));%需要获取所有流线的属性figure;quiver(x,y,u,v); %该函数使用箭头来直观的显示矢量场,小箭头来表示以该点为起点的向量(u,v)streamline(x,y,u,v,startx,starty); %绘制流线图,其中(x,y)是网格的坐标,(u,v)是在每个点上的水平和垂直速度分量,(startx,starty)是指定流线的起始点。
八、三维曲线图
figure;t = 0:pi/20:10*pi; xt = sin(t); yt = cos(t); plot3(xt,yt,t,'-o','Color','b','MarkerSize',10); %绘制三维图形
九、三维散点图
figure;[X,Y,Z] = sphere(16);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);C = repmat([1,2,3],numel(X),1);s = S(:);c = C(:);h = scatter3(x,y,z,s,c);h.MarkerFaceColor = [0 0.5 0.5];
x = linspace(1,20,100);y1 = log(x) + 1;y2 = log(x) + 2;y3 = y1 +rand(1,100) - 0.5;figure;scatter3(x,y2,y3,x,x,'filled');
十、三维伪彩图
[x,y,z] = peaks(30);figure;plot1 = subplot(1,2,1);surf(x,y,z);%获取第一幅图的colormap,默认为parulaplot2 = subplot(1,2,2);surf(x,y,z);%下面设置的是第二幅图的颜色colormap(hot);%设置第一幅图颜色显示为parula%一个坐标轴figure;h1 = surf(x,y,z);hold on;h2 = surf(x,y,z+5);hold off;colormap(hot);
十一、裁剪伪彩图
figure;n = 300;[x,y,z] = peaks(n);subplot(2,2,[1,3]);surf(x,y,z);shading interp;view(0,90);for i = 1:n for j = i: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 endendsubplot(2,2,2)surf(x,y,z);shading interpview(0,90)subplot(2,2,4)surf(x,y,z);shading interp
十二、等高线图
figure;[X,Y,Z] = peaks;subplot(2,2,1);contour(X,Y,Z,20,'LineWidth',2);subplot(2,2,2);contour(X,Y,Z,20,'--',"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);
十三、三维等高线图
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);
十四、等高线填充图
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]);
十五、三维矢量场图
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 interphold offview(0,90);
十六、伪彩图+投影图
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%平滑图像
十七、热图
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;endfor i = 31:50 z(30 + randi(20,1,1):end,i) = nan;endz(20:25,40:45) = nan;figure;% ax = surf(z);ax = pcolor(z);view(0,90);ax.EdgeColor = [1 1 1];
十八、分子模型图
clear;clc;%球面的坐标信息,为了看起来平滑一点,给到100[x,y,z] = sphere(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,关闭光源
十九、分形图