百科问答小站 logo
百科问答小站 font logo



你见过最美的 MATLAB 绘图是什么? 第1页

  

user avatar   fang-zong-29-83 网友的相关建议: 
      

我不是这个代码的原作者,我只是用这个代码画过,分享给大家,原作者和原文已在最后标明。

注:侵删致歉

这题我会啊,我记得当时给女朋友画过两次 ,我把代码粘出来。

       function drawrose2 grid on [x,t]=meshgrid((0:24)./24,(0:0.5:575)./575.*20.*pi-4*pi); p=(pi/2)*exp(-t./(8*pi)); change=sin(20*t)/150; u=1-(1-mod(3.3*t,2*pi)./pi).^4./2+change; y=2*(x.^2-x).^2.*sin(p);  r=u.*(x.*sin(p)+y.*cos(p)).*1.5; h=u.*(x.*cos(p)-y.*sin(p));  map=[0.9176    0.7490    0.3765     0.9188    0.7513    0.3879     0.9200    0.7537    0.3992     0.9211    0.7560    0.4106     0.9223    0.7584    0.4220     0.9235    0.7608    0.4334     0.9247    0.7631    0.4447     0.9259    0.7654    0.4561     0.9270    0.7678    0.4675     0.9282    0.7702    0.4788     0.9294    0.7725    0.4902     0.9298    0.7698    0.5008     0.9302    0.7670    0.5114     0.9306    0.7643    0.5220     0.9310    0.7615    0.5326     0.9314    0.7588    0.5432     0.9317    0.7561    0.5537     0.9321    0.7533    0.5643     0.9325    0.7506    0.5749     0.9329    0.7478    0.5855     0.9333    0.7451    0.5961     0.9290    0.7404    0.5894     0.9247    0.7357    0.5828     0.9204    0.7310    0.5761     0.9161    0.7263    0.5694     0.9118    0.7215    0.5627     0.9074    0.7168    0.5561     0.9031    0.7121    0.5494     0.8988    0.7074    0.5427     0.8945    0.7027    0.5361     0.8902    0.6980    0.5294     0.8890    0.6976    0.5388     0.8878    0.6972    0.5482     0.8867    0.6968    0.5576     0.8855    0.6964    0.5670     0.8843    0.6961    0.5765     0.8831    0.6957    0.5859     0.8819    0.6953    0.5953     0.8808    0.6949    0.6047     0.8796    0.6945    0.6141     0.8784    0.6941    0.6235     0.8827    0.6988    0.6364     0.8870    0.7035    0.6494     0.8914    0.7082    0.6623     0.8957    0.7129    0.6753     0.9000    0.7177    0.6882     0.9043    0.7224    0.7011     0.9086    0.7271    0.7141     0.9130    0.7318    0.7270     0.9173    0.7365    0.7400     0.9216    0.7412    0.7529     0.9153    0.7275    0.7545     0.9090    0.7137    0.7560     0.9028    0.7000    0.7576     0.8965    0.6863    0.7592     0.8902    0.6725    0.7608     0.8839    0.6588    0.7623     0.8776    0.6451    0.7639     0.8714    0.6314    0.7655     0.8651    0.6176    0.7670     0.8588    0.6039    0.7686     0.8580    0.5906    0.7557     0.8572    0.5772    0.7427     0.8565    0.5639    0.7298     0.8557    0.5506    0.7168     0.8549    0.5373    0.7039     0.8541    0.5239    0.6910     0.8533    0.5106    0.6780     0.8526    0.4973    0.6651     0.8518    0.4839    0.6521     0.8510    0.4706    0.6392     0.8420    0.4447    0.6110     0.8330    0.4188    0.5827     0.8239    0.3930    0.5545     0.8149    0.3671    0.5263     0.8059    0.3412    0.4980     0.7969    0.3153    0.4698     0.7879    0.2894    0.4416     0.7788    0.2636    0.4134     0.7698    0.2377    0.3851     0.7608    0.2118    0.3569]; set(gca,'CameraPosition',[2 2 2]) hold on surface(r.*cos(t),r.*sin(t),h,'EdgeAlpha',0.1,...     'EdgeColor',[0.5 0.5 0.5],'FaceColor','interp') colormap(map)  end     
       function drawrose grid on [x,t]=meshgrid((0:24)./24,(0:0.5:575)./575.*20.*pi+4*pi); p=(pi/2)*exp(-t./(8*pi)); change=sin(15*t)/150; u=1-(1-mod(3.6*t,2*pi)./pi).^4./2+change; y=2*(x.^2-x).^2.*sin(p);  r=u.*(x.*sin(p)+y.*cos(p)); h=u.*(x.*cos(p)-y.*sin(p));  map=[0.9176    0.9412    1.0000     0.9094    0.9341    0.9992     0.9011    0.9271    0.9984     0.8929    0.9200    0.9977     0.8847    0.9130    0.9969     0.8764    0.9059    0.9961     0.8682    0.8988    0.9953     0.8600    0.8918    0.9945     0.8518    0.8847    0.9938     0.8435    0.8777    0.9930     0.8353    0.8706    0.9922     0.8337    0.8698    0.9910     0.8322    0.8690    0.9898     0.8306    0.8682    0.9887     0.8290    0.8674    0.9875     0.8275    0.8667    0.9863     0.8259    0.8659    0.9851     0.8243    0.8651    0.9839     0.8227    0.8643    0.9828     0.8212    0.8635    0.9816     0.8196    0.8627    0.9804     0.8078    0.8521    0.9765     0.7961    0.8415    0.9726     0.7843    0.8310    0.9686     0.7726    0.8204    0.9647     0.7608    0.8098    0.9608     0.7490    0.7992    0.9569     0.7373    0.7886    0.9530     0.7255    0.7781    0.9490     0.7138    0.7675    0.9451     0.7020    0.7569    0.9412     0.6836    0.7400    0.9396     0.6651    0.7232    0.9381     0.6467    0.7063    0.9365     0.6282    0.6894    0.9349     0.6098    0.6725    0.9334     0.5914    0.6557    0.9318     0.5729    0.6388    0.9302     0.5545    0.6219    0.9286     0.5360    0.6051    0.9271     0.5176    0.5882    0.9255     0.5027    0.5776    0.9271     0.4878    0.5670    0.9286     0.4729    0.5565    0.9302     0.4580    0.5459    0.9318     0.4431    0.5353    0.9334     0.4282    0.5247    0.9349     0.4133    0.5141    0.9365     0.3984    0.5036    0.9381     0.3835    0.4930    0.9396     0.3686    0.4824    0.9412     0.3623    0.4742    0.9404     0.3561    0.4659    0.9396     0.3498    0.4577    0.9388     0.3435    0.4494    0.9380     0.3372    0.4412    0.9373     0.3310    0.4330    0.9365     0.3247    0.4247    0.9357     0.3184    0.4165    0.9349     0.3122    0.4082    0.9341     0.3059    0.4000    0.9333     0.2981    0.3918    0.9235     0.2902    0.3835    0.9137     0.2824    0.3753    0.9039     0.2745    0.3670    0.8941     0.2667    0.3588    0.8843     0.2589    0.3506    0.8745     0.2510    0.3423    0.8647     0.2432    0.3341    0.8549     0.2353    0.3258    0.8451     0.2275    0.3176    0.8353     0.2169    0.3086    0.8165     0.2063    0.2996    0.7977     0.1957    0.2906    0.7788     0.1851    0.2816    0.7600     0.1746    0.2726    0.7412     0.1640    0.2635    0.7224     0.1534    0.2545    0.7036     0.1428    0.2455    0.6847     0.1322    0.2365    0.6659     0.1216    0.2275    0.6471]; set(gca,'CameraPosition',[2 2 2]) hold on surface(r.*cos(t),r.*sin(t),h,'EdgeAlpha',0.1,...     'EdgeColor',[0 0 0],'FaceColor','interp') colormap(map)  end     

