|
发表于 2023-1-7 16:30:38
|
显示全部楼层
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.1 色彩模板】科研绘图必备的60套色彩模板
2.1.2 定制colormap
作为一位及格的科研工作者,绘制三维曲面图、伪彩图,怎样少得了自界说专属 colormap,显现高端优美的视图结果,挑选几种色彩,自动天生colormap,方式在这里
【2.1.2 定制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.2 带有误差线的柱状图】Matlab科研论文作图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.3 简单曲线之间添补】Matlab实现曲线之间添补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], &#39;LineWidth&#39;, 2, ...
- &#39;Color&#39;, all_colors(1, :), &#39;MeshDensity&#39;, 1000);
- fill(rmmissing(h.XData), rmmissing(h.YData), all_colors(6, :), &#39;DisplayName&#39;, &#39;g1 > 0 & g2 > 0&#39;);
- hold on
- fimplicit({g1, g2}, [0 3.5 0 4], &#39;LineWidth&#39;, 2, &#39;Color&#39;, all_colors(1, :));
- hold off
- legend(&#39;NumColumns&#39;, 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, &#39;EdgeColor&#39;, &#39;interp&#39;);
- colormap(all_themes{1});
- %%%%% 突变色曲线
- figure;
- fill([x, NaN], [y, NaN], [x, NaN], &#39;EdgeColor&#39;, &#39;interp&#39;, &#39;LineWidth&#39;, 2);
- % 点窜色彩模板
- colormap(all_themes{1});
- %%%%% 突变色散点
- figure;
- scatter(x, y, x*5+20, x, &#39;filled&#39;);
- colormap(all_colors);
复制代码
完整版
【2.2.5 突变色曲线、突变色添补】Matlab科研论文作图 2.2.6 带箭头曲线
- clear; clc;
- % 获得到色彩
- [all_themes, all_colors] = GetColors();
- x = 1:10;
- y = sin(x);
- plot(x, y, &#39;.&#39;);
- 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.6 带箭头曲线】Matlab科研论文作图 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, &#39;LineWidth&#39;, 2);
- yyaxis right
- plot(x, y2, &#39;LineWidth&#39;, 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(&#39;position&#39;, [0.1 0.1 0.5 0.8]);
- plot(x, y1, &#39;Color&#39;, all_colors(1, :), &#39;LineWidth&#39;, 2);
- hold on
- plot(x, newY2, &#39;Color&#39;, all_colors(2, :), &#39;LineWidth&#39;, 2);
- plot(x, newY3, &#39;o--&#39;, &#39;Color&#39;, all_colors(3, :), &#39;LineWidth&#39;, 2);
- hold off
- ylabel(&#39;line1&#39;);
- h1.YColor = all_colors(1, :);
- % 绘制别的两个空的坐标轴
- h2 = axes(&#39;position&#39;, [0.65 0.1 0.005 0.8]);
- % 反复绘制,曲线色彩用红色,和figure布风景分歧,看不出来即可
- plot(x, y2, &#39;w&#39;)
- % 色彩,位置,曲线标签
- set(h2, &#39;ycolor&#39;, all_colors(2, :), &#39;yaxislocation&#39;, &#39;right&#39;, &#39;xtick&#39;, [])
- % 鸿沟显现不清楚,所以画一条线
- hold on
- limX2 = get(h2, &#39;Xlim&#39;);
- limY2 = get(h2, &#39;Ylim&#39;);
- plot([limX2(2), limX2(2)], limY2, &#39;Color&#39;, all_colors(2, :));
- hold off
- % 取消边框
- box off
- ylabel(&#39;line2&#39;);
- %
- h3 = axes(&#39;position&#39;, [0.75 0.1 0.005 0.8]);
- plot(x, y3, &#39;w&#39;)
- set(h3, &#39;ycolor&#39;, all_colors(3, :), &#39;yaxislocation&#39;, &#39;right&#39;, &#39;xtick&#39;, [])
- hold on
- limX3 = get(h3, &#39;Xlim&#39;);
- limY3 = get(h3, &#39;Ylim&#39;);
- plot([limX3(2), limX3(2)], limY3, &#39;Color&#39;, all_colors(3, :));
- hold off
- box off
- ylabel(&#39;line3&#39;);
- % 取消坐标轴的色彩,和figure同一
- % set(h1, &#39;color&#39;,&#39;none&#39;)
- % set(h2, &#39;color&#39;,&#39;none&#39;)
- % set(h3, &#39;color&#39;,&#39;none&#39;)
- % figure布景设备成红色
- set(gcf,&#39;color&#39;,&#39;white&#39;);
复制代码 完整版
【2.2.7 多个Y轴】Matlab科研论文作图 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, &#39;Position&#39;);
- posX = posAxes(1);
- posY = posAxes(2);
- width = posAxes(3);
- height = posAxes(4);
- % 获得 Axes 范围
- limX = get(gca, &#39;Xlim&#39;);
- limY = get(gca, &#39;Ylim&#39;);
- 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(&#39;arrow&#39;, xNew, yNew, &#39;color&#39;, all_colors(6, :));
- end
- hold off
- title(&#39;带箭头的流线图&#39;);
复制代码
完整版
【2.2.6 带箭头流线】Matlab科研论文作图 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, &#39;Color&#39;, all_colors(1, :), &#39;LineWidth&#39;, 2);
- nexttile;
- rMax = 2*max(rho);
- % 这个看不到线,由于只要一个反复的点
- polarplot([0, 2*pi], [rMax, rMax]);
- hold on
- polarplot(theta, rho, &#39;Color&#39;, all_colors(2, :), &#39;LineWidth&#39;, 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, &#39;EdgeColor&#39;, &#39;none&#39;, &#39;BarWidth&#39;, 1, &#39;FaceColor&#39;, all_colors(1, :));
- hold on
- plot(x, y, &#39;Color&#39;, all_colors(6, :), &#39;LineWidth&#39;, 2);
- hold off
复制代码 2.3 三维绘图 2.3.1 给球面贴布景图
- clear;clc;
- % 获得到色彩
- [all_themes, all_colors] = GetColors();
- % 读取舆图文件
- image = imread(&#39;data\WorldMap.jpg&#39;);
- % 天生一个球面数据
- [x, y, z] = sphere(200);
- % 绘制球体
- p = surf(x, y, z);
- % 去网格线
- shading interp;
- p.CData = image;
- % 纹理贴图
- p.FaceColor = &#34;texturemap&#34;;
- axis equal;
- axis off
- % 光源,看起来更有立体感
- % light
- % lighting gouraud
- % 光源色彩
- handle = light(&#39;Color&#39;, &#39;w&#39;);
- 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, &#39;k&#39;);
- 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, &#39;Colormap&#39;, 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(&#39;data\MultiColormap.fig&#39;);
- cmap = fig.Colormap;
- % 多个子图
- cmap1 = fig.Children(1).Colormap;
- cmap2 = fig.Children(2).Colormap;
- % 数据 figure --> axes --> plot
- % ax = get(gcf, &#39;Children&#39;);
- % plt = get(gca, &#39;Children&#39;);
- 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.3 零丁设备子图的colormap】Matlab科研论文作图 2.3.4 曲面裁剪
% 获得到色彩 [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.4 曲面裁剪】Matlab科研论文作图 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, &#39;-o&#39;, &#39;Color&#39;, all_colors(6, :), &#39;MarkerSize&#39;, 10, ...
- &#39;MarkerFaceColor&#39;, all_colors(1, :), &#39;LineWidth&#39;, 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, &#39;LineWidth&#39;, 2);
- hold on
- end
- hold off
- % 定制色彩
- set(gca, &#39;colororder&#39;, all_themes{1});
- legend(&#39;line1&#39;, &#39;line2&#39;, &#39;line3&#39;, &#39;line4&#39;, &#39;line5&#39;);
- % 假如把突变曲线和三维曲线连系起来
- 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], &#39;EdgeColor&#39;, &#39;interp&#39;, &#39;LineWidth&#39;, 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, &#39;filled&#39;);
- % 定制色彩
- 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, &#39;LineWidth&#39;, 2);
- subplot(2, 2, 2);
- contour(X, Y, Z, &#39;--&#39;, &#39;LineWidth&#39;, 2)
- subplot(2, 2, 3);
- v = [1, 1];
- contour(X, Y, Z, v, &#39;LineWidth&#39;, 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, &#39;ShowText&#39;,&#39;on&#39;, &#39;LineWidth&#39;, 2);
- % 定制色彩
- colormap(all_themes{1});
- figure(&#39;Position&#39;, [0, 0, 900, 400]);
- subplot(1, 3, 1);
- [X, Y, Z] = sphere(50);
- contour3(X, Y, Z, &#39;LineWidth&#39;, 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], &#39;ShowText&#39;, &#39;on&#39;, &#39;LineWidth&#39;, 2)
- [X, Y, Z] = peaks;
- subplot(1, 3, 3);
- contour3(X, Y, Z, [2 2], &#39;LineWidth&#39;, 2);
- % 定制色彩
- colormap(all_colors(1:9, :));
- %%%%% 等高线添补图
- figure;
- tiledlayout(2, 2, &#39;TileSpacing&#39;, &#39;compact&#39;);
- nexttile
- [X, Y, Z] = peaks(50);
- contourf(X, Y, Z);
- nexttile
- contourf(X, Y, Z,&#39;--&#39;);
- % 限制范围
- nexttile
- contourf(X, Y, Z, [2 3], &#39;ShowText&#39;, &#39;on&#39;);
- 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, &#39;LineWidth&#39;, 2);
- subplot(3, 3, [6, 9]);
- plot(z2, y, &#39;LineWidth&#39;, 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, &#39;TileSpacing&#39;, &#39;Compact&#39;);
- % 占据 1*2,也就是 1 2
- nexttile([1, 2]);
- plot(x, z1, &#39;LineWidth&#39;, 2);
- % 从第 6 个起头,占据 2*1,也就是 6 和 9
- nexttile(6, [2, 1]);
- plot(z2, y, &#39;LineWidth&#39;, 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.3.7 绘制伪彩图和投影图】Matlab科研论文作图 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.5 函数方程可视化】Matlab科研论文作图 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), &#39;o&#39;, &#39;Color&#39;, all_colors(1, :), &#39;MarkerSize&#39;, 10, &#39;MarkerFaceColor&#39;, all_colors(1, :));
- hold on
- % 循环绘图
- for i = 2:len
- % 画两个点
- plot(x(i-1:i), y(i-1:i), &#39;Color&#39;, all_colors(1, :), &#39;LineWidth&#39;, 3);
- % 设备一下范围
- xlim([1, 1.5*x(i)]);
- ylim([0, 1.5*y(i)]);
- % 删掉之前的文本和标志
- delete(findobj(&#39;Type&#39;,&#39;text&#39;));
- delete(findobj(&#39;Marker&#39;,&#39;o&#39;));
- % 画标志点
- plot(x(i), y(i), &#39;o&#39;, &#39;Color&#39;, all_colors(6, :), &#39;MarkerSize&#39;, 10, &#39;MarkerFaceColor&#39;, all_colors(6, :));
- % 增加标注
- text(x(i),y(i),[&#39; 美国:&#39;, num2str(y(i))], &#39;Color&#39;, all_colors(1, :));
- title(&#39;美国新冠数据&#39;);
- % 停息 0.1s
- pause(0.1)
- end
- hold off
复制代码
完整版
【2.6.1 静态趋向线可视化】Matlab绘制静态趋向线 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], &#39;.k&#39;, &#39;MarkerSize&#39;, 20);
- % hold off
- shading flat
- axis tight manual off
- % xlim([-0.5, 0]);
- % ylim([0.5, 1]);
- % 保存成视频
- % set(gca, &#39;nextplot&#39;, &#39;replacechildren&#39;);
- % v = VideoWriter(&#39;NRfractal.avi&#39;);
- % 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.2 Newton-Raphson、Julia分形】数学之美:Matlab绘制分形图 2.6.3 分形树叶- clear;clc;close all;
- % 获得到色彩
- [all_themes, all_colors] = GetColors();
- % 初始点的位置
- x = [0.5; 0.5];
- % 绘制初始点
- plot(x(1), x(2), &#39;.&#39;, &#39;Color&#39;, all_colors(3, :), &#39;MarkerSize&#39;, 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), &#39;.&#39;, &#39;Color&#39;, all_colors(3, :), &#39;MarkerSize&#39;, 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(&#39;Position&#39;, [xPos-r, yPos-r, 2*r, 2*r], &#39;Curvature&#39;, [1 1], &#39;EdgeColor&#39;, all_colors(1, :), &#39;LineWidth&#39;, 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(&#39;Position&#39;, [xPos-r, yPos-r, 2*r, 2*r], &#39;Curvature&#39;, [1 1], &#39;EdgeColor&#39;, all_colors(1, :), &#39;LineWidth&#39;, 2);
- axis equal
- end
- end
- end
- end
- axis([0 width 0 height])
- box on
- hold off
复制代码
完整版
【2.6.4 在指定地区内随机天生不订交不相切的圆 】Matlab科研论文作图 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, &#39;LineWidth&#39;, 3, &#39;Color&#39;, all_colors(idx, :));
- title([&#39;y = x^&#39;, num2str(idx)]);
- end
- % 获得绘制工具
- fig = figure;
- for idx = 1:len
- y = x.^idx;
- plot(x, y, &#39;LineWidth&#39;, 3, &#39;Color&#39;, all_colors(idx, :));
- title([&#39;y = x^&#39;, num2str(n(idx))])
- % drawnow
- % pause(0.1);
- frame = getframe(fig);
- im{idx} = frame2im(frame);
- end
- % 输出文件名
- filename = &#39;data\testAnimated.gif&#39;;
- for idx = 1:len
- % 建造gif文件,图像必须是index索引图像
- [A, map] = rgb2ind(im{idx}, 256);
- if idx == 1
- imwrite(A, map, filename, &#39;gif&#39;, &#39;LoopCount&#39;, Inf, &#39;DelayTime&#39;, 0.3);
- else
- imwrite(A, map, filename, &#39;gif&#39;, &#39;WriteMode&#39;, &#39;append&#39;, &#39;DelayTime&#39;, 0.3);
- end
- end
- clear;clc;
- % 保存文件名
- filename = &#39;Model.gif&#39;;
- % 4张图
- for i = 1:4
- % 图片途径
- fileName = [num2str(i), &#39;.jpg&#39;];
- img = imread(fileName);
- % 重界说尺寸
- img = imresize(img, [256, 256]);
- imshow(img);
- % 不显现窗口
- set(gcf, &#39;visible&#39;, &#39;off&#39;);
- % 获得位置
- q = get(gca, &#39;position&#39;);
- % 设备左侧间隔值为零
- q(1) = 0;
- % 设备右侧间隔值为零
- q(2) = 0;
- % 重新设备位置
- set(gca, &#39;position&#39;, 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, &#39;gif&#39;, &#39;Loopcount&#39;, inf, &#39;DelayTime&#39;, 0.3);
- else
- imwrite(I, map, filename, &#39;gif&#39;, &#39;WriteMode&#39;, &#39;append&#39;, &#39;DelayTime&#39;, 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(&#39;Position&#39;, [50, 50, 900, 400]);
- tiledlayout(1, 2, &#39;TileSpacing&#39;, &#39;compact&#39;);
- nexttile
- plot(x1, y1, &#39;b&#39;, &#39;LineWidth&#39;, 2)
- xlim([-5, 5])
- ylim([-1, 25])
- hold on
- plot(xInit, yInit, &#39;ok&#39;, &#39;MarkerFaceColor&#39;, &#39;k&#39;)
- 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], &#39;k&#39;, &#39;k&#39;, 2)
- xInit = xNew;
- yInit = yNew;
- disp([&#39;第&#39;, num2str(i), &#39;次迭代成果:&#39;, num2str(xInit)]);
- plot(xNew, yNew, &#39;ok&#39;, &#39;MarkerFaceColor&#39;, &#39;k&#39;)
- end
- end
- hold off
- title(&#39;梯度下降&#39;);
- %% 多元函数梯度下降法
- % 示例: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], &#39;-ok&#39;, &#39;MarkerFaceColor&#39;, &#39;r&#39;)
- PlotLineArrow(gca, [initX, newX], [initY, newY], &#39;k&#39;, &#39;k&#39;, 2)
- initValue = newValue;
- initX = newX;
- initY = newY;
- initZ = newZ;
- disp([&#39;第&#39;, num2str(i), &#39;次迭代成果:&#39;, num2str(newValue&#39;)]);
- end
- end
- hold off
- title(&#39;梯度下降&#39;);
复制代码 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(&#39;position&#39;, [0.1, 0.1, 0.8, 0.8]);
- plot(x, y, &#39;Color&#39;, all_colors(1, :), &#39;LineWidth&#39;, 2);
- % 设备显现范围
- xlim([minX, maxX]);
- ylim([minY, maxY]);
- % 图片尺寸
- sizeQQ = 0.05;
- posX = 0.1;
- poxY = 0.85;
- % 图像的位置
- h2 = axes(&#39;position&#39;, [posX poxY sizeQQ sizeQQ]);
- % 显现图片
- imshow(&#39;data\qq.bmp&#39;);
- 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(&#39;position&#39;, [posX posY sizeQQ sizeQQ]);
- imshow(&#39;data\qq.bmp&#39;);
- % 停息以显现静态进程
- 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, &#39;Color&#39;, all_colors(19, :), &#39;LineWidth&#39;, 2);
- hold on
- plot(x2, y2, &#39;Color&#39;, all_colors(24, :), &#39;LineWidth&#39;, 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], &#39;w&#39;);
- hold off
- % 设备一些属性
- grid on
- ax.XTickLabel = {&#39;0&#39;, &#39;0.5&#39;, &#39;1&#39;, &#39;1.5&#39;, &#39;&#39;, &#39;3.5&#39;, &#39;4&#39;, &#39;4.5&#39;, &#39;5&#39;};
- ax.GridLineStyle = &#39;--&#39;;
复制代码 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, &#39;-o&#39;, &#39;Color&#39;, all_colors(1, :), &#39;LineWidth&#39;, 2);
- hold on
- plot(x, y2, &#39;-*&#39;, &#39;Color&#39;, all_colors(2, :), &#39;LineWidth&#39;, 2);
- plot(x, y3, &#39;-s&#39;, &#39;Color&#39;, all_colors(3, :), &#39;LineWidth&#39;, 2);
- hold off
- legend(&#39;legend1&#39;, &#39;legend2&#39;, &#39;legend3&#39;);
- % 假如要让图例色彩都是黑色怎样实现
- % 一个很笨但有用的方式,重新绘制一遍将原图覆盖
- figure
- plot(x, y1, &#39;-ok&#39;, &#39;LineWidth&#39;, 2);
- hold on
- plot(x, y2, &#39;-*k&#39;, &#39;LineWidth&#39;, 2);
- plot(x, y3, &#39;-sk&#39;, &#39;LineWidth&#39;, 2);
- plot(x, y1, &#39;-o&#39;, &#39;Color&#39;, all_colors(1, :), &#39;LineWidth&#39;, 2);
- plot(x, y2, &#39;-*&#39;, &#39;Color&#39;, all_colors(2, :), &#39;LineWidth&#39;, 2);
- plot(x, y3, &#39;-s&#39;, &#39;Color&#39;, all_colors(3, :), &#39;LineWidth&#39;, 2);
- hold off
- % 只显现前面三条曲线的legend
- legend(&#39;legend1&#39;, &#39;legend2&#39;, &#39;legend3&#39;);
- % 假如想显现指定曲线的legend
- figure
- h1 = plot(x, y1, &#39;-ok&#39;, &#39;LineWidth&#39;, 2);
- hold on
- h2 = plot(x, y2, &#39;-*k&#39;, &#39;LineWidth&#39;, 2);
- h3 = plot(x, y3, &#39;-sk&#39;, &#39;LineWidth&#39;, 2);
- h4 = plot(x, 2*y1, &#39;-o&#39;, &#39;Color&#39;, all_colors(1, :), &#39;LineWidth&#39;, 2);
- h5 = plot(x, 2*y2, &#39;-*&#39;, &#39;Color&#39;, all_colors(2, :), &#39;LineWidth&#39;, 2);
- h6 = plot(x, 2*y3, &#39;-s&#39;, &#39;Color&#39;, all_colors(3, :), &#39;LineWidth&#39;, 2);
- hold off
- legend([h1, h3, h5], &#39;legend1&#39;, &#39;legend3&#39;, &#39;legend5&#39;);
复制代码 完整版
【2.6.9 设备特别的图例 】Matlab科研论文作图 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, &#39;FaceColor&#39;, all_colors(6, :), &#39;EdgeColor&#39;, &#39;none&#39;)
- hold on
- % 四个小球,都偏离一点位置,正确的位置需要计较,这里演示一个大要位置
- surf(H*x, H*y, H*z + 10, &#39;FaceColor&#39;, all_colors(1, :), &#39;EdgeColor&#39;, &#39;none&#39;);
- surf(H*x + 10, H*y, H*z - 3, &#39;FaceColor&#39;, all_colors(1, :), &#39;EdgeColor&#39;, &#39;none&#39;);
- surf(H*x - 4, H*y - 10, H*z - 3, &#39;FaceColor&#39;, all_colors(1, :), &#39;EdgeColor&#39;, &#39;none&#39;);
- surf(H*x - 4, H*y + 10, H*z - 3, &#39;FaceColor&#39;, all_colors(1, :), &#39;EdgeColor&#39;, &#39;none&#39;);
- % 坐标轴设备
- 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, &#39;Color&#39;, all_colors(19, :), &#39;LineWidth&#39;, 2);
- hold on
- plot(x2, y2, &#39;Color&#39;, all_colors(24, :), &#39;LineWidth&#39;, 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, &#39;.&#39;, &#39;Color&#39;, all_colors(1, :), &#39;MarkerSize&#39;, 10);
- plot(xs2, ys2, &#39;.&#39;, &#39;Color&#39;, all_colors(2, :), &#39;MarkerSize&#39;, 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, &#39;Position&#39;, [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, &#39;k&#39;);
- hold on
- % 画带箭头 x 轴和 y 轴
- PlotLineArrow(ax, [-2*pi, 2*pi], [0, 0], &#39;none&#39;, &#39;k&#39;, 1);
- PlotLineArrow(ax, [0, 0], [-2, 2], &#39;none&#39;, &#39;k&#39;, 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], &#39;Color&#39;, all_colors(1, :));
- else
- plot([x(i), xx(i)], [0, yy(i)], &#39;Color&#39;, all_colors(1, :));
- end
- end
- hold off
- % 几个关键点
- text(-pi, 0.1, &#39;(-\pi, 0)&#39;);
- text(pi/2, 1.1, &#39;(-\pi/2, 1)&#39;);
复制代码 完整版
【2.6.12 绘制带斜线添补的曲线图】Matlab科研论文作图 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(&#39;hot&#39;);
- 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.13 球板模子】Matlab科研论文作图 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)], &#39;k&#39;);
- hold on
- end
- end
- for i = 1:11
- for j = 1:8
- plot([x2(i), x3(j)], [y2(i), y3(j)], &#39;k&#39;);
- end
- end
- % 神经元
- scatter(x1, y1, 200, &#39;k&#39;, &#39;MarkerFaceColor&#39;, &#39;r&#39;);
- scatter(x2, y2, 200, &#39;k&#39;, &#39;MarkerFaceColor&#39;, &#39;y&#39;);
- scatter(x3, y3, 200, &#39;k&#39;, &#39;MarkerFaceColor&#39;, &#39;k&#39;);
- plot([1.5, 1.5], [0, 11], &#39;k--&#39;);
- plot([2.5, 2.5], [0, 11], &#39;k--&#39;);
- hold off
- axis off
- % 文本
- text(1.5, 11.75, &#39;输入层&#39;);
- text(2, 11.75, &#39;隐藏层&#39;);
- text(2.5, 11.75, &#39;输出层&#39;);
复制代码
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, &#39;Color&#39;, 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, :), &#39;LineWidth&#39;, 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, :), &#39;MarkerFaceColor&#39;, all_colors(6, :), &#39;LineWidth&#39;, 2);
- hold off
- % 显现网格
- grid on
- % 网格属性
- set(gca, &#39;GridAlpha&#39;, 1, &#39;GridColor&#39;, [0 0 0], &#39;GridLineStyle&#39;, &#39;--&#39;);
复制代码
|
|