欢迎光临
我们一直在努力

【美赛画图】第二弹!(MATLAB不是万能的,但是没有MATLAB是万万不能的)O奖论文图片复刻——高级绘图matlab代码集锦,让你摆脱画图“一眼MATLAB”的痛苦!

这次是一些看起来不那么聪明的图……

目录

 极坐标直方图

散点+虚线+填充范围+渐变色迷宫形状的曲线

3D热力图切片

3D surf曲面图对应2D切片

左边折线图右边3D色谱

3D surf曲面+散点图+双colorbar


 极坐标直方图

参考:

感觉这种是spss的风格

南丁格尔玫瑰图(Nightingale rose diagram)又名鸡冠花图(Coxcomb Chart)或极坐标区域图(Polar area diagram)

其实matlab也可以画

MATLAB 图库 – MATLAB & Simulink

自制:

代码:

clear; clc; close all;

%% 自定义区
sport_names = {
'Aquatics','Gymnastics','Shooting','Table Tennis','Badminton',…
'Weightlifting','Athletics','Wrestling','Taekwondo','Canoeing',…
'Cycling','Judo','Golf','Archery','Unknown','Breaking',…
'Sport Climbing','Football'
};
percentages = [23.1, 17.4, 16.0, 12.3, 8.7, 10.4, 5.1, 4.3, 2.1, 2.1, 3.5, 0.7, 0.7, 1.4, 1.4, 0.7, 2.1, 2.1];

color_list = [
0.9 0.1 0.1; 0.9 0.4 0.1; 0.9 0.6 0.1; 0.9 0.8 0.1; 0.1 0.4 0.7;
0.9 0.5 0.5; 0.5 0.8 0.5; 0.9 0.9 0.5; 0.8 0.7 0.5; 0.5 0.7 0.9;
0.8 0.5 0.7; 0.8 0.9 0.7; 0.9 0.9 0.7; 0.9 0.7 0.7; 0.6 0.8 0.9;
0.8 0.3 0.6; 0.9 0.7 0.6; 0.9 0.9 0.3;
];
center_color = [1.0 1.0 1.0];
text_color = [0.2 0.2 0.2];
line_color = [0.5 0.5 0.5];

fontSize = 8;
labelFontSize = 13;

%% 统一模板执行区
figure('Position',[150,150,1000,600],'Color','white');
ax = gca;
hold(ax, 'on');
axis equal;

% 计算角度和高度
n_sports = length(sport_names);
angles = linspace(0, 2*pi, n_sports+1);
radius_inner = 0.3;
radius_outer_max = 1.7;
height_scale = 0.7;

% 根据百分比计算高度
max_percentage = max(percentages);
base_height=0.3;
bar_heights =base_height+ percentages / max_percentage * height_scale;

% 绘制环形条形图(圆角条形)
for i = 1:n_sports
current_color = color_list(i,:);
current_height = bar_heights(i);
current_angle_start = angles(i);
current_angle_end = angles(i+1);

if current_height > 0
% 条形内径(起始位置)
radius_start = radius_inner;

% 条形顶部(圆角处理)
theta_bar = linspace(current_angle_start, current_angle_end, 50);

% 创建圆角顶部
theta_mid = (current_angle_start + current_angle_end) / 2;
bar_width = (current_angle_end – current_angle_start) * 0.95; % 留出间隙
bar_start = theta_mid – bar_width/2;
bar_end = theta_mid + bar_width/2;

% 条形侧面(圆角)
n_corner = 10; % 圆角点数

% 左侧圆角
theta_left = linspace(bar_start, bar_start + bar_width*0.2, n_corner);
theta_main = linspace(bar_start + bar_width*0.2, bar_end – bar_width*0.2, 30);
theta_right = linspace(bar_end – bar_width*0.2, bar_end, n_corner);

theta_all = [theta_left, theta_main, theta_right];

% 计算圆角半径变化
r_corner = current_height * 0.5; % 圆角半径

% 创建高度曲线(带圆角)
r_values = zeros(size(theta_all));
for j = 1:length(theta_all)
theta_val = theta_all(j);

if theta_val <= bar_start + bar_width*0.2
% 左侧圆角
t = (theta_val – bar_start) / (bar_width*0.2);
r_values(j) = radius_start + current_height * (1 – sqrt(1 – t^2));
elseif theta_val >= bar_end – bar_width*0.2
% 右侧圆角
t = (bar_end – theta_val) / (bar_width*0.2);
r_values(j) = radius_start + current_height * (1 – sqrt(1 – t^2));
else
% 平直部分
r_values(j) = radius_start + current_height;
end
end

% 绘制条形(只填充到对应高度)
x_bar = r_values .* cos(theta_all);
y_bar = r_values .* sin(theta_all);

patch([x_bar, fliplr(radius_start*cos(theta_all))], …
[y_bar, fliplr(radius_start*sin(theta_all))], …
current_color, 'EdgeColor', 'none', …
'FaceAlpha', 0.8);

% 添加条形轮廓
plot(radius_start*cos(theta_all), radius_start*sin(theta_all), …
'Color', current_color*0.7, 'LineWidth', 0.5);
plot(x_bar, y_bar, 'Color', current_color*0.7, 'LineWidth', 0.5);

% 添加标签指示线
label_radius = radius_outer_max ;
line_start_radius = radius_start + current_height + 0.05;

x_line_start = line_start_radius * cos(theta_mid);
y_line_start = line_start_radius * sin(theta_mid);
x_line_end = label_radius * cos(theta_mid);
y_line_end = label_radius * sin(theta_mid);

plot([x_line_start, x_line_end], [y_line_start, y_line_end], …
'Color', line_color, 'LineWidth', 0.5, 'LineStyle', '–');

% 添加标签
label = sprintf('%s\\n%.1f%%', sport_names{i}, percentages(i));
text(x_line_end, y_line_end, label, 'FontName', 'Times New Roman', …
'FontSize', fontSize, 'Color', text_color, …
'HorizontalAlignment', 'center', 'VerticalAlignment', 'middle');
end
end

% 绘制中心空白圆
theta_center = linspace(0, 2*pi, 100);
x_center = radius_inner * cos(theta_center);
y_center = radius_inner * sin(theta_center);
patch(x_center, y_center, center_color, 'EdgeColor', 'none');

% 添加CHN标识
text(-1.8, -1.8, 'CHN', 'FontName', 'Times New Roman', …
'FontSize', 28, 'FontWeight', 'bold', 'Color', text_color);

% 添加标题
text(0, -2.0, 'Olympic Medal Distribution by Sport', …
'FontName', 'Times New Roman', 'FontSize', 14, 'FontWeight', 'bold', …
'Color', text_color, 'HorizontalAlignment', 'center');

% 坐标轴设置
ax.XLim = [-2.2, 2.2];
ax.YLim = [-2.2, 2.2];
ax.XTick = [];
ax.YTick = [];
ax.Box = 'off';
ax.Color = 'white';

% 添加背景辅助线
theta_grid = linspace(0, 2*pi, 100);
x_grid_outer = radius_outer_max * cos(theta_grid);
y_grid_outer = radius_outer_max * sin(theta_grid);
plot(x_grid_outer, y_grid_outer, 'Color', [0.8 0.8 0.8], 'LineWidth', 0.5, 'LineStyle', ':');

x_grid_inner = radius_inner * cos(theta_grid);
y_grid_inner = radius_inner * sin(theta_grid);
plot(x_grid_inner, y_grid_inner, 'Color', [0.8 0.8 0.8], 'LineWidth', 0.5, 'LineStyle', ':');

hold(ax, 'off');

fprintf('绘图完成\\n');

参考:

自制:

尽力了,就只能这种程度了

代码:

clear; clc; close all;

%% 绘图类型:错位式南丁格尔玫瑰图
%% ===================== 自定义区(可修改参数)=====================
% 1. 数据参数
indicators = {
'Outdoor air pollution (mg/m³)', 'Population (billion)', 'GDP growth (%)', …
'Carbon emissions (bn tonnes)', 'Exports ($tn)', 'Literacy rate (%)', …
'Olympic gold medals', 'Unemployment rate (%)', 'Military expenditure (%)', …
'Market capitalisation (%)', 'Enterprise value ($bn)', 'Film industry ($bn)'
};
n_indicators = length(indicators);

china_values = [98, 1.35, 7.8, 2.1, 2.05, 99, 38, 6.4, 2.6, 46.3, 55.4, 2.7];
us_values = [12, 0.33, 2.2, 1.4, 1.61, 92, 46, 8.2, 4.6, 104.3, 185.1, 10.8];

% 2. 可视化参数
radius_inner = 5; % 中心空白圆半径
radius_outer_max = 10; % 最大半径
height_scale = 16; % 高度缩放系数
angle_offset = 8; % 角度偏移量(度)
bar_width = 20; % 单条宽度(度)