快拿去画给你的心上人吧!!!

修改一下回答,我的意思是说我用这个代码画过,

原作者姓名:slandarer

原出处:CSDN

原文链接:


更新一下:

又看到一个动态爱心:

       % matlab动态心形曲线(保存GIF格式) clc;clear all; a=10; x=-2:0.01:2;  figure(1); set(gcf,'position',[0,0,800,600],'color','w'); for i=1:100     str_title = strcat('color{red}a=',num2str(a));     y = abs(x).^(2/3)+(0.9*sqrt((3.3-x.^2))).*sin(a*pi*x);     a=i/10;     figure(i);          set(gcf,'position',[0,0,800,600],'color','w');     p1 = plot(x,y,'r','LineWidth',3);     hold on;     title('color{red}f(x)=x^2^/^3+e/3*(π-x^2)^1^/^2*sin(a*π*x)','fontsize',15);     text(-0.2,2.3,str_title,'FontName','Times New Roman','FontSize',20);     xlim([-2 2]);     ylim([-1.5 2.5]);     frame = getframe(gcf);     im = frame2im(frame);     [I,map] = rgb2ind(im,256);     if i==1         imwrite(I,map,'心形图.gif','gif','Loopcount',Inf,'DelayTime',0.05);     else         imwrite(I,map,'心形图.gif','gif','WriteMode','append','DelayTime',0.05);     end     close(figure(i)); end     

原作者姓名:左手@明天

原出处:CSDN

原文链接:


user avatar   1105936347 网友的相关建议: 
      

Matlab绘图案例,整理如下,多图长文,建议收藏。

Matlab案例代码解析

2. 典型绘图案例

2. 典型绘图案例
2.1 优雅的绘图颜色
2.1.1 颜色模板
2.1.2 定制colormap
2.2 二维绘图
2.2.1 曲线图、散点图
2.2.2 带有误差线的柱状图
2.2.3 简单曲线之间填充
2.2.4 复杂函数曲线之间填充
2.2.5 渐变色曲线、渐变色填充
2.2.6 带箭头曲线
2.2.7 多个Y轴
2.2.8 旋转多边形
2.2.9 带箭头流线
2.2.10 极坐标绘图技巧
2.2.11 条形分布图
2.3 三维绘图
2.3.1 给球面贴背景图
2.3.2 三维矢量场图
2.3.3 单独设置子图的colormap
2.3.4 曲面裁剪
2.3.5 三维曲线、散点图
2.3.6 等高线图
2.3.7 绘制伪彩图和投影图
2.4 四维绘图
2.4.1 slice切片显示
2.5 函数方程可视化
2.5.1 一元方程
2.5.2 二元方程
2.5.3 三元方程
2.6 特殊绘图
2.6.1 动态趋势线可视化
2.6.2 Newton-Raphson、Julia分形
2.6.3 分形树叶
2.6.4 在指定区域内随机生成不相交不相切的圆
2.6.5 创建gif动图
2.6.6 梯度下降法的下降方向
2.6.7 目标沿指定轨迹运动
2.6.8 看起来像拼接两个Axis
2.6.9 设置特殊的图例
2.6.10 甲烷模型
2.6.11 在随机圆里产生随机点
2.6.12 斜线填充的曲线
2.6.13 球板模型
2.6.14 BP神经网络图
2.6.15 由一系列圆构成的圆环
2.6.16 棋盘格

部分存图先睹为快

2.1 优雅的绘图颜色 2.1.1 颜色模板

绘图技巧固然很重要,一个漂亮的颜色模板更能锦上添花,60套绘图颜色模板分享出来,每套模板6种颜色,以下为第一套模板

2.1.2 定制colormap

作为一名合格的科研工作者,绘制三维曲面图、伪彩图,怎么少得了自定义专属 colormap,呈现高端优美的视图效果,选择几种颜色,自动生成colormap,方法在这里

2.2 二维绘图 2.2.1 曲线图、散点图

二维曲线、散点图算是最最常见的一种曲线了,直接反应两个变量的因果关系,散点图常用来比较理论数据和实验数据的趋势关系;

设置线型(点线、虚线、实线、点虚线)、数据点标记(星号、圆、正方形等等)、线颜色、标记点颜色等等,使图形看起来更加优雅美观;

颜色设置格式:缩写字符(‘r’,固定颜色,种类较少);

十六进制字符串('#FD6D5A',种类不限,每种颜色都需要逐个设置);

RGB向量([0.5, 0.5, 0.5],种类不限,可设置颜色模板,使用最方便);

       clear;clc;close all; %%%%% 曲线图 x = 0:0.4:2*pi; y = sin(x); % 在一个 figure 中绘制多个子图,(2, 1, 1) 表示 2 行 1 列第 1 个子图 figure; subplot(2, 1, 1)    % 绘制一条曲线 plot(x, y, '-d', 'Color', 'r', 'MarkerEdgeColor', 'k', 'MarkerFaceColor', 'y',...      'MarkerSize', 10, 'LineWidth', 2); % d:表示倒置的菱形,-d表示用实线连接,r表示曲线红色, % MarkerEdgeColor表示标记点轮廓颜色 %bMarkerFaceColor 表示标记点内部颜色 % MarkerSize 表示标记大小 % 各种线型、符号、颜色代码参考帮助,如下     
       % 第二幅子图  subplot(2, 1, 2) % 绘制多条曲线 x = 1:4:100; y = log(x); for i = 1:6     plot(x, y + i, 'o-', 'LineWidth', 2);     hold on end hold off % 获取到颜色 [all_themes, all_colors] = GetColors(); % 修改默认的颜色模板 set(gca, 'colororder', all_themes{1});     
       %%%%% 散点图 figure; x = linspace(1, 200, 100); y1 = log(x) + 1; y2 = y1 + rand(1, 100) - 0.5; plot(x, y1, 'LineWidth', 2, 'Color', all_colors(1, :)); hold on % 设置数据点的形状、数据点的填充颜色、数据点的轮廓颜色 plot(x, y2, 'o', 'LineWidth', 2, 'Color', all_colors(6, :), 'MarkerFaceColor', all_colors(6, :)); hold off     

2.2.2 带有误差线的柱状图

       clear;clc;close all; % 获取到颜色 [all_themes, all_colors] = GetColors(); % 生成示例数据 m = 5; n = 3; x = 1:m; y = rand(m, n) + 2; % 误差限 neg = rand(m, n); pos = rand(m, n); % 单系列带有误差线的柱状图 figure; bar(x, y(:, 1)); hold on errorbar(x, y(:, 1), neg(:, 1), pos(:, 1), 'LineStyle', 'none', 'Color', 'k', 'LineWidth', 2); hold off % 多系列带有误差线的柱状图 figure; % 绘制柱状图 h = bar(x, y); % 设置每个系列颜色 for i = 1:length(h)     h(1, i).FaceColor = all_colors(i, :); end % 单独设置第二个系列第二个柱子颜色 % 这行代码少不了 h(1, 2).FaceColor = 'flat'; h(1, 2).CData(2,:) = all_colors(6, :);  % 获取误差线 x 值  % 也就是 XEndPoints 的值 xx = zeros(m, n); for i = 1 : n     xx(:, i) = h(1, i).XEndPoints';  end % 绘制误差线 hold on errorbar(xx, y, neg, pos, 'LineStyle', 'none', 'Color', 'k', 'LineWidth', 2); hold off % 绘制图例 legend({'A1', 'A2', 'A3'}); % 设置 x 轴标签 set(gca, 'XTickLabel', {'label1', 'label2', 'label3', 'label4', 'label5'}); % 试试 barweb figure; barweb(y, neg, 1, {'label1', 'label2', 'label3', 'label4', 'label5'});     

完整版

