|
演示如何用 Matlab 绘制常见的高质量论文插图;
2022-3-13,更新绘图案例
先睹为快
- 二维曲线图
- 二维散点图
- 二维渐变色图
- 二维条形图
- 二维填充图
- 多 Y 轴图
- 二维场图
- 带箭头曲线图
- 三维曲线图
- 三维散点图
- 伪彩图
- 裁剪伪彩图
- 等高线图
- 等高线填充图
- 矢量场图
- 伪彩图+投影图
- 条形分布图
- 热图
- 分子模型图
- 分形图
- 曲线斜线填充
- 曲面贴图
- 自定义颜色图
- gif 动态图
- 方程曲线图
- 极坐标图
- 不相交随机圆
- BP 神经网络图
- 小圆构成的圆环
- 棋盘格
二维曲线
二维曲线算是最最常见的一种曲线了,它能反应两个变量的因果关系;
clear;clc;close all;
x = linspace(1, 200, 100);
y1 = log(x) + 1;
y2 = log(x) + 2;
figure;
plot(x, y1);
hold on
plot(x, y2, 'LineWidth', 2);
hold off
legend('y1', 'y2');
调整线的粗细,给人感觉就不一样;
或者说这个颜色可能你觉得不好看,查阅帮助文档里给的的标准颜色也都不喜欢;
没关系,我整理了 28 种个人觉得比较好看的颜色,自行做一下颜色搭配,一般够用了;
【061 Matlab 基础】推荐28种超好看的淡色figure;
plot(x, y1, 'LineWidth', 2, 'Color', [0.21, 0.21, 0.67]);
hold on
plot(x, y2, 'LineWidth', 2, 'Color', [0.99, 0.49, 0.00]);
hold off
legend('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]);
hold off
二维渐变色图
用不同的颜色、数据点大小表征不同数值,更加直观;
更详细的案例分析:
【048 绘图】Matlab 绘制渐变色曲线、填充渐变色% 先自己做一个 colormap
c_map = [0.00, 0.36, 0.67
0.68, 0.42, 0.89
0.44, 0.62, 0.98
0.10, 0.67, 0.59
0.99, 0.57, 0.59
0.28, 0.55, 0.86
0.96, 0.62, 0.24
0.30, 0.90, 0.56
0.12, 0.46, 0.71
0.46, 0.63, 0.90
0.96, 0.37, 0.40
0.14, 0.76, 0.71
0.99, 0.50, 0.02
0.00, 0.57, 0.76
0.35, 0.90, 0.89
0.17, 0.62, 0.47
0.21, 0.21, 0.67
0.99, 0.49, 0.00
0.98, 0.74, 0.44
0.97, 0.60, 0.58
0.18, 0.62, 0.17
0.68, 0.87, 0.53
0.12, 0.46, 0.70
0.65, 0.79, 0.89
0.95, 0.99, 0.69
0.74, 0.92, 0.68
0.37, 0.81, 0.72
0.01, 0.72, 0.77];
scatter(x, y3, x, x, 'filled');
colormap(c_map);
figure;
fill([x, NaN], [y2, NaN], [x, NaN], 'EdgeColor', 'interp', 'LineWidth', 2);
条形图
正常条形图没啥好说的,这里演示绘制带有误差线的柱状图:
figure;
m = 5;
n = 3;
x = 1:m;
y = rand(m, n) + 2;
% 误差限
neg = rand(m, n);
pos = rand(m, n);
% 绘制柱状图
h = bar(x, y);
% 设置三个系列颜色
h(1, 1).FaceColor = [0.00, 0.36, 0.67];
h(1, 2).FaceColor = [0.68, 0.42, 0.89];
h(1, 3).FaceColor = [0.44, 0.62, 0.98];
% 单独设置第二个系列第二个柱子颜色
h(1, 2).FaceColor = 'flat';
h(1, 2).CData(2,:) = [0.21, 0.21, 0.67];
% 获取误差线 x 值
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', 'm', 'LineWidth', 1.5);
hold off
% 绘制图例
legend({'A1', 'A2', 'A3'});
% 设置 x 轴标签
set(gca, 'XTickLabel', {'label1', 'label2', 'label3', 'label4', 'label5'});
填充图
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
% 函数交叉区域填充
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);
hold on
h2 = fimplicit(g2, [0, 10], 'LineWidth', 2);
h3 = fimplicit(g3, [0, 10], 'LineWidth', 2);
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, [0.21, 0.21, 0.67]);
hold off
多 Y 轴图
figure;
load(&#39;accidents.mat&#39;,&#39;hwydata&#39;)
ind = 1:51;
drivers = hwydata(:,5);
yyaxis left
scatter(ind, drivers, &#39;LineWidth&#39;, 2);
title(&#39;Highway Data&#39;);
xlabel(&#39;States&#39;);
ylabel(&#39;Licensed Drivers (thousands)&#39;);
pop = hwydata(:,7);
yyaxis right
scatter(ind, pop, &#39;LineWidth&#39;, 2);
ylabel(&#39;Vehicle Miles Traveled (millions)&#39;);
figure;
x = linspace(0,10);
y1 = 2*sin(3*x);
y2 = sin(3*x).*exp(0.5*x);
%% 三个纵坐标演示,更多纵坐标可以按照此方法类推
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;
% 画线
axes(&#39;position&#39;, [0.1 0.1 0.5 0.5]);
plot(x, y1, &#39;k&#39;, x, newY2, &#39;r&#39;, x, newY3, &#39;ob--&#39;)
ylabel(&#39;line1&#39;);
% 绘制另外两个空的坐标轴
h2 = axes(&#39;position&#39;, [0.65 0.1 0.005 0.5]);
% 重复绘制,曲线颜色用白色,和figure背景色一致,看不出来即可
plot(x, y2, &#39;w&#39;)
% 颜色,位置,曲线标签
set(h2, &#39;ycolor&#39;, &#39;r&#39;, &#39;yaxislocation&#39;, &#39;right&#39;, &#39;xtick&#39;, [])
% 不知道为啥2018B版本边界显示不清楚,所以画一条线
hold on
limX2 = get(h2, &#39;Xlim&#39;);
limY2 = get(h2, &#39;Ylim&#39;);
plot([limX2(2), limX2(2)], limY2, &#39;r&#39;);
hold off
% 取消边框
box off
ylabel(&#39;line2&#39;);
%
h3 = axes(&#39;position&#39;, [0.75 0.1 0.005 0.5]);
plot(x, y3, &#39;w&#39;)
set(h3, &#39;ycolor&#39;, &#39;b&#39;, &#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;b&#39;);
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;);
二维场图
% 直接把 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));
% 需要获取所有流线的属性
figure;
quiver(x, y, u, v);
streamline(x, y, u, v, startx, starty);
带箭头曲线
% 直接把 streamline 函数的帮助文档 demo 拷贝过来
figure;
[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));
% 需要获取所有流线的属性
lines = streamline(x, y, u, v, startx, starty);
% 下面开始画箭头,涉及到坐标的转换
% 获取 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;, &#39;b&#39;);
end
hold off
title(&#39;带箭头的流线图&#39;);
三维曲线图
figure;
t = 0:pi/20:10*pi;
xt = sin(t);
yt = cos(t);
plot3(xt, yt, t, &#39;-o&#39;, &#39;Color&#39;, &#39;b&#39;, &#39;MarkerSize&#39;, 10, &#39;MarkerFaceColor&#39;, &#39;#D9FFFF&#39;);
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 方向每条曲线都是一个值,重复 y 的长度这么多次
xx = x(i)*ones(1, 101);
% z 方向的值,每次取一条
zz = z(i, :);
% plot3 在 xyz 空间绘制曲线,保证 x y z 长度一致即可
plot3(xx, y, zz, &#39;LineWidth&#39;, 2);
hold on
end
hold off
legend(&#39;line1&#39;, &#39;line2&#39;, &#39;line3&#39;, &#39;line4&#39;, &#39;line5&#39;);
% 如果把渐变曲线和三维曲线结合起来
figure;
x = linspace(0, 2*pi, 50);
y = sin(x);
fill3([ones(size(x)), NaN], [x, NaN], [y, NaN], [x NaN], &#39;EdgeColor&#39;, &#39;interp&#39;, &#39;LineWidth&#39;, 2)
hold on
for i = 2 : 5
if mod(i, 2) == 0
y = sin(2*x);
fill3([i * ones(size(x)), NaN], [x, NaN], [y, NaN], [x NaN], &#39;EdgeColor&#39;, &#39;interp&#39;, &#39;LineWidth&#39;, 2)
else
y = sin(10*x);
fill3([i * ones(size(x)), NaN], [x, NaN], [y, NaN], [x NaN], &#39;EdgeColor&#39;, &#39;interp&#39;, &#39;LineWidth&#39;, 2)
end
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 = [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, &#39;filled&#39;);
colormap(c_map);
三维伪彩图
[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);
% 一个坐标轴
figure;
h1 = surf(x, y, z);
freezeColors;
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 = 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
等高线图
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);
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);
等高线填充图
figure;
subplot(2, 2, 1);
[X, Y, Z] = peaks(50);
contourf(X, Y, Z);
subplot(2, 2, 2);
contourf(X, Y, Z,&#39;--&#39;);
% 限定范围
subplot(2, 2, 3);
contourf(X, Y, Z, [2 3], &#39;ShowText&#39;, &#39;on&#39;);
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, &#39;r&#39;);
hold on
surf(X,Y,Z);
xlim([-3, 3]);
ylim([-3, 3.2]);
shading interp
hold off
view(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, &#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);
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);
xlim([-3, 3]);
ylim([-4, 4]);
view(0, 90);
shading interp
条形分布图
clear;clc;
x = linspace(-10, 10, 100);
y = gauss(100, 5) ;
figure;
% 设置边缘颜色和宽度
bar(x, y, &#39;EdgeColor&#39;, &#39;none&#39;, &#39;BarWidth&#39;, 1);
热图
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];
axis off
figure;
for i = 1:50
for j = 1:50
if ~isnan(z(i, j))
x = [i - 1, i, i, i - 1];
y = [j - 1, j - 1, j, j];
c1 = &#39;w&#39;;
c2 = &#39;w&#39;;
if z(i, j) >= 0.1
c1 = &#39;black&#39;;
c2 = [0.65, 0.78, 0.56];
if z(i, j) >= 0.6
c2 = [0.98, 0.46, 0.23];
if z(i, j) >= 0.8
c2 = [0.99, 0.02, 0.02];
end
end
end
patch(x, y, c2, &#39;EdgeColor&#39;, c1);
end
end
end
axis off
分子模型图
clear;clc;
% 球面的坐标信息,为了看起来平滑一点,给到 100
[x, y, z] = sphere(100);
% C 大小
C = 10;
% H 大小
H = 5;
figure;
% 大球
surf(C*x, C*y, C*z, &#39;FaceColor&#39;, &#39;red&#39;, &#39;EdgeColor&#39;, &#39;none&#39;)
hold on
% 四个小球,都偏离一点位置,准确的位置需要计算,这里演示一个大概位置
surf(H*x, H*y, H*z + 10, &#39;FaceColor&#39;, &#39;blue&#39;, &#39;EdgeColor&#39;, &#39;none&#39;);
surf(H*x + 10, H*y, H*z - 3, &#39;FaceColor&#39;, &#39;blue&#39;, &#39;EdgeColor&#39;, &#39;none&#39;);
surf(H*x - 4, H*y - 10, H*z - 3, &#39;FaceColor&#39;, &#39;blue&#39;, &#39;EdgeColor&#39;, &#39;none&#39;);
surf(H*x - 4, H*y + 10, H*z - 3, &#39;FaceColor&#39;, &#39;blue&#39;, &#39;EdgeColor&#39;, &#39;none&#39;);
% 坐标轴设置
axis equal off
% 光源,看起来更有立体感
light
lighting gouraud
分形图
clear;clc;
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);
% 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
% 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);
shading flat
axis tight manual
曲线斜线填充
【058 基础】用 Matlab 绘制带有X轴和Y轴箭头以及斜线填充的曲线图曲面贴图
clear;clc;
figure;
% 读取地图文件
image = imread(&#39;World.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;, [1 1 1]);
t = 0;
while t < 100
t = t + 1;
view([t 10]);
lightangle(handle, t, 0);
pause(0.01);
end
自定义颜色图
clear;clc;
N = 500;
z = peaks(N);
% 选择 ROI 以外的不显示
for i = 1:N
for j = 1:N
if sqrt((i - N/2)*(i - N/2) + (j - N/2)*(j - N/2)) > 240
z(i, j) = nan;
end
end
end
% 自定义颜色 大概就是给特定范围值指定颜色
N = 14 * 5;
crange = linspace(-6, 8, N);
cmap = zeros(N, 3);
for i = 1:N
if crange(i) <= 0
cmap(i, :) = [0, 1, 0];
elseif crange(i) <= 3
cmap(i, :) = [0, 0, 1];
elseif crange(i) <= 6
cmap(i, :) = [1, 1, 0];
else
cmap(i, :) = [1, 0, 0];
end
end
figure;
surf(z)
colormap(cmap);
% 去网格
shading interp
% 视角
view(0, 90);
% 去坐标轴
axis off
gif 动态图
clear; clc;
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)
title([&#39;y = x^&#39;, num2str(idx)])
end
% 获取绘制对象
fig = figure(1);
for idx = 1:len
y = x.^idx;
plot(x, y, &#39;LineWidth&#39;, 3)
title([&#39;y = x^&#39;, num2str(n(idx))])
% drawnow
% pause(0.1);
frame = getframe(fig);
im{idx} = frame2im(frame);
end
% 输出文件名
% filename = &#39;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
方程曲线图
% 二元方程 x^2*cos(y) + x*sinh(y^2) = 0
xy = linspace(-2, 2, 4);
f = @(x, y) x.^2.*cos(y) + x.*sinh(y.^2);
figure
fimplicit(f, [-2, 2]);
【027】函数可视化极坐标图
figure;
theta = 0:0.01:2*pi;
rho = sin(2*theta).*cos(2*theta);
tiledlayout(1, 2);
nexttile;
polarplot(theta, rho, &#39;c&#39;, &#39;LineWidth&#39;, 2);
nexttile;
rMax = 2*max(rho);
% 这个看不到线,因为只有一个重复的点
polarplot([0, 2*pi], [rMax, rMax]);
hold on
polarplot(theta, rho, &#39;m&#39;, &#39;LineWidth&#39;, 2);
hold off
不相交随机圆
clear;clc;close all;
% 限定范围
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]);
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]);
axis equal
end
end
end
end
axis([0 width 0 height])
box on
hold off
BP 神经网络图
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;);
小圆构成的圆环
clear;clc;
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;k&#39;);
hold on
end
hold off
% 限定显示范围
xlim([-3, 3]);
ylim([-3, 3]);
% x y 轴等宽
axis equal
% 紧凑
axis tight
棋盘格
figure;
% 生成数据点
[x, y] = meshgrid(30:39);
x = x(:);
y = y(:);
scatter(x, y, 100, &#39;red&#39;);
hold on
% 随机填充几个点
number = 5;
index_x = randi(9, 1, number) + 30;
index_y = randi(9, 1, number) + 30;
scatter(index_x, index_y, 100, &#39;blue&#39;, &#39;MarkerFaceColor&#39;, &#39;blue&#39;);
hold off
% 显示网格
grid on
% 网格属性
set(gca, &#39;GridAlpha&#39;, 1, &#39;GridColor&#39;, [0 0 0], &#39;GridLineStyle&#39;, &#39;--&#39;);
|
本帖子中包含更多资源
您需要 登录 才可以下载或查看,没有账号?立即注册
×
|