% 3. 颜色参数
color_china = [0.9 0.1 0.1]; % 中国红色
color_us = [0.1 0.3 0.8]; % 美国蓝色
color_china_text = [0.7 0.0 0.0]; % 中国文字红色
color_us_text = [0.0 0.2 0.6]; % 美国文字蓝色
center_color = [1.0 1.0 1.0]; % 中心圆颜色
bg_color = [1.0 1.0 1.0]; % 背景白色

% 4. 文字参数
indicator_fontsize = 8; % 指标名称字号
value_fontsize = 7; % 数值标签字号
legend_fontsize = 9; % 图例字号

%% ===================== 统一模板执行区 =====================
figure('Position',[150,150,1000,1000],'Color',bg_color);
ax = gca;
hold(ax, 'on');
axis equal;

% 计算角度和高度
angles = linspace(0, 2*pi, n_indicators+1);

% 根据数值计算高度
max_combined = max([china_values, us_values]);
china_heights = radius_inner + china_values / max_combined * height_scale;
us_heights = radius_inner + us_values / max_combined * height_scale;

% 转换为弧度
angle_offset_rad = deg2rad(angle_offset);
bar_width_rad = deg2rad(bar_width);

% 绘制环形条形图(错位显示)
for i = 1:n_indicators
current_angle_start = angles(i);
current_angle_end = angles(i+1);
angle_mid = (current_angle_start + current_angle_end) / 2;

% 美国条形:占据前半段(下方)
us_start = angle_mid – bar_width_rad/2;
us_end = us_start + bar_width_rad;
theta_us = linspace(us_start, us_end, 50);

% 中国条形:占据后半段(上方)
china_start = angle_mid – bar_width_rad/2 + angle_offset_rad;
china_end = china_start + bar_width_rad;
theta_china = linspace(china_start, china_end, 50);

% 绘制美国条形(底层)
r_us_bottom = radius_inner * ones(size(theta_us));
r_us_top = us_heights(i) * ones(size(theta_us));
x_us = [r_us_bottom .* cos(theta_us), fliplr(r_us_top .* cos(theta_us))];
y_us = [r_us_bottom .* sin(theta_us), fliplr(r_us_top .* sin(theta_us))];

patch(x_us, y_us, color_us, 'EdgeColor', 'none', 'FaceAlpha', 1.0);

% 绘制中国条形(上层)
r_china_bottom = radius_inner * ones(size(theta_china));
r_china_top = china_heights(i) * ones(size(theta_china));
x_china = [r_china_bottom .* cos(theta_china), fliplr(r_china_top .* cos(theta_china))];
y_china = [r_china_bottom .* sin(theta_china), fliplr(r_china_top .* sin(theta_china))];

patch(x_china, y_china, color_china, 'EdgeColor', 'none', 'FaceAlpha', 1.0);

% 添加指标名称标签
text_angle = angle_mid;
text_radius = max(china_heights(i), us_heights(i)) + 1.5;

[x_text, y_text] = pol2cart(text_angle, text_radius);
text(x_text, y_text, indicators{i}, …
'FontName','Times New Roman', 'FontSize',indicator_fontsize, …
'Color',[0.2 0.2 0.2], …
'HorizontalAlignment','center', 'VerticalAlignment','middle');

% 添加数值标签
value_radius = max(china_heights(i), us_heights(i)) + 0.8;

% 中国数值(红色,在条形上方)
[x_china_val, y_china_val] = pol2cart(china_start + bar_width_rad/2, value_radius);
text(x_china_val, y_china_val, sprintf('CN: %.1f', china_values(i)), …
'FontName','Times New Roman', 'FontSize',value_fontsize, …
'Color',color_china_text, 'FontWeight','bold',…
'HorizontalAlignment','center', 'VerticalAlignment','bottom');

% 美国数值(蓝色,在条形上方)
[x_us_val, y_us_val] = pol2cart(us_start + bar_width_rad/2, value_radius – 0.3);
text(x_us_val, y_us_val, sprintf('US: %.1f', us_values(i)), …
'FontName','Times New Roman', 'FontSize',value_fontsize, …
'Color',color_us_text, 'FontWeight','bold',…
'HorizontalAlignment','center', 'VerticalAlignment','top');
end

% 绘制中心空白圆
theta_center = linspace(0, 2*pi, 100);
x_center = radius_inner * cos(theta_center);
y_center = radius_inner * sin(theta_center);
patch(x_center, y_center, center_color, 'EdgeColor', 'none');

% 创建图例
h_china = patch([0,0,0,0], [0,0,0,0], color_china, 'EdgeColor','none', 'DisplayName','China');
h_us = patch([0,0,0,0], [0,0,0,0], color_us, 'EdgeColor','none', 'DisplayName','USA');
legend([h_china, h_us], 'Location','northwest', 'FontName','Times New Roman',…
'FontSize',legend_fontsize, 'Box','on');

% 坐标轴设置
max_display_radius = max([china_heights, us_heights]) + 2;
ax.XLim = [-max_display_radius, max_display_radius];
ax.YLim = [-max_display_radius, max_display_radius];
ax.XTick = [];
ax.YTick = [];
ax.Box = 'off';
ax.Color = 'white';

% 添加背景辅助线
theta_grid = linspace(0, 2*pi, 100);
x_grid_inner = radius_inner * cos(theta_grid);
y_grid_inner = radius_inner * sin(theta_grid);
plot(x_grid_inner, y_grid_inner, 'Color', [0.8 0.8 0.8], 'LineWidth', 0.5, 'LineStyle', ':');

hold(ax, 'off');

fprintf('绘图完成\\n');

参考:

自制:

代码:

clear; clc; close all;

%% 自定义区
% 数据
half_data = [35, 28, 18, 12, 7];
quarter_data = [42, 30, 18, 10];
half_labels = {'Category A', 'Category B', 'Category C', 'Category D', 'Category E'};
quarter_labels = {'Type 1', 'Type 2', 'Type 3', 'Type 4'};

% 颜色参数
color_start = [0.9 0.6 0.7];
color_end = [0.3 0.8 0.5];
bg_color = [1 1 1];
grid_color = [0.85 0.85 0.85];
text_color = [0.2 0.2 0.2];

% 显示参数
show_grid = true;
show_labels = true;
show_values = true;
show_legend = true;
show_percentage = true;

% 尺寸参数
fontSize = 10;
labelFontSize = 13;
radius_inner = 0.2;
radius_outer_max = 2.0;

%% 统一模板执行区
figure('Position', [150, 150, 1100, 850], 'Color', bg_color);

% 上半圆极坐标直方图
subplot(2, 1, 1);
ax_half = gca;
hold(ax_half, 'on');
axis equal;

n_half = length(half_data);
angles_half = linspace(0, pi, n_half+1);
max_half = max(half_data);
height_scale = 1.2;
bar_heights_half = radius_inner + (half_data/max_half)*height_scale;
half_percent = 100 * half_data / sum(half_data);

half_colors = zeros(n_half, 3);
for i = 1:n_half
ratio = (i-1)/(n_half-1);
half_colors(i,:) = color_start + ratio*(color_end – color_start);
end

half_patches = gobjects(n_half, 1);
for i = 1:n_half
current_color = half_colors(i,:);
current_angle_start = angles_half(i);
current_angle_end = angles_half(i+1);
current_height = bar_heights_half(i);

theta_mid = (current_angle_start + current_angle_end)/2;
bar_width = (current_angle_end – current_angle_start)*0.85;
bar_start = theta_mid – bar_width/2;
bar_end = theta_mid + bar_width/2;

theta_left = linspace(bar_start, bar_start+bar_width*0.15, 8);
theta_main = linspace(bar_start+bar_width*0.15, bar_end-bar_width*0.15, 25);
theta_right = linspace(bar_end-bar_width*0.15, bar_end, 8);
theta_all = [theta_left, theta_main, theta_right];

r_values = zeros(size(theta_all));
for j = 1:length(theta_all)
theta_val = theta_all(j);
if theta_val <= bar_start + bar_width*0.15
t = (theta_val – bar_start)/(bar_width*0.15);
r_values(j) = radius_inner + current_height*(1 – sqrt(1 – t^2));
elseif theta_val >= bar_end – bar_width*0.15
t = (bar_end – theta_val)/(bar_width*0.15);
r_values(j) = radius_inner + current_height*(1 – sqrt(1 – t^2));
else
r_values(j) = radius_inner + current_height;
end
end

x_bar = r_values .* cos(theta_all);
y_bar = r_values .* sin(theta_all);

half_patches(i) = patch([x_bar, fliplr(radius_inner*cos(theta_all))], …
[y_bar, fliplr(radius_inner*sin(theta_all))], …
current_color, 'EdgeColor', 'none', 'FaceAlpha', 0.9);

if show_labels
label_radius = radius_outer_max + 0.1;
x_text = label_radius * cos(theta_mid);
y_text = label_radius * sin(theta_mid);