2.2.3 简单曲线之间填充

       clear;clc;close all; % 获取到颜色 [all_themes, all_colors] = GetColors(); % 生成数据 x = linspace(0, 2*pi, 100); y = sin(2*x); figure subplot(1, 2, 1) fill(x, y, all_colors(1, :));  hold on plot([0, 7], [0, 0], 'Color', all_colors(6, :)); hold off subplot(1, 2, 2) patch(x, y, all_colors(1, :));  hold on plot([0, 7], [0, 0], 'Color', all_colors(6, :)); hold off % 换一下数据 x = 0.4:0.1:2*pi; y = sin(2*x); figure subplot(1, 2, 1) fill(x, y, all_colors(1, :)); hold on plot([0, 7], [0, 0], 'k'); hold off subplot(1, 2, 2) patch(x, y, all_colors(1, :));  hold on plot([0, 7], [0, 0], 'k'); hold off 这个结果似乎没有达到预期,查询到 fill 和 patch 函数是做的多边形填充,因此需要做一些修改。 % 强制首尾各添加一个点,保证 y 值为 0 x = [x(1), x, x(end)]; y = [0, y, 0]; figure subplot(1, 2, 1) fill(x, y, all_colors(1, :)); hold on plot([0, 7], [0, 0], 'k'); hold off subplot(1, 2, 2) patch(x, y, all_colors(1, :));  hold on plot([0, 7], [0, 0], 'k'); hold off 函数之间围成的区域填充演示: x = 0.4:0.1:2*pi; y1 = sin(2*x); y2 = sin(x); % 确定 y2 和 y4 的上下边界 maxY = max([y1; y2]); minY = min([y1; y2]); % 确定填充多边形,按照顺时针方向来确定点 % fliplr 实现左右翻转 xFill = [x, fliplr(x)]; yFill = [maxY, fliplr(minY)]; figure subplot(1, 2, 1) fill(xFill, yFill, all_colors(1, :)); hold on plot(x, y1, 'k', 'LineWidth', 2) plot(x, y2, 'k', 'LineWidth', 2) hold off subplot(1, 2, 2) patch(xFill, yFill, all_colors(1, :));  hold on plot(x, y1, 'k', 'LineWidth', 2) plot(x, y2, 'k', 'LineWidth', 2) hold off     

完整版


2.2.4 复杂函数曲线之间填充

       clear;clc;close all; % 获取到颜色 [all_themes, all_colors] = GetColors(); % 常规思路 figure; % 这是三个方程 g1 = @(x,y) 1 - x.^2.*y/20; g2 = @(x, y) 1 - (x+y-5).^2/30 - (x-y-12).^2/120; g3 = @(x, y) 1 - 80./(x.^2 + 8*y + 5); % 把他们的图像画出来 h1 = fimplicit(g1, [0, 10], 'LineWidth', 2, 'Color', all_colors(1, :)); hold on h2 = fimplicit(g2, [0, 10], 'LineWidth', 2, 'Color', all_colors(1, :)); h3 = fimplicit(g3, [0, 10], 'LineWidth', 2, 'Color', all_colors(1, :)); legend('g1', 'g2', 'g3'); % 问题来了,这个 y 值怎么确定 x1 = h1.XData; y1 = h1.YData; x2 = h2.XData; y2 = h2.YData; x3 = h3.XData; y3 = h3.YData; % 发现 x1, x2 是从大到小的,逆序搞一下 x1 = fliplr(x1); x2 = fliplr(x2); y1 = fliplr(y1); y2 = fliplr(y2); % 接下来,确定相交区域,主要看这三个关键点:g1-g2, g1-g3, g2-g3 % 数据长度不一样,难搞了,借助【数据提示】功能吧 pt1 = [3.11069, 2.06667]; pt2 = [8.53623, 0.266667]; pt3 = [7.7847, 1.8]; % 确定三个范围 idx1 = find(x1 >= pt1(1) & x1 < pt2(1)); idx2 = find(x2 >= pt1(1) & x2 < pt3(1)); idx3 = find(x3 >= pt3(1) & x3 < pt2(1)); x1 = x1(idx1); x2 = x2(idx2); x3 = x3(idx3); y1 = y1(idx1); y2 = y2(idx2); y3 = y3(idx3); % 一定要是一个闭环 xFill = [x1, fliplr(x3), fliplr(x2)]; yFill = [y1, fliplr(y3), fliplr(y2)]; fill(xFill, yFill, all_colors(6, :)); hold off % 高级点的做法 figure; g1 = @(x, y) -x .* sin(4 * x) - 1.1 * y .* sin(2 * y); g2 = @(x, y) x + y - 3; h = fimplicit(@(x, y) min(g1(x, y), g2(x, y)), [0 3.5 0 4], 'LineWidth', 2, ...     'Color', all_colors(1, :), 'MeshDensity', 1000); fill(rmmissing(h.XData), rmmissing(h.YData), all_colors(6, :), 'DisplayName', 'g1 > 0 & g2 > 0'); hold on fimplicit({g1, g2}, [0 3.5 0 4], 'LineWidth', 2, 'Color', all_colors(1, :)); hold off legend('NumColumns', 3)     

2.2.5 渐变色曲线、渐变色填充

       clear;clc;close all; % 获取到颜色 [all_themes, all_colors] = GetColors(); x = linspace(0, 3*pi, 360); y = sin(2*x); %%%%% 渐变色填充 figure; fill(x, y, y, 'EdgeColor', 'interp'); colormap(all_themes{1}); %%%%% 渐变色曲线 figure; fill([x, NaN], [y, NaN], [x, NaN], 'EdgeColor', 'interp', 'LineWidth', 2); % 修改颜色模板 colormap(all_themes{1}); %%%%% 渐变色散点 figure; scatter(x, y, x*5+20, x, 'filled'); colormap(all_colors);     

完整版

2.2.6 带箭头曲线

       clear; clc; % 获取到颜色 [all_themes, all_colors] = GetColors(); x = 1:10; y = sin(x); plot(x, y, '.'); hold on for i = 1:9     PlotLineArrow(gca, [x(i), x(i + 1)], [y(i), y(i + 1)], all_colors(6, :), ...         all_colors(1, :), 1); end hold off     

完整版


2.2.7 多个Y轴

       clear;clc;close all; % 获取到颜色 [all_themes, all_colors] = GetColors(); % yyaxis 官方帮助文档 demo figure; x = linspace(0, 10, 100); y1 = 2*sin(3*x); y2 = sin(3*x) .* exp(0.5*x); yyaxis left plot(x, y1, 'LineWidth', 2); yyaxis right plot(x, y2, 'LineWidth', 2); % 三个纵坐标演示,更多纵坐标可以按照此方法类推 figure; y3 = 10*cos(3*x); % 控制 aies 的大小和位置,注意是相对于figure的,范围为[0, 1] % 三条线绘制到一起,注意数据都标准化到 y1 范围 maxY1 = max(y1); maxY2 = max(y2); maxY3 = max(y3); minY1 = min(y1); minY2 = min(y2); minY3 = min(y3); newY2 = (y2 - minY2)/(maxY2 - minY2);   % 归一化 newY2 = newY2*(maxY1 - minY1) + minY1;  % 反归一化 newY3 = (y3 - minY3)/(maxY3 - minY3); newY3 = newY3*(maxY1 - minY1) + minY1; % 画线 h1 = axes('position', [0.1 0.1 0.5 0.8]);   plot(x, y1, 'Color', all_colors(1, :), 'LineWidth', 2); hold on plot(x, newY2, 'Color', all_colors(2, :), 'LineWidth', 2); plot(x, newY3, 'o--', 'Color', all_colors(3, :), 'LineWidth', 2); hold off ylabel('line1'); h1.YColor = all_colors(1, :); % 绘制另外两个空的坐标轴 h2 = axes('position', [0.65 0.1 0.005 0.8]);  % 重复绘制,曲线颜色用白色,和figure背景色一致,看不出来即可 plot(x, y2, 'w') % 颜色,位置,曲线标签 set(h2, 'ycolor', all_colors(2, :), 'yaxislocation', 'right', 'xtick', []) % 边界显示不清楚,所以画一条线 hold on limX2 = get(h2, 'Xlim'); limY2 = get(h2, 'Ylim'); plot([limX2(2), limX2(2)], limY2, 'Color', all_colors(2, :)); hold off % 取消边框 box off ylabel('line2'); % h3 = axes('position', [0.75 0.1 0.005 0.8]);  plot(x, y3, 'w') set(h3, 'ycolor', all_colors(3, :), 'yaxislocation', 'right', 'xtick', []) hold on limX3 = get(h3, 'Xlim'); limY3 = get(h3, 'Ylim'); plot([limX3(2), limX3(2)], limY3, 'Color', all_colors(3, :)); hold off box off ylabel('line3'); % 取消坐标轴的颜色,和figure统一 % set(h1, 'color','none') % set(h2, 'color','none') % set(h3, 'color','none') % figure背景设置成白色 set(gcf,'color','white');      