if show_percentage && show_values
text_str = [half_labels{i}, ' ', num2str(half_data(i)), ' (', num2str(half_percent(i), '%.1f'), '%)'];
elseif show_percentage
text_str = [half_labels{i}, ' ', num2str(half_percent(i), '%.1f'), '%'];
elseif show_values
text_str = [half_labels{i}, ' ', num2str(half_data(i))];
else
text_str = half_labels{i};
end

text(x_text, y_text, text_str, …
'FontName', 'Times New Roman', 'FontSize', fontSize, 'Color', text_color, …
'HorizontalAlignment', 'center', 'VerticalAlignment', 'middle');
end
end

theta_center = linspace(0, pi, 100);
x_center = radius_inner * cos(theta_center);
y_center = radius_inner * sin(theta_center);
patch(x_center, y_center, bg_color, 'EdgeColor', 'none');

if show_grid
theta_grid = linspace(0, pi, 100);
for r = 0.5:0.5:radius_outer_max
x_grid = r * cos(theta_grid);
y_grid = r * sin(theta_grid);
plot(x_grid, y_grid, 'Color', grid_color, 'LineWidth', 0.5, 'HandleVisibility', 'off');
end
end

ax_half.XLim = [-2.5, 2.5];
ax_half.YLim = [-0.2, 2.5];
ax_half.XTick = [];
ax_half.YTick = [];
ax_half.Box = 'off';
ax_half.Color = bg_color;
ax_half.XAxis.Visible = 'off';
ax_half.YAxis.Visible = 'off';

% 创建图例项
legend_items = [];
legend_labels = {};
for i = 1:n_half
if i == 1
legend_items(end+1) = patch([0 0 0 0], [0 0 0 0], half_colors(i,:), …
'EdgeColor', 'none', 'FaceAlpha', 0.9, 'DisplayName', half_labels{i});
else
legend_items(end+1) = patch([0 0 0 0], [0 0 0 0], half_colors(i,:), …
'EdgeColor', 'none', 'FaceAlpha', 0.9, 'DisplayName', half_labels{i});
end
legend_labels{end+1} = half_labels{i};
end

if show_legend
legend(ax_half, legend_items, legend_labels, 'Location', 'best', …
'FontName', 'Times New Roman', 'FontSize', fontSize);
end

hold(ax_half, 'off');

% 四分之一圆极坐标直方图
subplot(2, 1, 2);
ax_quarter = gca;
hold(ax_quarter, 'on');
axis equal;

n_quarter = length(quarter_data);
angles_quarter = linspace(pi/2, pi, n_quarter+1);
max_quarter = max(quarter_data);
bar_heights_quarter = radius_inner + (quarter_data/max_quarter)*height_scale;
quarter_percent = 100 * quarter_data / sum(quarter_data);

quarter_colors = zeros(n_quarter, 3);
for i = 1:n_quarter
ratio = (i-1)/(n_quarter-1);
quarter_colors(i,:) = color_start + ratio*(color_end – color_start);
end

quarter_patches = gobjects(n_quarter, 1);
for i = 1:n_quarter
current_color = quarter_colors(i,:);
current_angle_start = angles_quarter(i);
current_angle_end = angles_quarter(i+1);
current_height = bar_heights_quarter(i);

theta_mid = (current_angle_start + current_angle_end)/2;
bar_width = (current_angle_end – current_angle_start)*0.85;
bar_start = theta_mid – bar_width/2;
bar_end = theta_mid + bar_width/2;

theta_left = linspace(bar_start, bar_start+bar_width*0.15, 8);
theta_main = linspace(bar_start+bar_width*0.15, bar_end-bar_width*0.15, 25);
theta_right = linspace(bar_end-bar_width*0.15, bar_end, 8);
theta_all = [theta_left, theta_main, theta_right];

r_values = zeros(size(theta_all));
for j = 1:length(theta_all)
theta_val = theta_all(j);
if theta_val <= bar_start + bar_width*0.15
t = (theta_val – bar_start)/(bar_width*0.15);
r_values(j) = radius_inner + current_height*(1 – sqrt(1 – t^2));
elseif theta_val >= bar_end – bar_width*0.15
t = (bar_end – theta_val)/(bar_width*0.15);
r_values(j) = radius_inner + current_height*(1 – sqrt(1 – t^2));
else
r_values(j) = radius_inner + current_height;
end
end

x_bar = r_values .* cos(theta_all);
y_bar = r_values .* sin(theta_all);

quarter_patches(i) = patch([x_bar, fliplr(radius_inner*cos(theta_all))], …
[y_bar, fliplr(radius_inner*sin(theta_all))], …
current_color, 'EdgeColor', 'none', 'FaceAlpha', 0.9);

if show_labels
label_radius = radius_outer_max + 0.1;
x_text = label_radius * cos(theta_mid);
y_text = label_radius * sin(theta_mid);

if show_percentage && show_values
text_str = [quarter_labels{i}, ' ', num2str(quarter_data(i)), ' (', num2str(quarter_percent(i), '%.1f'), '%)'];
elseif show_percentage
text_str = [quarter_labels{i}, ' ', num2str(quarter_percent(i), '%.1f'), '%'];
elseif show_values
text_str = [quarter_labels{i}, ' ', num2str(quarter_data(i))];
else
text_str = quarter_labels{i};
end

text(x_text, y_text, text_str, …
'FontName', 'Times New Roman', 'FontSize', fontSize, 'Color', text_color, …
'HorizontalAlignment', 'center', 'VerticalAlignment', 'middle');
end
end

theta_center = linspace(pi/2, pi, 100);
x_center = radius_inner * cos(theta_center);
y_center = radius_inner * sin(theta_center);
patch(x_center, y_center, bg_color, 'EdgeColor', 'none');

if show_grid
theta_grid = linspace(pi/2, pi, 100);
for r = 0.5:0.5:radius_outer_max
x_grid = r * cos(theta_grid);
y_grid = r * sin(theta_grid);
plot(x_grid, y_grid, 'Color', grid_color, 'LineWidth', 0.5, 'HandleVisibility', 'off');
end
end

ax_quarter.XLim = [-2.5, 0.2];
ax_quarter.YLim = [-0.2, 2.5];
ax_quarter.XTick = [];
ax_quarter.YTick = [];
ax_quarter.Box = 'off';
ax_quarter.Color = bg_color;
ax_quarter.XAxis.Visible = 'off';
ax_quarter.YAxis.Visible = 'off';

% 创建图例项
legend_items2 = [];
legend_labels2 = {};
for i = 1:n_quarter
legend_items2(end+1) = patch([0 0 0 0], [0 0 0 0], quarter_colors(i,:), …
'EdgeColor', 'none', 'FaceAlpha', 0.9, 'DisplayName', quarter_labels{i});
legend_labels2{end+1} = quarter_labels{i};
end

if show_legend
legend(ax_quarter, legend_items2, legend_labels2, 'Location', 'best', …
'FontName', 'Times New Roman', 'FontSize', fontSize);
end

hold(ax_quarter, 'off');

display_statistics(half_data, quarter_data);
fprintf('极坐标直方图绘制完成\\n');

%% 统计函数
function display_statistics(half_data, quarter_data)
total_half = sum(half_data);
total_quarter = sum(quarter_data);
avg_half = mean(half_data);
avg_quarter = mean(quarter_data);

fprintf('上半圆数据统计: 总数=%d, 平均值=%.1f\\n', total_half, avg_half);
fprintf('四分之一圆数据统计: 总数=%d, 平均值=%.1f\\n', total_quarter, avg_quarter);
end

散点+虚线+填充范围+渐变色迷宫形状的曲线

自制:(数据不好模拟,凑合看)

clear; clc; close all;
%% 绘图类型:高密度散点+幂指数拟合虚线+半透明红色置信区间+四方向多色连续渐变迷宫曲线(全彩色图例)
%% ===================== 自定义区(可修改参数)=====================
% 1. 数据参数
% 基础自变量(1000个散点)
x_raw = linspace(1, 20, 1000);
% 幂指数函数:y = a*x^b + 噪声
a = 2.5;
b = 0.8;
y_true = a * (x_raw.^b);
y_noise = y_true + randn(size(x_raw))*4;
% 拟合用高密度x
x_fit = linspace(1, 20, 200);

% 置信区间(上下限)
confidence_upper = a * (x_fit.^b) + 2.5;
confidence_lower = a * (x_fit.^b) – 2.5;

% 四方向迷宫曲线坐标(仅上下左右移动)
maze_x = [1,1,1,1,1,2,3,4,4,4,4,5,6,7,7,7,7,8,9,10,10,10,10,11,12,13,13,13,13,14,15,16,16,16,16,17,18,19,19,19];
maze_y = [2,4,6,8,10,10,10,10,8,6,4,4,4,4,6,8,10,10,10,10,8,6,4,4,4,4,6,8,10,10,10,10,8,6,4,4,4,4,6,8];
maze_len = length(maze_x);