完整版


2.2.8 旋转多边形

       clear;clc;close all; % 获取到颜色 [all_themes, all_colors] = GetColors(); m = 3; n = 41; d_angle = 2.25; rotate = 45; shift = [0, 0]; figure SpiralPolygon(m, n, d_angle, rotate, shift, all_colors, 1.5); colormap(all_themes{1}); m = 4; figure SpiralPolygon(m, n, d_angle, rotate, shift, all_colors, 1.5); figure; V = SpiralPolygon(m, n, -d_angle, rotate, shift, all_colors, 1.5); hold on dx = max(V(1, :)) - min(V(1, :)); dy = max(V(2, :)) - min(V(2, :)); shift = [dx, 0]; SpiralPolygon(m, n, d_angle, rotate, shift, all_colors, 1.5); shift = [dx, dy]; SpiralPolygon(m, n, -d_angle, rotate, shift, all_colors, 1.5); shift = [0, dy]; SpiralPolygon(m, n, d_angle, rotate, shift, all_colors, 1.5); hold off     

2.2.9 带箭头流线

       clear;clc;close all; % 获取到颜色 [all_themes, all_colors] = GetColors(); [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); streamline(x, y, u, v, startx, starty); figure; lines = streamline(x, y, u, v, startx, starty); for i = 1:length(lines)     lines(i).Color = all_colors(i, :);     lines(i).LineWidth = 2; end % 下面开始画箭头,涉及到坐标的转换 % 获取 Axes 位置 posAxes = get(gca, 'Position'); posX = posAxes(1); posY = posAxes(2); width = posAxes(3); height = posAxes(4); % 获取 Axes 范围 limX = get(gca, 'Xlim'); limY = get(gca, 'Ylim'); minX = limX(1); maxX = limX(2); minY = limY(1); maxY = limY(2); % 遍历,逐条流线加箭头 for i = 1 : length(lines)     % 获取每条流线的数据     xData = lines(i).XData;     yData = lines(i).YData;     % 这里取的是最后两个点,一定要是相邻的两个点用来确定箭头方向     x0 = xData(end-1 : end);     y0 = yData(end-1 : end);     % 转换坐标到相对于figure的坐标     xNew = posX + (x0 - minX) / (maxX - minX) * width;     yNew = posY + (y0 - minY) / (maxY - minY) * height;     % 画箭头     hold on     annotation('arrow', xNew, yNew, 'color', all_colors(6, :)); end hold off title('带箭头的流线图');     

完整版

2.2.10 极坐标绘图技巧

       clear;clc;close all; % 获取到颜色 [all_themes, all_colors] = GetColors(); figure; theta = 0:0.01:2*pi; rho = sin(2*theta).*cos(2*theta); tiledlayout(1, 2); nexttile; polarplot(theta, rho, 'Color', all_colors(1, :), 'LineWidth', 2); nexttile; rMax = 2*max(rho); % 这个看不到线,因为只有一个重复的点 polarplot([0, 2*pi], [rMax, rMax]);     hold on polarplot(theta, rho, 'Color', all_colors(2, :), 'LineWidth', 2); hold off     

2.2.11 条形分布图

       clear;clc;close all; % 获取到颜色 [all_themes, all_colors] = GetColors(); n = 50; x = linspace(-10, 10, n); y = gauss(n, 5) ;  figure; % 设置边缘颜色和宽度 bar(x, y, 'EdgeColor', 'none', 'BarWidth', 1, 'FaceColor', all_colors(1, :)); hold on plot(x, y, 'Color', all_colors(6, :), 'LineWidth', 2); hold off     

2.3 三维绘图 2.3.1 给球面贴背景图

       clear;clc; % 获取到颜色 [all_themes, all_colors] = GetColors(); % 读取地图文件 image = imread('dataWorldMap.jpg');  % 生成一个球面数据 [x, y, z] = sphere(200); % 绘制球体 p = surf(x, y, z);  % 去网格线 shading interp; p.CData = image;  % 纹理贴图 p.FaceColor = "texturemap";  axis equal; axis off % 光源,看起来更有立体感 % light % lighting gouraud % 光源颜色 handle = light('Color', 'w'); t = 0; while t < 100     t = t + 1;     view([t 10]);     lightangle(handle, t, 0);     pause(0.01); end     

2.3.2 三维矢量场图

       clear;clc; % 获取到颜色 [all_themes, all_colors] = GetColors(); % 产生数据 [X, Y, Z] = peaks(30);   % 矢量场,曲面法线 [U, V, W] = surfnorm(X, Y, Z);   % 箭头长度、颜色 quiver3(X, Y, Z, U, V, W, 0.5, 'k');      hold on surf(X,Y,Z);    xlim([-3, 3]); ylim([-3, 3]); colormap(all_colors(1:30, :)); %  网格线不显示 shading interp hold off view(0, 90);     

2.3.3 单独设置子图的colormap

       clear;clc;close all; % 获取到颜色 [all_themes, all_colors] = GetColors(); [x, y, z] = peaks(30); figure; plot1 = subplot(1,2,1); surf(x, y, z); % 获取第一幅图的 colormap,默认为 parula cMap = colormap; plot2 = subplot(1,2,2); surf(x, y, z); % 下面设置的是第二幅图的颜色,默认是整个 figure 的 colormap(hot); % 设置第一幅图颜色显示为 parula set(plot1, 'Colormap', cMap); % 用 freezeColors 方法 figure; subplot(1,2,1); surf(x, y, -z); freezeColors; subplot(1,2,2); surf(x, y, -z); colormap(hot); % 一个坐标轴 figure; h1 = surf(x, y, z); freezeColors; hold on h2 = surf(x, y, z + 5); hold off colormap(hot); % 读取本地的 figure 文件进行二次编辑 fig = openfig('dataMultiColormap.fig'); cmap = fig.Colormap; % 多个子图 cmap1 = fig.Children(1).Colormap; cmap2 = fig.Children(2).Colormap; % 数据 figure --> axes --> plot % ax = get(gcf, 'Children'); % plt = get(gca, 'Children'); x = fig.Children(2).Children(1).XData; y = fig.Children(2).Children(1).YData; z = fig.Children(2).Children(1).ZData; figure; surf(x, y, z); colormap(cmap1); freezeColors; hold on surf(x, y, z + 5); hold off colormap(all_themes{4});     

完整版

2.3.4 曲面裁剪

       clear;clc;close all;       

% 获取到颜色 [all_themes, all_colors] = GetColors(); % 生成多峰图 figure; n = 300; [x, y, z] = peaks(n); % 绘制原图 subplot(2, 2, [1,3]) surf(x, y, z); xlim([-3, 3]); ylim([-3, 3]); shading interp view(0, 90) % 裁剪 z(z > 1) = NaN; subplot(2, 2, 2) surf(x, y, z); xlim([-3, 3]); shading interp view(0, 90) subplot(2, 2, 4) surf(x, y, z); colormap(all_themes{1}); shading interp view(0, 45)