% 2. 颜色参数
color_scatter = [0.7 0.7 0.7]; % 散点颜色(浅灰)
color_fit = [0.1 0.3 0.8]; % 拟合曲线颜色(蓝色)
color_confidence = [0.9 0.2 0.2]; % 置信区间颜色(红色)
confidence_alpha = 0.3; % 置信区间透明度(0-1)
% 迷宫曲线渐变关键色(4个节点)
maze_key_colors = [
0.1 0.1 0.1; % 黑色
0.8 0.2 0.2; % 红色
0.9 0.5 0.1; % 橙色
1.0 0.9 0.1; % 黄色
];

% 3. 文字参数
xlabel_txt = 'X Value';
ylabel_txt = 'Y Value';
% 完整图例文字
legend_txt = {
'Simulated Data', % 散点
'Power Exponential Fit', % 拟合曲线
'95% Confidence Interval', % 置信区间
'Path Trajectory' % 迷宫曲线
};
fontSize = 11;
labelFontSize = 13;

%% ===================== 统一模板执行区 =====================
figure('Position',[150,150,1200,700],'Color','white');
ax = gca;
hold(ax, 'on');

% 1. 绘制高密度散点(设置displayname,彩色图例)
h_scatter = scatter(x_raw, y_noise, 20, color_scatter, 'filled', 'Marker','o', 'DisplayName',legend_txt{1});

% 2. 绘制半透明置信区间填充
x_fill = [x_fit, fliplr(x_fit)];
y_fill = [confidence_upper, fliplr(confidence_lower)];
patch(x_fill, y_fill, color_confidence, 'EdgeColor','none', 'FaceAlpha',confidence_alpha);

% 3. 绘制幂指数拟合虚线(设置displayname,彩色图例)
y_fit_curve = a * (x_fit.^b);
h_fit = plot(x_fit, y_fit_curve, 'Color',color_fit, 'LineStyle','–', 'LineWidth',2, 'DisplayName',legend_txt{2});

% 4. 生成4色连续渐变颜色矩阵
maze_gradient_colors = zeros(maze_len-1, 3);
key_points = linspace(0, 1, size(maze_key_colors,1));
for i = 1:maze_len-1
ratio = (i-1)/(maze_len-2);
for k = 1:size(maze_key_colors,1)-1
if ratio >= key_points(k) && ratio <= key_points(k+1)
seg_ratio = (ratio – key_points(k))/(key_points(k+1) – key_points(k));
maze_gradient_colors(i,1) = maze_key_colors(k,1) + seg_ratio*(maze_key_colors(k+1,1)-maze_key_colors(k,1));
maze_gradient_colors(i,2) = maze_key_colors(k,2) + seg_ratio*(maze_key_colors(k+1,2)-maze_key_colors(k,2));
maze_gradient_colors(i,3) = maze_key_colors(k,3) + seg_ratio*(maze_key_colors(k+1,3)-maze_key_colors(k,3));
break;
end
end
end

% 5. 绘制四方向多色渐变迷宫曲线(第一条设displayname,彩色图例)
h_maze = plot(maze_x(1:2), maze_y(1:2), 'Color',maze_gradient_colors(1,:), 'LineWidth',3, 'DisplayName',legend_txt{4});
for i = 2:maze_len-1
plot(maze_x(i:i+1), maze_y(i:i+1), 'Color',maze_gradient_colors(i,:), 'LineWidth',3, 'HandleVisibility','off');
end

% 6. 坐标轴设置
ax.XLim = [0, 20];
ax.YLim = [-5, 30];
ax.FontName = 'Times New Roman';
ax.FontSize = fontSize;
ax.XLabel.String = xlabel_txt;
ax.XLabel.FontWeight = 'bold';
ax.XLabel.FontSize = labelFontSize;
ax.XLabel.FontName = 'Times New Roman';
ax.YLabel.String = ylabel_txt;
ax.YLabel.FontWeight = 'bold';
ax.YLabel.FontSize = labelFontSize;
ax.YLabel.FontName = 'Times New Roman';
ax.XGrid = 'on';
ax.YGrid = 'on';
ax.GridColor = [0.9 0.9 0.9];
ax.TickDir = 'in';
ax.Box = 'on';

% 7. 图例设置(全彩色,包含所有要素)
% 创建置信区间代理图例句柄(红色半透明,彩色图例)
h_conf = patch([0,0,0,0], [0,0,0,0], color_confidence, 'EdgeColor','none', 'FaceAlpha',confidence_alpha, 'DisplayName',legend_txt{3});
% 整合所有图例句柄,展示彩色图例
legend_handles = [h_scatter, h_fit, h_conf, h_maze];
legend(legend_handles, legend_txt, 'Location','northeast','FontSize',10,'FontName','Times New Roman');

hold(ax, 'off');

% 结果输出
fprintf('绘图完成:全彩色图例版-散点+幂指数拟合+半透明置信区间+四色渐变迷宫曲线\\n');

3D热力图切片

参考:

自制:

代码

clear; clc; close all;
%% 绘图类型:多时间层2D热力图堆叠的时空演化图(优化Y轴标签方向+细密色阶+Colorbar)
%% ===================== 自定义区(可修改参数)=====================
% 1. 数据参数
grid_size = 70; % 网格尺寸
time_labels = {'t=1h','t=3h','t=5h','t=7h','t=9h','t=10h'};
n_layers = length(time_labels);
% 模拟热点移动轨迹
hotspot_x = linspace(10, 30, n_layers);
hotspot_y = linspace(10, 30, n_layers);
hotspot_sigma = 5; % 热点扩散范围

% 2. 颜色参数(更细密的色阶:20级渐变)
colormap_hot = [
0.0 0.0 0.0; 0.1 0.0 0.0; 0.2 0.0 0.0; 0.3 0.0 0.0; 0.4 0.0 0.0;
0.5 0.0 0.0; 0.6 0.0 0.0; 0.7 0.0 0.0; 0.8 0.0 0.0; 0.9 0.0 0.0;
1.0 0.1 0.0; 1.0 0.2 0.0; 1.0 0.3 0.0; 1.0 0.4 0.0; 1.0 0.5 0.0;
1.0 0.6 0.0; 1.0 0.7 0.0; 1.0 0.8 0.0; 1.0 0.9 0.0; 1.0 1.0 0.0;
];
background_color = [0 0 0];
text_color = [1 1 1];
colorbar_label = 'Intensity'; % 色标标签

% 3. 文字参数
xlabel_txt = 'Coordinates (Grid) in the x-direction';
ylabel_txt = 'Coordinates (Grid) in the y-direction';
zlabel_txt = 'Time';
fontSize = 11;
labelFontSize = 13;

%% ===================== 统一模板执行区 =====================
figure('Position',[150,150,900,700],'Color','white');
ax = axes('Position',[0.1,0.1,0.75,0.8],'Color','none');
hold(ax, 'on');
view(3);
axis equal;

% 生成网格坐标
[X,Y] = meshgrid(1:grid_size, 1:grid_size);

% 逐层绘制热力图
for layer = 1:n_layers
z = layer * 10; % 层间距
% 生成高斯热点数据
heat_data = exp(-((X – hotspot_x(layer)).^2 + (Y – hotspot_y(layer)).^2) / (2*hotspot_sigma^2));
% 绘制热图平面(双线性插值优化显示细腻度)
surf(X, Y, z*ones(size(X)), heat_data, 'EdgeColor','none', 'FaceColor','interp', 'FaceLighting','phong');
% 绘制层边框
plot3([1 grid_size grid_size 1 1], [1 1 grid_size grid_size 1], z*ones(1,5), 'Color',[0.3 0.3 0.3], 'LineWidth',1);
% 添加时间标签
text(0, 0, z, time_labels{layer}, 'FontName','Times New Roman', 'FontSize',fontSize, 'Color',text_color, 'HorizontalAlignment','right');
end

% 设置颜色映射(20级细密色阶)
colormap(colormap_hot);
caxis([0 1]);

% 添加Colorbar并设置样式(修复属性错误)
cb = colorbar(ax, 'Position',[0.88,0.1,0.03,0.8]);
cb.Label.String = colorbar_label;
cb.Label.FontName = 'Times New Roman';
cb.Label.FontSize = labelFontSize;
cb.Label.FontWeight = 'bold';
% 修正:设置Colorbar刻度字体(通过ax的FontName间接控制)
cb.FontName = 'Times New Roman';
cb.FontSize = fontSize;

% 坐标轴设置
ax.XLim = [0 grid_size+10];
ax.YLim = [0 grid_size+10];
ax.ZLim = [0 (n_layers+1)*10];
% X轴标签
ax.XLabel.String = xlabel_txt;
ax.XLabel.FontWeight = 'bold';
ax.XLabel.FontSize = labelFontSize;
ax.XLabel.FontName = 'Times New Roman';
% Y轴标签(沿Y轴方向显示)
ax.YLabel.String = ylabel_txt;
ax.YLabel.FontWeight = 'bold';
ax.YLabel.FontSize = labelFontSize;
ax.YLabel.FontName = 'Times New Roman';
ax.YLabel.VerticalAlignment = 'middle';
ax.YLabel.HorizontalAlignment = 'right';
% Z轴标签
ax.ZLabel.String = zlabel_txt;
ax.ZLabel.FontWeight = 'bold';
ax.ZLabel.FontSize = labelFontSize;
ax.ZLabel.FontName = 'Times New Roman';

ax.TickDir = 'out';
ax.Box = 'on';
ax.GridColor = [0.2 0.2 0.2];
ax.XGrid = 'on';
ax.YGrid = 'on';
ax.ZGrid = 'on';
ax.FontName = 'Times New Roman'; % 统一坐标轴字体
ax.FontSize = fontSize;

hold(ax, 'off');

% 结果输出
fprintf('绘图完成:时空演化热力图堆叠图(优化Y轴标签+细密色阶+Colorbar)\\n');

3D(随高度变化)的散点图

参考:

自制:

代码:

clear; clc; close all;

%% 绘图类型:3D散点概率分布图(颜色映射深度值)
%% ===================== 自定义区(可修改参数)=====================
% 1. 数据参数
n_points = 20000; % 散点数量
% 模拟坐标与深度
lat = 4.072e6 + randn(n_points,1)*2500;
lon = 2.045e6 + randn(n_points,1)*15000;
depth = linspace(0, -4000, n_points)' + randn(n_points,1)*200;

% 2. 颜色参数(优化后的色阶 – 从浅蓝到深蓝的科学色系)
% 创建更平滑的色阶(使用parula风格的改进版)
N = 256; % 增加颜色分辨率
colormap_depth = zeros(N, 3);

% 创建更美观的颜色映射:浅黄->橙色->红色->紫色->深蓝
for i = 1:N
t = (i-1)/(N-1);

% 多段式颜色映射
if t < 0.25
% 浅黄到橙色
r = 1.0;
g = 1.0 – t*1.2;
b = 0.0;
elseif t < 0.5
% 橙色到红色
r = 1.0;
g = 0.7 – (t-0.25)*2.8;
b = 0.0;
elseif t < 0.75
% 红色到紫色
r = 1.0;
g = 0.0;
b = (t-0.5)*4;
else
% 紫色到深蓝
r = 1.0 – (t-0.75)*4;
g = 0.0;
b = 1.0;
end

colormap_depth(i,:) = [r, g, b];
end

% 或者使用MATLAB内置的高级色阶
% colormap_depth = turbo(256); % 高对比度色阶
% colormap_depth = viridis(256); % 科学论文常用
% colormap_depth = plasma(256); % 明亮色阶
% colormap_depth = inferno(256); % 暗色调色阶

colorbar_label = 'Depth (m)';
text_color = [0.95 0.95 0.95]; % 改为浅色文字

% 3. 文字参数
title_txt = 'Probability Distribution Map of Submersible';
xlabel_txt = 'Distance in Latitude (m)';
ylabel_txt = 'Distance in Longitude (m)';
zlabel_txt = 'Depth (m)';
fontSize = 12;
labelFontSize = 14;

%% ===================== 统一模板执行区 =====================
% 创建带有渐变背景的图形
figure('Position',[100,100,1000,750],…
'Color','k',…
'Name','3D Probability Distribution');

% 创建带有渐变背景的坐标系
ax = axes('Color','none',…
'Position',[0.08,0.1,0.75,0.8]);
hold(ax, 'on');

% 设置3D视角
view(ax, 45, 30);
axis(ax, 'equal');

% 绘制3D散点图 – 增加点的大小和透明度
scatter3(lat, lon, depth, 8, depth,…
'filled',…
'Marker','o',…
'MarkerFaceAlpha',0.6,…
'MarkerEdgeAlpha',0.3,…
'MarkerEdgeColor',[0.3 0.3 0.3]);

% 设置颜色映射
colormap(ax, colormap_depth);
caxis(ax, [-4000 0]);

% 添加高级Colorbar
cb = colorbar(ax,…
'Location','eastoutside',…
'Position',[0.85,0.15,0.02,0.7]);
cb.Label.String = colorbar_label;
cb.Label.FontName = 'Arial';
cb.Label.FontSize = labelFontSize;
cb.Label.FontWeight = 'bold';
cb.Label.Color = text_color;
cb.Color = text_color;
cb.FontName = 'Arial';
cb.FontSize = fontSize;
cb.TickDirection = 'out';

% 坐标轴设置
ax.XLim = [4.072e6, 4.084e6];
ax.YLim = [2.045e6, 2.07e6];
ax.ZLim = [-4000, 0];

% 坐标轴标签
ax.XLabel.String = xlabel_txt;
ax.YLabel.String = ylabel_txt;
ax.ZLabel.String = zlabel_txt;
ax.XLabel.FontWeight = 'bold';
ax.YLabel.FontWeight = 'bold';
ax.ZLabel.FontWeight = 'bold';
ax.XLabel.FontSize = labelFontSize;
ax.YLabel.FontSize = labelFontSize;
ax.ZLabel.FontSize = labelFontSize;
ax.XLabel.FontName = 'Arial';
ax.YLabel.FontName = 'Arial';
ax.ZLabel.FontName = 'Arial';
ax.XLabel.Color = text_color;
ax.YLabel.Color = text_color;
ax.ZLabel.Color = text_color;

% 坐标轴样式
ax.Color = [0.05 0.05 0.1]; % 深蓝黑色背景
ax.GridColor = [0.3 0.3 0.5];
ax.GridAlpha = 0.3;
ax.XGrid = 'on';
ax.YGrid = 'on';
ax.ZGrid = 'on';
ax.MinorGridColor = [0.2 0.2 0.3];
ax.MinorGridAlpha = 0.1;

% 轴线颜色
ax.XColor = [0.6 0.6 0.8];
ax.YColor = [0.6 0.6 0.8];
ax.ZColor = [0.6 0.6 0.8];

ax.TickDir = 'out';
ax.Box = 'on';
ax.BoxStyle = 'full';
ax.LineWidth = 1.5;
ax.FontName = 'Arial';
ax.FontSize = fontSize;
ax.FontWeight = 'bold';
ax.Color = 'none'; % 透明背景以显示图形背景

% 添加标题
title(ax, title_txt,…
'FontName','Arial',…
'FontSize',labelFontSize+2,…
'FontWeight','bold',…
'Color',text_color);

% 添加图标背景(在图形上添加水印或图标)
annotation('rectangle', [0.7 0.02 0.25 0.25],…
'FaceColor', [0.1 0.1 0.2],…
'FaceAlpha', 0.3,…
'EdgeColor', 'none');

% 添加图标文字
annotation('textbox', [0.72 0.05 0.2 0.15],…
'String', {'Deep Sea','Exploration','Data Analysis'},…
'Color', [0.7 0.8 1],…
'FontName', 'Arial',…
'FontSize', 10,…
'FontWeight', 'bold',…
'EdgeColor', 'none',…
'HorizontalAlignment', 'left',…
'VerticalAlignment', 'top');

% 添加光源增强3D效果
light('Position',[1 1 1],'Style','infinite');
light('Position',[-1 -1 -1],'Style','infinite');
lighting gouraud;
material dull;

hold(ax, 'off');

% 设置图形背景渐变
set(gcf, 'Color', [0 0 0.05]); % 深蓝色背景

% 添加网格线增强
grid(ax, 'on');
grid(ax, 'minor');

% 结果输出
fprintf('绘图完成:3D散点概率分布图(颜色映射深度)\\n');
fprintf('色阶:自定义科学色阶(浅黄→橙→红→紫→深蓝)\\n');
fprintf('点数量:%d\\n', n_points);

3D surf曲面图对应2D切片

参考:

自制:

代码:

clear; clc; close all;

%% 参数:上下布局2D精度曲线和3D响应曲面
%% ===================== 自定义区(可修改参数)=====================
% 1. 2D精度曲线数据
param_range = 50:5:150; % 参数范围
accuracy_2d = 0.7 + 0.2*sin((param_range-50)/100*2*pi) + 0.1*randn(size(param_range));
peak_idx = 11; % 峰值位置索引
sample_points = [1, 6, 11, 16, 21]; % 采样点位置

% 2. 3D响应曲面数据(增加网格密度)
n_points = 50; % 网格点数
n_estimators_3d = linspace(50, 150, n_points);
max_depth_3d = linspace(3, 15, n_points);

% 创建网格
[X, Y] = meshgrid(n_estimators_3d, max_depth_3d);
Z = zeros(n_points, n_points);
accuracy_3d = zeros(n_points, n_points);

% 生成3D曲面数据
for i = 1:n_points
for j = 1:n_points
% min_samples_split与n_estimators相关
Z(i,j) = 1 + cos((X(i,j)-50)/100) + X(i,j)*sin(9*(X(i,j)-50)/100);

% 精度计算
accuracy_3d(i,j) = 0.75 + 0.15*sin((X(i,j)-50)/100*pi) * …
cos((Y(i,j)-3)/12*pi) * …
(1 – Z(i,j)/15);
end
end