完整版

2.3.5 三维曲线、散点图

       clear;clc;close all; % 获取到颜色 [all_themes, all_colors] = GetColors();   %%%%% 三维曲线图 figure; t = 0:pi/20:10*pi; xt = sin(t); yt = cos(t); plot3(xt, yt, t, '-o', 'Color', all_colors(6, :), 'MarkerSize', 10, ...     'MarkerFaceColor', all_colors(1, :), 'LineWidth', 2); figure; x = -20:10:20; y = linspace(0, 10, 100); % 生成 5 组数据 z = zeros(5, 100); z(1, :) = gaussmf(y, [0.5 3]); z(2, :) = gaussmf(y, [0.6 4]); z(3, :) = gaussmf(y, [0.7 5]); z(4, :) = gaussmf(y, [0.8 6]); z(5, :) = gaussmf(y, [0.9 7]); for i = 1:5     % x 方向每条曲线都是一个值     xx = x(i)*ones(1, 100);     % z 方向的值,每次取一条     zz = z(i, :);     % plot3 在 xyz 空间绘制曲线,保证 x y z 长度一致即可     plot3(xx, y, zz, 'LineWidth', 2);     hold on end hold off % 定制颜色 set(gca, 'colororder', all_themes{1}); legend('line1', 'line2', 'line3', 'line4', 'line5'); % 如果把渐变曲线和三维曲线结合起来 figure; x = linspace(0, 10, 100); y = zeros(5, 100); y(1, :) = gaussmf(x, [0.5 3]); y(2, :) = gaussmf(x, [0.6 4]); y(3, :) = gaussmf(x, [0.7 5]); y(4, :) = gaussmf(x, [0.8 6]); y(5, :) = gaussmf(x, [0.9 7]); for i = 1:5     fill3([i*ones(size(x)), NaN], [x, NaN], [y(i, :), NaN], [x NaN], 'EdgeColor', 'interp', 'LineWidth', 2)     hold on end hold off %%%%% 三维散点图 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 = all_colors(6, :); figure; 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 + 10, x, 'filled'); % 定制颜色 colormap(all_colors);     

2.3.6 等高线图

       clear;clc;close all; % 获取到颜色 [all_themes, all_colors] = GetColors();  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); % 定制颜色 colormap(all_themes{1}); 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); % 定制颜色 colormap(all_colors(1:9, :)); %%%%% 等高线填充图 figure; tiledlayout(2, 2, 'TileSpacing', 'compact'); nexttile [X, Y, Z] = peaks(50); contourf(X, Y, Z); nexttile contourf(X, Y, Z,'--'); % 限定范围 nexttile contourf(X, Y, Z, [2 3], 'ShowText', 'on'); nexttile contourf(X, Y, Z,[2 2]); % 定制颜色 colormap(all_colors(1:18, :));     

2.3.7 绘制伪彩图和投影图

       clear;clc;close all; % 获取到颜色 [all_themes, all_colors] = GetColors();  % 创建一个 colormap map = GenColormap(all_themes{1}, 64); x = linspace(-3, 3, 30); y = linspace(-4, 4, 40); [X, Y] = meshgrid(x, y); Z = peaks(X, Y); 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); colormap(map); xlim([-3, 3]); ylim([-4, 4]); view(0, 90); shading interp figure; % 3*3 布局 tiledlayout(3, 3, 'TileSpacing', 'Compact'); % 占据 1*2,也就是 1 2 nexttile([1, 2]); plot(x, z1, 'LineWidth', 2); % 从第 6 个开始,占据 2*1,也就是 6 和 9 nexttile(6, [2, 1]); plot(z2, y, 'LineWidth', 2); % 从第 4 个开始,占据 2*2,也就是 4 5 7 8 nexttile(4, [2, 2]); surf(x, y, Z); colormap(map); xlim([-3, 3]); ylim([-4, 4]); view(0, 90); shading interp     

完整版

2.4 四维绘图 2.4.1 slice切片显示

       clear;clc;close all; % 获取到颜色 [all_themes, all_colors] = GetColors();  % 创建 colormap map = GenColormap(all_themes{1}); [X, Y, Z] = meshgrid(-2:0.2:2); V = X .* exp(-X.^2 - Y.^2 - Z.^2); % 切片位置 xslice = [-1.2, 0.8, 2];    yslice = []; zslice = 0; s = slice(X, Y, Z, V, xslice, yslice, zslice); % 裁剪 for i = 1:length(s)     s(i).XData(10:end, 10:end) = nan;     s(i).YData(10:end, 10:end) = nan;     s(i).ZData(10:end, 10:end) = nan; end % 加 colorbar 方便看值 colorbar colormap(map) shading interp     

2.5 函数方程可视化 2.5.1 一元方程 2.5.2 二元方程 2.5.3 三元方程

完整版

2.6 特殊绘图 2.6.1 动态趋势线可视化

       clear;clc;close all; % 获取到颜色 [all_themes, all_colors] = GetColors();   % 数据,可以补充最好 y = [34,34,34,53,57,60,60,64,69,89,106,125,161,233,345,445,572,717,1010,1322,1264,1678,3489,...      4629,5894,9345,14250,19624,22043,32717,46332,53268,65285,83507,101657,121117,139675,...      161367,184183,206207,236339,270473,312076,334125,363349,394182,425828,463433,498674,...      530384,559245,586941,610632,641397,674829,710021,738697,762496,789383,825306,844992,...      877497,916348,955488,985060,1004942,1029878,1056646,1092656,1125305,1156744,1185167]; len = length(y); x = 1:len; % 第一个点为标记点 plot(x(1), y(1), 'o', 'Color', all_colors(1, :), 'MarkerSize', 10, 'MarkerFaceColor', all_colors(1, :)); hold on % 循环画图 for i = 2:len     % 画两个点     plot(x(i-1:i), y(i-1:i), 'Color', all_colors(1, :), 'LineWidth', 3);     % 设置一下范围     xlim([1, 1.5*x(i)]);     ylim([0, 1.5*y(i)]);     % 删掉之前的文本和标记     delete(findobj('Type','text'));     delete(findobj('Marker','o'));     % 画标记点     plot(x(i), y(i), 'o', 'Color', all_colors(6, :), 'MarkerSize', 10, 'MarkerFaceColor', all_colors(6, :));     % 添加标注     text(x(i),y(i),['     美国:', num2str(y(i))], 'Color', all_colors(1, :));       title('美国新冠数据');     % 暂停 0.1s     pause(0.1) end hold off     

完整版

2.6.2 Newton-Raphson、Julia分形

       clear;clc;close all; % 获取到颜色 [all_themes, all_colors] = GetColors(); % 创建 colormap map = GenColormap(all_themes{2}, 64); n = 1000; m = 1000; f = @(z) z^8 - 1; df = @(z) 8*z^7; [X, Y] = meshgrid(linspace(-2, 2, n), linspace(-2, 2, m)); A = nan(n, m); for i = 1:n     for j = 1:m         z0 = X(i, j) + 1i*Y(i, j);         % Newton-Raphson         z = z0;         ite = 0;         fz = f(z);         while abs(fz) > 1e-6 && ite < 20             z = z - f(z)/df(z);              fz = f(z);             ite = ite + 1;         end         A(i, j) = ite;     end end figure; pcolor(X, Y, A); colormap(map); % hold on % plot([1, -1, 0, 0], [0, 0, 1, -1], '.k', 'MarkerSize', 20); % hold off shading flat axis tight manual off  % xlim([-0.5, 0]); % ylim([0.5, 1]); % 保存成视频 % set(gca, 'nextplot', 'replacechildren');  % v = VideoWriter('NRfractal.avi'); % open(v); % for k = 1:20  %     pcolor(X, Y, A); %     shading flat %     xlim([-2*(1-k/25), 2*(1-k/25)]); %     ylim([-2*(1-k/25), 2*(1-k/25)]); %     frame = getframe(gcf); %     writeVideo(v, frame); % end  % (-0.19, 0.6557) % (-0.1, 0.651) % zx = zx*zx - zy*zy + cx % zy = 2*zx*zy+cy c = - 0.1 + 1i*0.651; radius = 32; [X, Y] = meshgrid(linspace(-1.5, 1.5, n), linspace(-1.5, 1.5, m)); A = nan(n, m); for i = 1:n     for j = 1:m         z = X(i, j) + 1i*Y(i, j);         % Julia         for ite = 1:1000             z = (z*z + c) ;             if abs(z) > radius                 break;             end         end         A(i, j) = ite;     end end figure; pcolor(X, Y, A); colormap(map); shading flat axis tight manual off     