% 3. 颜色参数
curve_color = [0.2 0.8 0.2]; % 2D曲线绿色
fill_color = [0.8 1.0 0.8]; % 填充浅绿色
marker_color = [1.0 0.5 0.0]; % 橙色标记
line_color = [1.0 0.5 0.0]; % 橙色虚线
arrow_color = [1.0 0.5 0.0]; % 橙色箭头

% 4. 文字参数
xlabel_2d = 'n_{estimators}';
ylabel_2d = 'Accuracy';
zlabel_3d = 'min_{samples}_{split}';
legend_2d = {'Accuracy curve'};

% 5. 字体大小
label_fontsize = 13;
tick_fontsize = 11;

%% ===================== 统一模板执行区 =====================
figure('Position',[100 100 1200 800], 'Color','white');

% 上方:2D精度曲线
ax1 = subplot(2,1,1);
hold(ax1, 'on');
grid(ax1, 'on');

% 填充曲线下方区域
x_fill = [param_range(1), param_range, param_range(end)];
y_fill = [0, accuracy_2d, 0];
patch('XData', x_fill, 'YData', y_fill,…
'FaceColor', fill_color, 'EdgeColor', 'none',…
'Parent', ax1, 'FaceAlpha', 0.3, 'DisplayName', 'filled_{area}');

% 绘制精度曲线
plot(ax1, param_range, accuracy_2d,…
'Color', curve_color, 'LineWidth', 2.5,…
'DisplayName', legend_2d{1});

% 标记采样点
for i = 1:length(sample_points)
idx = sample_points(i);
if i == 1
plot(ax1, param_range(idx), accuracy_2d(idx), 'o',…
'Color', marker_color, 'MarkerFaceColor', marker_color,…
'MarkerSize', 10, 'DisplayName', 'Sample points');
else
plot(ax1, param_range(idx), accuracy_2d(idx), 'o',…
'Color', marker_color, 'MarkerFaceColor', marker_color,…
'MarkerSize', 10, 'HandleVisibility', 'off');
end

% 绘制橙色虚线(只画到3D部分的会由后面的代码处理)
plot(ax1, [param_range(idx), param_range(idx)],…
[accuracy_2d(idx), 0], '–', 'Color', line_color,…
'LineWidth', 1.5, 'HandleVisibility', 'off');
end

% 坐标轴设置
ax1.XLabel.String = xlabel_2d;
ax1.YLabel.String = ylabel_2d;
ax1.XLabel.FontName = 'Times New Roman';
ax1.YLabel.FontName = 'Times New Roman';
ax1.XLabel.FontSize = label_fontsize;
ax1.YLabel.FontSize = label_fontsize;
ax1.XLabel.FontWeight = 'bold';
ax1.YLabel.FontWeight = 'bold';
ax1.FontName = 'Times New Roman';
ax1.FontSize = tick_fontsize;
ax1.YLim = [0.2 1.2];

% 图例
legend(ax1, 'Location', 'northeast', 'FontName', 'Times New Roman',…
'FontSize', tick_fontsize, 'Box', 'on');

hold(ax1, 'off');

% 下方:3D响应曲面
ax2 = subplot(2,1,2);
hold(ax2, 'on');
grid(ax2, 'on');

% 创建平滑渐变色阶:紫色→蓝色→青色→绿色→黄色
N = 256;
custom_cmap = zeros(N, 3);
for i = 1:N
t = (i-1)/(N-1);

% 使用更平滑的颜色过渡函数
% 紫色 (0.5,0,0.5) → 蓝色 (0,0,1) → 青色 (0,1,1) → 绿色 (0,1,0) → 黄色 (1,1,0)
if t < 0.2
% 紫色到蓝色
t2 = t/0.2;
r = 0.5 * (1 – t2);
g = 0;
b = 0.5 + 0.5*t2;
elseif t < 0.4
% 蓝色到青色
t2 = (t-0.2)/0.2;
r = 0;
g = t2;
b = 1;
elseif t < 0.6
% 青色到绿色
t2 = (t-0.4)/0.2;
r = 0;
g = 1;
b = 1 – t2;
elseif t < 0.8
% 绿色到黄绿色
t2 = (t-0.6)/0.2;
r = t2;
g = 1;
b = 0;
else
% 黄绿色到黄色
t2 = (t-0.8)/0.2;
r = 1;
g = 1;
b = 0;
end
custom_cmap(i,:) = [r, g, b];
end

% 绘制3D曲面 – 根据高度Z映射颜色
surf(ax2, X, Y, Z, Z, 'EdgeColor', 'none', 'FaceAlpha', 0.9);
colormap(ax2, custom_cmap);

% 设置颜色范围
z_min = min(Z(:));
z_max = max(Z(:));
caxis(ax2, [z_min z_max]);

colorbar(ax2);
view(ax2, 45, 30);

% 连接2D和3D的橙色虚线
for i = 1:length(sample_points)
idx = sample_points(i);
x_val = param_range(idx);

% 找到3D中对应的y和z值
y_val = 9; % max_depth中间值

% 计算对应的Z值
z_val = 1 + cos((x_val-50)/100) + x_val*sin(9*(x_val-50)/100);

% 绘制连接线
plot3(ax2, [x_val, x_val], [y_val, y_val],…
[0, z_val], '–', 'Color', line_color, 'LineWidth', 1.5);

% 标记3D中的点
scatter3(ax2, x_val, y_val, z_val, 50,…
'MarkerFaceColor', marker_color,…
'MarkerEdgeColor', marker_color);
end

% 绘制指向最佳参数的箭头
[best_acc, best_idx] = max(accuracy_2d);
best_x = param_range(best_idx);
best_y = 9;
best_z = 1 + cos((best_x-50)/100) + best_x*sin(9*(best_x-50)/100);

arrow_start = [best_x-10, best_y-2, best_z+1];
arrow_end = [best_x, best_y, best_z];

quiver3(ax2, arrow_start(1), arrow_start(2), arrow_start(3),…
arrow_end(1)-arrow_start(1), arrow_end(2)-arrow_start(2),…
arrow_end(3)-arrow_start(3), 0,…
'Color', arrow_color, 'LineWidth', 2, 'MaxHeadSize', 1);

% 坐标轴设置
ax2.XLabel.String = 'n_{estimators}';
ax2.YLabel.String = 'max_{depth}';
ax2.ZLabel.String = zlabel_3d;
ax2.XLabel.FontName = 'Times New Roman';
ax2.YLabel.FontName = 'Times New Roman';
ax2.ZLabel.FontName = 'Times New Roman';
ax2.XLabel.FontSize = label_fontsize;
ax2.YLabel.FontSize = label_fontsize;
ax2.ZLabel.FontSize = label_fontsize;
ax2.XLabel.FontWeight = 'bold';
ax2.YLabel.FontWeight = 'bold';
ax2.ZLabel.FontWeight = 'bold';
ax2.FontName = 'Times New Roman';
ax2.FontSize = tick_fontsize;

% 添加文字标注
text(ax2, arrow_start(1)-15, arrow_start(2)-3, arrow_start(3)+1,…
'Best parameters', 'FontName', 'Times New Roman',…
'FontSize', tick_fontsize, 'FontWeight', 'bold');

hold(ax2, 'off');

fprintf('参数优化可视化图绘制完成\\n');

左边折线图右边3D色谱

参考:

Ai提示词:

左边的图,公式使用水蓝色,放在图的中下方区域(靠右侧),散点采用三角形,置信区间分两档,分别用深浅的水蓝色绘制,添加透明度,最中间是虚线绘制的拟合曲线,线宽大约为2,网络标示清晰,图标背景用浅蓝色填充

右边的图,标示出坐标轴xyz,colorbar的标题为ρ_z/(kg/m^3),加粗显示,colorbar设置为水蓝色的渐变色,绘制一个3D的立方体,z坐标向下增加

自制:

立方体这个尽力了,这个Z轴的箭头一眼就不是matlab画的

代码:

clear; clc; close all;

%% 自定义区
% 数据参数
depth = [0 50 100 150 200 250 300 350 400 450 500 550 600 650 700 750 800 850 900];
density = [1025.74 1026.49 1027.49 1028.49 1029.49 1030.49 1031.49 1032.49 1033.49 1034.49 1035.49 1036.49 1037.49 1038.49 1039.49 1040.49 1041.49 1042.49 1043.49];

% 添加噪声到观测数据
noise_level = 1; % 噪声水平
density_noisy = density + noise_level * randn(size(density));

% 颜色参数
color_water = [0.4 0.7 1.0]; % 水蓝色主色调
color_fit = color_water * 0.8; % 拟合曲线颜色
color_data = [0.3 0.3 0.3]; % 散点颜色
color_ci1 = color_water * 0.8; % 浅色置信区间
color_ci2 = color_water * 0.9; % 深色置信区间
bg_color_left = [0.9 0.95 1.0]; % 左侧图浅蓝色背景
bg_color = [1 1 1]; % 白色背景
grid_color = [0.8 0.8 0.8];
text_color = [0.2 0.2 0.2];