完整版

2.6.3 分形树叶

       clear;clc;close all; % 获取到颜色 [all_themes, all_colors] = GetColors(); % 初始点的位置 x = [0.5; 0.5];         % 绘制初始点 plot(x(1), x(2), '.', 'Color', all_colors(3, :), 'MarkerSize', 10);  % 设置判断向量 h = [0.75, 0.90, 0.98, 1.00]; b1 = [0; 1.5]; b2 = [0; 1.5]; b3 = [0; 0.4]; % 构造仿射变换矩阵 A1 = [0.75, 0.04; -0.04, 0.75]; A2 = [0.20, -0.3; 0.25, 0.23]; A3 = [-0.15, 0.32; 0.26, 0.24]; A4 = [0, 0; 0, 0.15]; %循环绘制 hold on for i = 1 : 1000     r = rand;     if r < h(1)         x = A1 * x + b1;     elseif r < h(2)         x = A2 * x + b2;     elseif r < h(3)         x = A3 * x + b3;     else         x = A4 * x;     end     plot(x(1), x(2), '.', 'Color', all_colors(3, :), 'MarkerSize', 10);     pause(0.001);     % drawnow 太慢了 end hold off     

2.6.4 在指定区域内随机生成不相交不相切的圆

       clear;clc;close all; % 获取到颜色 [all_themes, all_colors] = GetColors();  % 限定范围 width = 10;   height = 10; % 最多产生的圆的个数 circleNumber = 100;    % 记录每个圆的横坐标、纵坐标、半径 paras = zeros(circleNumber, 3);   num = 0; hold on while num < circleNumber     num = num + 1;         % 圆半径小于1或者设定为固定值     r = rand;      % 在范围内随机坐标     xPos = rand*(width-2*r) + r;       yPos = rand*(height-2*r) + r;     % 记录坐标、半径     paras(num,:) = [xPos, yPos, r];     % 判断每个圆的位置是否不相切、不相交     if num > 1         % 新产生的圆和之前产生的所有圆计算距离                 xs = paras(1:num - 1, 1);         ys = paras(1:num - 1, 2);         rs = paras(1:num - 1, 3);         dist1 = sqrt((xPos - xs).^2 + (yPos - ys).^2);         dist2 = abs(r + rs);         % 如果相离则绘制当前产生的圆,否则就重新生成一个圆         if all(dist1 > dist2)             rectangle('Position', [xPos-r, yPos-r, 2*r, 2*r], 'Curvature', [1 1], 'EdgeColor', all_colors(1, :), 'LineWidth', 2);             axis equal         else             r = rand;              xPos = rand*(width-2*r) + r;               yPos = rand*(height-2*r) + r;             paras(num,:) = [xPos,yPos,r];             % 防止死循环             temp = 0;             maxTry = 100;             while any(dist1 <= dist2) && temp < maxTry                 temp = temp + 1;                 dist1 = sqrt((xPos - xs).^2 + (yPos - ys).^2);                 dist2 = abs(r + rs);             end             if all(dist1 > dist2)                 rectangle('Position', [xPos-r, yPos-r, 2*r, 2*r], 'Curvature', [1 1], 'EdgeColor', all_colors(1, :), 'LineWidth', 2);                 axis equal             end         end           end end axis([0 width 0 height]) box on hold off     

完整版

2.6.5 创建gif动图

       clear;clc;close all; % 获取到颜色 [all_themes, all_colors] = GetColors();  x = 0:0.01:1; n = 1:9; len = length(n); im = cell(1, len); % 单独显示每个图 figure; for idx = 1:len     subplot(3, 3, idx)     plot(x, x.^idx, 'LineWidth', 3, 'Color', all_colors(idx, :));     title(['y = x^', num2str(idx)]); end % 获取绘制对象 fig = figure; for idx = 1:len     y = x.^idx;     plot(x, y, 'LineWidth', 3, 'Color', all_colors(idx, :));     title(['y = x^', num2str(n(idx))]) %     drawnow %     pause(0.1);     frame = getframe(fig);     im{idx} = frame2im(frame); end % 输出文件名 filename = 'data	estAnimated.gif';  for idx = 1:len     % 制作gif文件,图像必须是index索引图像     [A, map] = rgb2ind(im{idx}, 256);     if idx == 1         imwrite(A, map, filename, 'gif', 'LoopCount', Inf, 'DelayTime', 0.3);     else         imwrite(A, map, filename, 'gif', 'WriteMode', 'append', 'DelayTime', 0.3);     end end  clear;clc; % 保存文件名 filename = 'Model.gif';   % 4张图 for i = 1:4     % 图片路径     fileName = [num2str(i), '.jpg'];           img = imread(fileName);     % 重定义尺寸     img = imresize(img, [256, 256]);           imshow(img);     % 不显示窗口     set(gcf, 'visible', 'off');        % 获取位置     q = get(gca, 'position');       % 设置左边距离值为零     q(1) = 0;       % 设置右边距离值为零     q(2) = 0;          % 重新设置位置     set(gca, 'position', q);     frame = getframe(gcf, [0, 0, 200, 200]);       im = frame2im(frame);           imshow(im);     [I, map] = rgb2ind(im, 256);     if i == 1         imwrite(I, map, filename, 'gif', 'Loopcount', inf, 'DelayTime', 0.3);     else         imwrite(I, map, filename, 'gif', 'WriteMode', 'append', 'DelayTime', 0.3);     end end     