% 置信区间参数
ci_level1 = 0.5; % 50%置信区间
ci_level2 = 0.9; % 90%置信区间

% 文字参数
xlabel_2d = 'Depth/m';
ylabel_2d = 'Density/(kg/m^3)';
zlabel_3d = 'ρ_z/(kg/m^3)';
equation_text = 'ρ_z = -1.90×10^{-12}z^4 + 4.46×10^{-8}z^3 – 4.19×10^{-5}z^2 + 2.50×10^{-2}z + 1025.78';
r2_text = 'R^2 = 0.999';
fontSize = 10;
labelFontSize = 12;

%% 统一模板执行区
figure('Position', [150, 150, 1300, 550], 'Color', bg_color);

% 2D密度拟合曲线(左侧)
ax2d = subplot(1, 2, 1);
hold(ax2d, 'on');

% 设置左侧图背景
set(ax2d, 'Color', bg_color_left);

% 多项式拟合
p = polyfit(depth, density_noisy, 4);
depth_fine = linspace(0, 900, 200);
density_fit = polyval(p, depth_fine);

% 计算置信区间
n = length(depth);
dof = n – length(p); % 自由度
t_critical1 = tinv(1 – (1-ci_level1)/2, dof);
t_critical2 = tinv(1 – (1-ci_level2)/2, dof);

% 计算预测标准误差
y_fit_all = polyval(p, depth);
residuals = density_noisy – y_fit_all;
mse = sum(residuals.^2) / dof;

% 计算预测值及其标准误差
X_fine = [depth_fine.^4; depth_fine.^3; depth_fine.^2; depth_fine; ones(size(depth_fine))]';
X_obs = [depth.^4; depth.^3; depth.^2; depth; ones(size(depth))]';

% 计算Hat矩阵
H = X_obs * pinv(X_obs' * X_obs) * X_obs';
leverage = diag(H);
mean_leverage = mean(leverage);

% 预测标准误差
pred_se = sqrt(mse * (1 + mean_leverage));

ci1 = t_critical1 * pred_se;
ci2 = t_critical2 * pred_se;

% 绘制置信区间
fill([depth_fine, fliplr(depth_fine)], …
[density_fit+ci2, fliplr(density_fit-ci2)], …
color_ci2, 'EdgeColor', 'none', 'FaceAlpha', 0.3, 'DisplayName', '90% Confidence Interval');

fill([depth_fine, fliplr(depth_fine)], …
[density_fit+ci1, fliplr(density_fit-ci1)], …
color_ci1, 'EdgeColor', 'none', 'FaceAlpha', 0.5, 'DisplayName', '50% Confidence Interval');

% 绘制散点数据(三角形)
scatter(depth, density_noisy, 50, color_data, 'filled', 'Marker', '^', 'DisplayName', 'Observed Data (with noise)');

% 绘制拟合曲线(虚线)
plot(depth_fine, density_fit, '–', 'Color', color_fit, 'LineWidth', 2.5, 'DisplayName', 'Fitted Curve');

% 添加方程和R²(中下方靠右)
text(600, 1028, equation_text, 'FontName', 'Times New Roman', 'FontSize', fontSize, …
'Color', color_water, 'HorizontalAlignment', 'center');
text(600, 1027.5, r2_text, 'FontName', 'Times New Roman', 'FontSize', fontSize, …
'Color', color_water, 'HorizontalAlignment', 'center');

% 网格和坐标轴设置
grid(ax2d, 'on');
ax2d.GridColor = grid_color;
ax2d.GridAlpha = 1;
ax2d.GridLineStyle = '-';

ax2d.XLim = [0, 900];
ax2d.YLim = [1025, 1045];
xlabel(ax2d, xlabel_2d, 'FontName', 'Times New Roman', 'FontSize', labelFontSize, 'FontWeight', 'bold');
ylabel(ax2d, ylabel_2d, 'FontName', 'Times New Roman', 'FontSize', labelFontSize, 'FontWeight', 'bold');
ax2d.FontName = 'Times New Roman';
ax2d.FontSize = fontSize;
ax2d.Box = 'on';

% 图例
legend(ax2d, 'Location', 'northwest', 'FontName', 'Times New Roman', 'FontSize', fontSize-1);

hold(ax2d, 'off');

% 3D密度分布立方体(右侧)
ax3d = subplot(1, 2, 2);
hold(ax3d, 'on');
view(ax3d, 30, 25);

% 创建水蓝色渐变色
water_colormap = zeros(64, 3);
for i = 1:64
ratio = (i-1)/63;
water_colormap(i,:) = [0.4+0.3*ratio, 0.7+0.2*ratio, 1.0-0.2*ratio];
end
colormap(ax3d, water_colormap);

% 绘制实心立方体
% 定义立方体的8个顶点
vertices = [0 0 0; 10 0 0; 10 10 0; 0 10 0; 0 0 10; 10 0 10; 10 10 10; 0 10 10];

% 定义立方体的6个面
faces = [1 2 3 4; % 底面 (z=0)
5 6 7 8; % 顶面 (z=10)
1 2 6 5; % 前面 (y=0)
3 4 8 7; % 后面 (y=10)
2 3 7 6; % 右面 (x=10)
1 4 8 5]; % 左面 (x=0)

% 计算每个面的密度值(根据z坐标)
face_density = zeros(6, 1);
for i = 1:6
% 获取每个面的z坐标
face_vertices = vertices(faces(i,:), :);
z_vals = face_vertices(:, 3);
% 使用平均z值计算密度
avg_z = mean(z_vals);
face_density(i) = polyval(p, avg_z * 90); % 转换为实际深度
end

% 绘制实心立方体
min_density = min(face_density);
max_density = max(face_density);

for i = 1:6
% 计算颜色索引
density_val = face_density(i);
color_idx = round((density_val – min_density) / (max_density – min_density) * 63) + 1;
color_idx = max(1, min(color_idx, 64));

% 绘制立方体面
patch('Vertices', vertices, 'Faces', faces(i,:), …
'FaceColor', water_colormap(color_idx,:), 'EdgeColor', 'k', …
'LineWidth', 1, 'FaceAlpha', 0.2);
end

% 添加坐标系指示(Z轴向下)
% X轴
quiver3(ax3d, 0, 0, 0, 12, 0, 0, 'Color', text_color, 'LineWidth', 1.5, 'MaxHeadSize', 0.5);
text(ax3d, 13, 0, 0, 'X', 'FontName', 'Times New Roman', 'FontSize', labelFontSize, …
'Color', text_color, 'FontWeight', 'bold');

% Y轴
quiver3(ax3d, 0, 0, 0, 0, 12, 0, 'Color', text_color, 'LineWidth', 1.5, 'MaxHeadSize', 0.5);
text(ax3d, 0, 13, 0, 'Y', 'FontName', 'Times New Roman', 'FontSize', labelFontSize, …
'Color', text_color, 'FontWeight', 'bold');

% Z轴(向下增大)
quiver3(ax3d, 0, 0, 10, 0, 0, 12, 'Color', text_color, 'LineWidth', 1.5, 'MaxHeadSize', 0.5);
text(ax3d, 0, 0, 23, 'Z', 'FontName', 'Times New Roman', 'FontSize', labelFontSize, …
'Color', text_color, 'FontWeight', 'bold');

% 添加深度方向标注
text(ax3d, -2, -2, 5, 'Depth', 'FontName', 'Times New Roman', 'FontSize', fontSize, …
'Color', text_color, 'HorizontalAlignment', 'right');

% 添加色标
caxis(ax3d, [min_density, max_density]);

% 创建色标并设置位置
cb_pos = [0.88, 0.15, 0.03, 0.7];
cb = colorbar(ax3d, 'Location', 'manual', 'Position', cb_pos);
cb.Label.String = zlabel_3d;
cb.Label.FontName = 'Times New Roman';
cb.Label.FontSize = labelFontSize;
cb.Label.FontWeight = 'bold';
cb.FontName = 'Times New Roman';
cb.FontSize = fontSize;

% 设置色标刻度
cb.Ticks = [min_density, (min_density + max_density)/2, max_density];
cb.TickLabels = {num2str(min_density, '%.2f'), …
num2str((min_density + max_density)/2, '%.2f'), …
num2str(max_density, '%.2f')};

% 3D坐标轴设置
ax3d.XLim = [-2, 15];
ax3d.YLim = [-2, 15];
ax3d.ZLim = [-2, 25];
ax3d.XTick = [];
ax3d.YTick = [];
ax3d.ZTick = [];
ax3d.Box = 'on';
ax3d.Projection = 'perspective';
ax3d.Color = bg_color;

% 添加坐标轴标签
xlabel(ax3d, 'X Coordinate', 'FontName', 'Times New Roman', 'FontSize', fontSize, 'FontWeight', 'bold');
ylabel(ax3d, 'Y Coordinate', 'FontName', 'Times New Roman', 'FontSize', fontSize, 'FontWeight', 'bold');
zlabel(ax3d, 'Depth (Z)', 'FontName', 'Times New Roman', 'FontSize', fontSize, 'FontWeight', 'bold');

hold(ax3d, 'off');

% 调整子图位置
set(ax2d, 'Position', [0.08, 0.12, 0.38, 0.8]);
set(ax3d, 'Position', [0.52, 0.12, 0.38, 0.8]);

% 结果输出
fprintf('海洋密度深度变化可视化图绘制完成\\n');

3D surf曲面+散点图+双colorbar

参考:

AI识别:

  • 坐标轴定义
    • X、Y 轴:代表水平面上的平面坐标(单位:米),范围约为 0∼12×104 米,覆盖了一个大面积的海域。
    • Z 轴:代表垂直深度(单位:米),值为负表示海面以下,范围从 0 到 – 3500 米。
  • 颜色映射3D 曲面使用了从黄色到深蓝色的连续渐变色谱,是典型的 “彩虹色阶(Rainbow Colormap)”。
    • 黄色对应最浅的深度(约 – 1700 米)
    • 橙色、绿色、蓝色依次对应深度增加
    • 深蓝色对应最深的海底(约 – 2100 米)这种颜色映射直观展示了海底地形的起伏,黄色区域是浅丘,深蓝色区域是海沟或凹陷地带。
  • 主体部分:一条蓝色的柱状轨迹从海面(Z≈0)垂直延伸至海底附近,其颜色与海底地形的色标保持一致,代表该段轨迹的深度与周围海底地形的深度同步变化。
  • 特殊标记:在轨迹的最下端,也就是接触海底的部分,被专门用红色进行了绘制。这一设计是为了突出轨迹抵达海底的位置,与 3D 曲面下方的红色视觉呼应,让探测终点一目了然。

注意3D曲面使用的色谱colorbar类型,散点的颜色是另外一个colorbar,对于散点图中在3D曲面下方的点,使用红色绘制。

自制:

代码

clear; clc; close all;

%% 绘图类型:3D海底地形曲面+探测轨迹散点图
%% ===================== 自定义区(可修改参数)=====================
% 1. 数据参数
x_range = linspace(0, 120000, 60);
y_range = linspace(0, 120000, 60);
[X,Y] = meshgrid(x_range, y_range);

% 复杂海底地形
Z_seabed = -1700 – 600 * (sin(X/25000) .* cos(Y/35000) + 0.7*sin(X/12000) .* sin(Y/18000) + 0.3*sin(X/6000));

% 探测轨迹数据
traj_x = linspace(20000, 100000, 300);
traj_y = 25000 + 70000 * (sin(traj_x/18000) + 0.4*sin(traj_x/7000) + 0.2*sin(traj_x/3000));
traj_z = linspace(-800, -2800, 300) + 400 * sin(traj_x/20000);

% 2. 颜色参数
bg_color = [0.88 0.94 1.0];
text_color = [0.1 0.1 0.25];

% 海底地形色阶(使用viridis风格的科学色阶)
N = 256;
seabed_cmap = zeros(N, 3);
for i = 1:N
t = (i-1)/(N-1);
% viridis-like 色阶:深紫→蓝→绿→黄
if t < 0.25
% 深紫到蓝色
r = 0.267 + 0.333*t;
g = 0.005 + 0.495*t;
b = 0.329 + 0.371*t;
elseif t < 0.5
% 蓝色到青绿色
t2 = (t-0.25)/0.25;
r = 0.6 – 0.333*t2;
g = 0.5 + 0.4*t2;
b = 0.7 – 0.171*t2;
elseif t < 0.75
% 青绿色到黄绿色
t2 = (t-0.5)/0.25;
r = 0.267 + 0.733*t2;
g = 0.9 – 0.1*t2;
b = 0.529 – 0.429*t2;
else
% 黄绿色到亮黄色
t2 = (t-0.75)/0.25;
r = 1.0;
g = 0.8 + 0.2*t2;
b = 0.1 + 0.1*t2;
end
seabed_cmap(i,:) = [r, g, b];
end

% 轨迹颜色
traj_color_normal = [0.0 0.5 0.9]; % 正常轨迹(青色)
traj_color_below = [0.9 0.1 0.1]; % 海底下方(红色)

% 3. 文字参数
xlabel_txt = 'Horizontal Coordinate X (m)';
ylabel_txt = 'Horizontal Coordinate Y (m)';
zlabel_txt = 'Depth (m)';
seabed_cb_title = 'Seabed Depth (m)';
fontSize = 11;
labelFontSize = 13;

% 4. 显示参数
grid_alpha = 0.2;
edge_alpha = 0.25;
scatter_size = 45;

%% ===================== 统一模板执行区 =====================
figure('Position',[150,150,1000,800],'Color',bg_color);
ax = axes('Color',bg_color);
hold(ax, 'on');
view(ax, 50, 20);

% 网格设置
ax.XGrid = 'on';
ax.YGrid = 'on';
ax.ZGrid = 'on';
ax.GridColor = [0.25 0.35 0.6];
ax.GridAlpha = grid_alpha;
ax.Box = 'on';
ax.BoxStyle = 'full';
ax.LineWidth = 1.2;

% ————————–
% 1. 绘制3D海底地形曲面
% ————————–
surf_handle = surf(ax, X, Y, Z_seabed,…
'EdgeColor', [0.25 0.35 0.65],…
'EdgeAlpha', edge_alpha,…
'FaceColor', 'interp',…
'DisplayName', 'Seabed Terrain');
colormap(ax, seabed_cmap);
caxis(ax, [-2300, -1100]);

% ————————–
% 2. 绘制探测轨迹散点
% ————————–
% 计算海底深度
traj_seabed_z = interp2(X, Y, Z_seabed, traj_x, traj_y);
idx_below = traj_z < traj_seabed_z;
idx_above = ~idx_below;

% 绘制海底上方轨迹点
scatter3(ax, traj_x(idx_above), traj_y(idx_above), traj_z(idx_above),…
scatter_size, traj_color_normal, 'filled',…
'Marker', 'o',…
'MarkerEdgeColor', traj_color_normal*0.7,…
'LineWidth', 0.8,…
'DisplayName', 'Above Seabed');

% 绘制海底下方轨迹点
scatter3(ax, traj_x(idx_below), traj_y(idx_below), traj_z(idx_below),…
scatter_size, traj_color_below, 'filled',…
'Marker', 'o',…
'MarkerEdgeColor', traj_color_below*0.7,…
'LineWidth', 0.8,…
'DisplayName', 'Below Seabed');

% ————————–
% 3. 坐标轴设置
% ————————–
ax.XLim = [0, 120000];
ax.YLim = [0, 120000];
ax.ZLim = [-3500, 0];

ax.XLabel.String = xlabel_txt;
ax.YLabel.String = ylabel_txt;
ax.ZLabel.String = zlabel_txt;
ax.XLabel.FontName = 'Times New Roman';
ax.YLabel.FontName = 'Times New Roman';
ax.ZLabel.FontName = 'Times New Roman';
ax.XLabel.FontSize = labelFontSize;
ax.YLabel.FontSize = labelFontSize;
ax.ZLabel.FontSize = labelFontSize;
ax.XLabel.FontWeight = 'bold';
ax.YLabel.FontWeight = 'bold';
ax.ZLabel.FontWeight = 'bold';
ax.XLabel.Color = text_color;
ax.YLabel.Color = text_color;
ax.ZLabel.Color = text_color;
ax.FontName = 'Times New Roman';
ax.FontSize = fontSize;

% ————————–
% 4. Colorbar设置
% ————————–
cb = colorbar(ax, 'Location', 'eastoutside');
cb.Position = [0.85, 0.15, 0.02, 0.7];
cb.Label.String = seabed_cb_title;
cb.Label.FontName = 'Times New Roman';
cb.Label.FontSize = labelFontSize;
cb.Label.FontWeight = 'bold';
cb.Label.Color = text_color;
cb.Color = text_color;
cb.FontName = 'Times New Roman';
cb.FontSize = fontSize;

% ————————–
% 5. 光照和图例
% ————————–
light('Position', [1 1 0.5], 'Style', 'infinite', 'Color', [0.8 0.9 1.0]);
light('Position', [-1 -1 -0.5], 'Style', 'infinite', 'Color', [0.7 0.8 1.0]);
lighting gouraud;
material([0.5 0.7 0.3 15 0.6]);

legend(ax, 'Location', 'northwest', 'FontName', 'Times New Roman',…
'FontSize', fontSize, 'TextColor', text_color, 'Box', 'on');

hold(ax, 'off');

% 调整视角
view(ax, 135, 25);

fprintf('绘图完成\\n');

赞(0)
未经允许不得转载:171主机测评 » 【美赛画图】第二弹!(MATLAB不是万能的,但是没有MATLAB是万万不能的)O奖论文图片复刻——高级绘图matlab代码集锦,让你摆脱画图“一眼MATLAB”的痛苦!
分享到: 更多 (0)

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址