2.6.6 梯度下降法的下降方向

       clear;clc;close all; % 获取到颜色 [all_themes, all_colors] = GetColors();  % 创建一个 colormap map = GenColormap(all_themes{1}); %% 一元函数梯度下降法 % 示例:f(x) = min{(x - 1)^2} % 梯度:g(x) =  2 * (x - 1) % 学习率,一般设置小一点,否则容易在最小值附近震荡或者不收敛 yita = 0.25;    x1 = -5 : 0.1 : 5; y1 = (x1 - 1).^2; iteMax = 1000; xInit = 4; yInit = (xInit - 1)^2; err = 1e-6; figure('Position', [50, 50, 900, 400]); tiledlayout(1, 2, 'TileSpacing', 'compact'); nexttile plot(x1, y1, 'b', 'LineWidth', 2) xlim([-5, 5]) ylim([-1, 25]) hold on plot(xInit, yInit, 'ok', 'MarkerFaceColor', 'k') for i = 1 : iteMax     % x = x + yita * grad;     xNew = xInit - yita * 2 * (xInit - 1);     yNew = (xNew - 1)^2;     % 退出条件     if abs(xNew - xInit) < err         break;     else         PlotLineArrow(gca, [xInit, xNew], [yInit, yNew], 'k', 'k', 2)         xInit = xNew;         yInit = yNew;         disp(['第', num2str(i), '次迭代结果:', num2str(xInit)]);         plot(xNew, yNew, 'ok', 'MarkerFaceColor', 'k')             end end hold off title('梯度下降');  %% 多元函数梯度下降法 % 示例:f(x) = min{x1^2 + x2^2} % 梯度:g(x) = [2 * x1; 2 * x2] [x, y] = meshgrid(-5:0.5:5, -5:0.5:5); z = x.^2 + y.^2; initX = 4; initY = 3; initZ = initX^2 + initY^2; initValue = [initX; initY]; nexttile contourf(x, y, z, 20); colormap(map); shading interp hold on grad = zeros(1, 2); e = 0.1; yita = 5;   % Adagrad 更快收敛 for i = 1 : iteMax     % 标准的梯度法  x = x + yita * grad; %     newValue = initValue - yita * [2 * initX; 2 * initY];     % Adagrad 法    x = x + yita * inv(G) * grad;     grad = grad + [(2 * initX)^2, (2 * initY)^2];     newValue = initValue - yita * diag(1 ./ sqrt(grad + e)) * [2 * initX; 2 * initY];     % 退出条件     if norm(newValue - initValue) < err         break;     else         newX = newValue(1);         newY = newValue(2);         newZ = newX^2 + newY^2; %         plot([initX, newX], [initY, newY], '-ok', 'MarkerFaceColor', 'r')         PlotLineArrow(gca, [initX, newX], [initY, newY], 'k', 'k', 2)         initValue = newValue;         initX = newX;         initY = newY;         initZ = newZ;         disp(['第', num2str(i), '次迭代结果:', num2str(newValue')]);         end end hold off title('梯度下降');     

2.6.7 目标沿指定轨迹运动

       clear;clc;close all; % 获取到颜色 [all_themes, all_colors] = GetColors();  % 生成轨迹 x = linspace(0, 2*pi, 20); y = sin(x); minX = min(x); maxX = max(x); minY = min(y); maxY = max(y); % 在特定位置绘制轨迹图 h1 = axes('position', [0.1, 0.1, 0.8, 0.8]); plot(x, y, 'Color', all_colors(1, :), 'LineWidth', 2); % 设置显示范围 xlim([minX, maxX]); ylim([minY, maxY]); % 图片尺寸 sizeQQ = 0.05; posX = 0.1; poxY = 0.85; % 图像的位置 h2 = axes('position', [posX poxY sizeQQ sizeQQ]); % 显示图片 imshow('dataqq.bmp'); for i = 1 : length(x)     % 计算图片的位置     posX = (x(i) - minX) / (maxX - minX) * 0.8 + 0.1;     posY = (y(i) - minY) / (maxY - minY) * 0.8 + 0.1;     % 删除之前的图片     delete(h2);     % 在新的位置显示     h2 = axes('position', [posX posY sizeQQ sizeQQ]);      imshow('dataqq.bmp');     % 暂停以显示动态过程     pause(0.1); end     

2.6.8 看起来像拼接两个Axis

       clear;clc;close all; % 获取到颜色 [all_themes, all_colors] = GetColors();  x1 = 0:0.1:2; y1 = x1; x2 = 2:0.1:4; y2 = x2 + 1; ax = axes(); plot(x1, y1, 'Color', all_colors(19, :), 'LineWidth', 2); hold on plot(x2, y2, 'Color', all_colors(24, :), 'LineWidth', 2); fill([0, 2, 2], [0, 0, 0.5], all_colors(1, :)); fill([2, 4, 2], [0.5, 0, 0.5], all_colors(1, :)); fill([0, 2, 2], [0.5, 0.1, 0.5], all_colors(3, :)); fill([2, 4, 2], [0.1, 0.5, 0.5], all_colors(3, :)); fill([1.9, 2.1, 2, 2.1, 1.9, 1.8], [0, 0, 2.5, 5, 5, 2.5], 'w'); hold off % 设置一些属性 grid on ax.XTickLabel = {'0', '0.5', '1', '1.5', '', '3.5', '4', '4.5', '5'}; ax.GridLineStyle = '--';     

2.6.9 设置特殊的图例

       clear;clc;close all; % 获取到颜色 [all_themes, all_colors] = GetColors();  % 生成曲线数据 x = 0:0.2:12; y1 = besselj(1, x); y2 = besselj(2, x); y3 = besselj(3, x); % 先看正常绘图时的 legend,显示出来的线型、标记、颜色和曲线是对应的 figure plot(x, y1, '-o', 'Color', all_colors(1, :), 'LineWidth', 2); hold on plot(x, y2, '-*', 'Color', all_colors(2, :), 'LineWidth', 2); plot(x, y3, '-s', 'Color', all_colors(3, :), 'LineWidth', 2); hold off legend('legend1', 'legend2', 'legend3'); % 如果要让图例颜色都是黑色怎么实现 % 一个很笨但有效的方法,重新绘制一遍将原图覆盖 figure plot(x, y1, '-ok', 'LineWidth', 2); hold on  plot(x, y2, '-*k', 'LineWidth', 2); plot(x, y3, '-sk', 'LineWidth', 2);  plot(x, y1, '-o', 'Color', all_colors(1, :), 'LineWidth', 2); plot(x, y2, '-*', 'Color', all_colors(2, :), 'LineWidth', 2); plot(x, y3, '-s', 'Color', all_colors(3, :), 'LineWidth', 2); hold off % 只显示前面三条曲线的legend legend('legend1', 'legend2', 'legend3'); % 如果想显示指定曲线的legend figure h1 = plot(x, y1, '-ok', 'LineWidth', 2); hold on  h2 = plot(x, y2, '-*k', 'LineWidth', 2); h3 = plot(x, y3, '-sk', 'LineWidth', 2); h4 = plot(x, 2*y1, '-o', 'Color', all_colors(1, :), 'LineWidth', 2); h5 = plot(x, 2*y2, '-*', 'Color', all_colors(2, :), 'LineWidth', 2); h6 = plot(x, 2*y3, '-s', 'Color', all_colors(3, :), 'LineWidth', 2); hold off legend([h1, h3, h5], 'legend1', 'legend3', 'legend5');     

完整版

2.6.10 甲烷模型

       clear;clc;close all; % 获取到颜色 [all_themes, all_colors] = GetColors();  % 球面的坐标信息,为了看起来平滑一点,给到 100 [x, y, z] = sphere(100); % C 大小 C = 10;  % H 大小 H = 5; figure; % 大球 surf(C*x, C*y, C*z, 'FaceColor', all_colors(6, :), 'EdgeColor', 'none') hold on  % 四个小球,都偏离一点位置,准确的位置需要计算,这里演示一个大概位置 surf(H*x, H*y, H*z + 10, 'FaceColor', all_colors(1, :), 'EdgeColor', 'none'); surf(H*x + 10, H*y, H*z - 3, 'FaceColor', all_colors(1, :), 'EdgeColor', 'none'); surf(H*x - 4, H*y - 10, H*z - 3, 'FaceColor', all_colors(1, :), 'EdgeColor', 'none'); surf(H*x - 4, H*y + 10, H*z - 3, 'FaceColor', all_colors(1, :), 'EdgeColor', 'none'); % 坐标轴设置 axis equal off % 光源,看起来更有立体感 light lighting gouraud     

2.6.11 在随机圆里产生随机点

       clear;clc;close all; % 获取到颜色 [all_themes, all_colors] = GetColors();  % 半径 r = 8; % 圆心距 d = 50; % 圆 1 的坐标,可变 ox1 = 10; oy1 = 10; % 圆 2 的坐标,可变,那就随机吧 theta_r = rand*2*pi; ox2 = ox1 + d*cos(theta_r); oy2 = oy1 + d*sin(theta_r); % 用极坐标形式,毫无疑问 theta = linspace(0, 2*pi, 100); % 圆坐标 x1 = ox1 + r*cos(theta); y1 = oy1 + r*sin(theta); x2 = ox2 + r*cos(theta); y2 = oy2 + r*sin(theta); % 画图 plot(x1, y1, 'Color', all_colors(19, :), 'LineWidth', 2); hold on plot(x2, y2, 'Color', all_colors(24, :), 'LineWidth', 2); count = 100; % 散点位置 % 随机半径 rs1 = rand(1, count)*r; % 随机角度 thetas1 = rand(1, count)*2*pi; % 确定坐标 xs1 = ox1 + rs1 .* cos(thetas1); ys1 = oy1 + rs1 .* sin(thetas1); % 同样来一遍 rs2 = rand(1, count)*r; thetas2 = rand(1, count)*2*pi; xs2 = ox2 + rs1 .* cos(thetas2); ys2 = oy2 + rs1 .* sin(thetas2); % 画图 plot(xs1, ys1, '.', 'Color', all_colors(1, :), 'MarkerSize', 10); plot(xs2, ys2, '.', 'Color', all_colors(2, :), 'MarkerSize', 10); hold off % 设置一下显示比例 x_range = xlim; y_range = ylim; x_len = x_range(2) - x_range(1); y_len = y_range(2) - y_range(1); set(gcf, 'Position', [50, 50, x_len*10, y_len*10]);     

2.6.12 斜线填充的曲线

       clear;clc;close all; % 获取到颜色 [all_themes, all_colors] = GetColors();  syms t n = 30; x = linspace(-pi, pi, n); y = sin(x); ax = axes; % 曲线 plot(ax, x, y, 'k'); hold on % 画带箭头 x 轴和 y 轴 PlotLineArrow(ax, [-2*pi, 2*pi], [0, 0], 'none', 'k', 1); PlotLineArrow(ax, [0, 0], [-2, 2], 'none', 'k', 1); % 计算所有的斜线 一条斜线两个点 xx = zeros(1, n); for i = 1:n     % 这个方程用来计算斜线的位置     f1 = 2*(t - x(i));     f2 = sin(t);     sol = vpasolve(f1 == f2, t);     xx(i) = double(sol); end yy = sin(xx); % 画斜线 for i = 1:n     % 分 x 正负情况     if x(i) < 0         plot([xx(i), x(i)], [yy(i), 0], 'Color', all_colors(1, :));     else         plot([x(i), xx(i)], [0, yy(i)], 'Color', all_colors(1, :));     end end hold off % 几个关键点 text(-pi, 0.1, '(-pi, 0)'); text(pi/2, 1.1, '(-pi/2, 1)');     

完整版

2.6.13 球板模型

       clear;clc;close all; % 获取到颜色 [all_themes, all_colors] = GetColors();   % 生成球面数据 [x1, y1, z1] = sphere(101);          z0 = z1; % 半径 0.8 之外的不显示 z1(x1.^2 + y1.^2 >= 0.8^2) = nan;    x0 = linspace(-1.2, 1.2, 25); z = linspace(-0.6, 0.6, 10); [x, z] = meshgrid(x0, z); y0 = zeros(size(x)); [x2, y2] = meshgrid(x0); % 生成圆柱数据 [x3, y3, z3] = cylinder(0.8);        s = zeros(size(x2)); % 绘制两个球冠 surf(x1, y1, z1); hold on % 绘制圆柱面 surf(x3, y3, 1.2 * (z3 - 0.5)); colormap('hot'); freezeColors; % 绘制上下两个面 surf(x2, y2, s-0.6);               surf(x2, y2, s+0.6); % 绘制四个侧面 mesh(x, y0-1.2, z); mesh(x, y0+1.2, z); mesh(y0+1.2, x, z); mesh(y0-1.2, x, z); % 绘制两个大的球冠 mesh(1.2*x1, 1.2*y1, 1.2*z0); hold off colormap(GenColormap(all_themes{1}, 64)); % 镂空 hidden off shading interp axis equal axis square axis off     

完整版

2.6.14 BP神经网络图

       clear;clc;close all; figure; x1 = ones(1, 5); x2 = 2 * ones(1, 11); x3 = 3 * ones(1, 8); % 神经元位置 y1 = 4:8; y2 = 1:11; y3 = 2.5:9.5; % 神经元之间的连线 for i = 1:5     for j = 1:11         plot([x1(i), x2(j)], [y1(i), y2(j)], 'k');         hold on     end end for i = 1:11     for j = 1:8         plot([x2(i), x3(j)], [y2(i), y3(j)], 'k');     end end % 神经元 scatter(x1, y1, 200, 'k', 'MarkerFaceColor', 'r'); scatter(x2, y2, 200, 'k', 'MarkerFaceColor', 'y'); scatter(x3, y3, 200, 'k', 'MarkerFaceColor', 'k'); plot([1.5, 1.5], [0, 11], 'k--'); plot([2.5, 2.5], [0, 11], 'k--'); hold off axis off % 文本 text(1.5, 11.75, '输入层'); text(2, 11.75, '隐藏层'); text(2.5, 11.75, '输出层');     

2.6.15 由一系列圆构成的圆环

       clear;clc;close all; % 获取到颜色 [all_themes, all_colors] = GetColors();  x0 = 0; y0 = 0; r1 = 2; r2 = 1; theta = 0:pi/50:2*pi; for i = 1:length(theta)     % 系列小圆的圆心坐标 极坐标方式     x = x0 + r1 * cos(theta(i));     y = y0 + r1 * sin(theta(i));     % 以小圆圆心画圆 极坐标方式     xx = x + r2 * cos(theta);     yy = y + r2 * sin(theta);     plot(xx, yy, 'Color', all_colors(1, :));     hold on end hold off % 限定显示范围 xlim([-3, 3]); ylim([-3, 3]); % x y 轴等宽 axis equal % 紧凑 axis tight     

2.6.16 棋盘格

       clear;clc;close all; % 获取到颜色 [all_themes, all_colors] = GetColors();  figure; % 生成数据点 [x, y] = meshgrid(30:39); x = x(:); y = y(:); scatter(x, y, 100, all_colors(1, :), 'LineWidth', 2); hold on % 随机填充几个点 number = 10; index_x = randi(9, 1, number) + 30; index_y = randi(9, 1, number) + 30; scatter(index_x, index_y, 100, all_colors(6, :), 'MarkerFaceColor', all_colors(6, :), 'LineWidth', 2); hold off % 显示网格 grid on % 网格属性 set(gca, 'GridAlpha', 1, 'GridColor', [0 0 0], 'GridLineStyle', '--');     











user avatar   guo-tong-89-88 网友的相关建议: 
      

玩过瘟疫公司的人都知道,不能太早点致死

……

兄弟萌,点错了,不是死早了影响传播,而是死人很容易让人重视起来,造疫苗啥的,病毒再猛,也是蛋白质和遗传物质组成的。


user avatar   MATHmodels 网友的相关建议: 
      

玩过瘟疫公司的人都知道,不能太早点致死

……

兄弟萌,点错了,不是死早了影响传播,而是死人很容易让人重视起来,造疫苗啥的,病毒再猛,也是蛋白质和遗传物质组成的。




  

相关话题

  如何在公网下载学术文献? 
  四边不等的一般四边形如何求面积? 
  有哪些比较好的数学建模的评价模型? 
  股市之中百分之九十的散户都是在亏钱,那从概率角度来看,我是否大概率能赢散户的钱? 
  什么是寄存器压栈?栈是内存的一部分,寄存器是cpu一部分,怎么压? 
  数学专业的实变函数为什么这么难? 
  如何看待世界顶尖数学家、菲尔兹奖得主 Caucher Birkar 将全职加盟清华大学? 
  现在手机 SoC 性能大概相当于哪个时代的 PC? 
  是否存在有源有旋场,不是说有旋必定无源? 
  有哪些神奇的数学巧合? 

前一个讨论
教育部表示「2022届高校毕业生规模预计 1076 万人,规模创历史新高」,会给就业形势带来哪些变化?
下一个讨论
读博期间生娃有什么需要注意的吗?





© 2024-05-11 - tinynew.org. All Rights Reserved.
© 2024-05-11 - tinynew.org. 保留所有权利