[matlab]子图排版和线性回归
[matlab]子图排版和线性回归
[matlab]子图排版和线性回归
图片
.rtcContent { padding: 30px; } .lineNode {font-size: 12pt; font-family: "Times New Roman", Menlo, Monaco, Consolas, "Courier New", monospace; font-style: normal; font-weight: normal; }
% median width
% swh
clear;clc;close all;
table_tidalflat = readtable('China_tidalflat.csv','VariableNamingRule','preserve');
tidalflat = table2array(table_tidalflat);% table to double
width_loc=find(isnan(tidalflat(:,6)));% index
tidalflat(width_loc,:)=[];
zeroloc=isnan(tidalflat(:,5));
tidalflat(zeroloc,:)=[];
x1=550;
y1=550/600*700*4/3;
figure('Position', [0, 0, x1, y1],'color','w')
%% tsm
subplot(4,2,1)
ranges = {[0 20], [20 50], [50 75], [75 280]};
numRanges = length(ranges);
y2 = zeros(1, numRanges);
s2 = zeros(1, numRanges);
X2 = strings(1, numRanges);
for i = 1:numRangesX2(i) = sprintf('%d-%d', ranges{i}(1), ranges{i}(2));
end
X2 = categorical(X2, X2);
for i = 1:numRangesloc1 = find(tidalflat(:,7) > ranges{i}(1) & tidalflat(:,7) < ranges{i}(2));B = filloutliers(tidalflat(loc1,6),'center','median');l2(i) = length(tidalflat(loc1,6));y2(i) = median(tidalflat(loc1,6),'omitnan');s2(i) = std(B,'omitnan');CI = prctile(tidalflat(loc1,6), [15, 85]);err = [y2(i) - CI(1), CI(2) - y2(i)];color = [246-(i-1)/(numRanges-1)*137 178-(i-1)/(numRanges-1)*177 147-(i-1)/(numRanges-1)*116]/256;hold onb2 = bar(X2(i), y2(i), 0.5, 'FaceColor', color, 'EdgeColor', 'none');text(X2(i), 0, num2str(l2(i)), 'HorizontalAlignment', 'center', 'VerticalAlignment', 'bottom','Color','w','FontName','times new roman','fontsize',8);
end
ax = gca;
ax.XGrid = 'off';
ax.YGrid = 'off';
xtips2 = b2(1).XEndPoints;
ytips2 = b2(1).YEndPoints;
set(gca,'FontSize',8,'fontname','times new roman');
xlabel('SSC (mg/L)','fontsize',8,'fontweight','bold')
ylabel('Median width (km)','fontsize',8,'fontweight','bold')
yticks([0 500 1000 1500])
yticklabels([0 0.5 1 1.5])
ylim([0 1500])
ax.TickLength = [0.02, 0.02];
ax.LineWidth = 1.5;
ax.XRuler.TickDirection = 'out';
ax.YRuler.TickDirection = 'out';
ax.Layer = 'top';
title('A','fontsize',8,'fontname','times new roman','fontweight','bold','position',[0.5,1500])
set(gca,'fontname','times new roman')
%% tidal_range
subplot(4,2,3)
ranges = {[0.5 2], [2 5],[5 7.5]};
numRanges = length(ranges);
y4 = zeros(1, numRanges);
s4 = zeros(1, numRanges);
X4 = strings(1, numRanges);
for i = 1:numRangesX4(i) = sprintf('%.1f-%.1f', ranges{i}(1), ranges{i}(2));
end
X4 = categorical(X4, X4);
for i = 1:numRangesloc1 = find(tidalflat(:,5) > ranges{i}(1) & tidalflat(:,5) < ranges{i}(2));B = filloutliers(tidalflat(loc1,6),'center','median');l4(i) = length(tidalflat(loc1,6));y4(i) = median(tidalflat(loc1,6),'omitnan');s4(i) = std(tidalflat(loc1,6),'omitnan');CI = prctile(tidalflat(loc1,6), [15, 85]);err = [y4(i) - CI(1), CI(2) - y4(i)];color = [246-(i-1)/(numRanges-1)*137 178-(i-1)/(numRanges-1)*177 147-(i-1)/(numRanges-1)*116]/256;hold onb4 = bar(X4(i), y4(i), 0.5, 'FaceColor', color, 'EdgeColor', 'none');text(X4(i), 0, num2str(l4(i)), 'HorizontalAlignment', 'center', 'VerticalAlignment', 'bottom','Color','w','FontName','times new roman','fontsize',8);
end
ax = gca;
ax.XGrid = 'off';
ax.YGrid = 'off';
xtips4 = b4(1).XEndPoints;
ytips4 = b4(1).YEndPoints;
set(gca,'FontSize',8,'fontname','times new roman');
xlabel('Tidal range (m)','fontsize',8,'fontweight','bold')
ylabel('Median width (km)','fontsize',8,'fontweight','bold')
yticks([0 500 1000])
yticklabels([0 0.5 1])
ax.TickLength = [0.02, 0.02];
ax.LineWidth = 1.5;
ax.XRuler.TickDirection = 'out';
ax.YRuler.TickDirection = 'out';
ax.Layer = 'top';
title('C','fontsize',9,'fontname','times new roman','fontweight','bold','position',[0.5,1000])
set(gca,'fontname','times new roman')
%% swh
subplot(4,2,5)
% ranges = {[0 0.4],[0.4 0.8],[0.8 1.2]};
ranges = {[0 0.2],[0.2 0.4],[0.4 0.6],[0.6 1.2]};
numRanges = length(ranges);
y4 = zeros(1, numRanges);
s4 = zeros(1, numRanges);
X4 = strings(1, numRanges);
for i = 1:numRangesX4(i) = sprintf('%.1f-%.1f', ranges{i}(1), ranges{i}(2));
end
X4 = categorical(X4, X4);
for i = 1:numRangesloc1 = find(tidalflat(:,8) > ranges{i}(1) & tidalflat(:,8) < ranges{i}(2));B = filloutliers(tidalflat(loc1,6),'center','median');l4(i) = length(tidalflat(loc1,6));y4(i) = median(tidalflat(loc1,6),'omitnan');s4(i) = std(tidalflat(loc1,6),'omitnan');CI = prctile(tidalflat(loc1,6), [15, 85]);err = [y4(i) - CI(1), CI(2) - y4(i)];color = [246-(i-1)/(numRanges-1)*137 178-(i-1)/(numRanges-1)*177 147-(i-1)/(numRanges-1)*116]/256;hold onb4 = bar(X4(i), y4(i), 0.5, 'FaceColor', color, 'EdgeColor', 'none');text(X4(i), 0, num2str(l4(i)), 'HorizontalAlignment', 'center', 'VerticalAlignment', 'bottom','Color','w','FontName','times new roman','fontsize',8);
end
ax = gca;
ax.XGrid = 'off';
ax.YGrid = 'off';
xtips4 = b4(1).XEndPoints;
ytips4 = b4(1).YEndPoints;
set(gca,'FontSize',8,'fontname','times new roman');
xlabel('Wave height (m)','fontsize',8,'fontweight','bold')
ylabel('Median width (km)','fontsize',8,'fontweight','bold')
yticks([0 400 800])
yticklabels([0 0.4 0.8])
ylim([0 800])
ax.TickLength = [0.02, 0.02];
ax.LineWidth = 1.5;
ax.XRuler.TickDirection = 'out';
ax.YRuler.TickDirection = 'out';
ax.Layer = 'top';
title('E','fontsize',9,'fontname','times new roman','fontweight','bold','position',[0.5,800])
set(gca,'fontname','times new roman')
%%
clearvars -except x1 y1
table_tidalflat = readtable('China_tidalflat.csv','VariableNamingRule','preserve');
tidalflat = table2array(table_tidalflat);
nanloc1=isnan(tidalflat(:,6));
tidalflat(nanloc1,:)=[];
zeroloc=isnan(tidalflat(:,5));
tidalflat(zeroloc,:)=[];
swh=tidalflat(:,8);
tsm=tidalflat(:,7);
tr=tidalflat(:,5);
width=tidalflat(:,6);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
[swh_uni,aa]=unique(tidalflat(:,end),'stable');
tf_unique=[];
for i=1:length(aa)loc=find(tidalflat(:,end)==swh_uni(i));slope_loc=tidalflat(loc,:);slope_u=mean(slope_loc,1);tf_unique=[tf_unique;slope_u];
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
lon=tf_unique(:,2);
lat=tf_unique(:,3);
y=tf_unique(:,6);
y_cbrt=y.^(1/3);
y_log=log(y);
x_tsm=tf_unique(:,7);
x_tsm_cbrt=tf_unique(:,7).^(1/3);
x_tsm_log=log(tf_unique(:,7));
x_tsm_std=(x_tsm-mean(x_tsm))./std(x_tsm);
x_tr=tf_unique(:,5);
x_tr_std=(x_tr-mean(x_tr))./std(x_tr);
x_tr_cbrt=tf_unique(:,5).^(1/3);
x_tr_log=log(tf_unique(:,5));
x_swh=tf_unique(:,8);
x_swh_std=(x_swh-mean(x_swh))./std(x_swh);
x_swh_cbrt=tf_unique(:,8).^(1/3);
x_swh_log=log(tf_unique(:,8));
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Y1=y_cbrt;
X = [x_tsm_cbrt, x_tr_cbrt, x_swh_cbrt];
mdl = stepwiselm(X, Y1, 'Criterion', 'bic');
formula = mdl.Formula;
disp(mdl);
X = [x_tr_cbrt x_tsm_cbrt];
model = fitlm(X, y_cbrt);
r = model.Rsquared.Ordinary;
adj_r = model.Rsquared.Adjusted;
rmse = model.RMSE;
subplot(4,2,7)
scatter(X(:,2),y_cbrt,6,[109 1 31]/256,'filled');
alpha(0.5);
x_tsm_cbrt_new = linspace(min(X(:,2)),max(X(:,2)),100)';
[y_cbrt_pred, ci] = predict(model, [mean(x_tr_cbrt)*ones(100,1) x_tsm_cbrt_new], 'Prediction', 'curve', 'Alpha', 0.05);
hold on;
text(5,2,'R^2=0.61','Color',[109 1 31]/256,'FontSize',8,'Fontname','times new roman','fontweight','bold')
fill([x_tsm_cbrt_new;flipud(x_tsm_cbrt_new)],[ci(:,1);flipud(ci(:,2))],[109 1 31]/256,'EdgeColor','none','FaceAlpha',0.3);
hold on;
plot(x_tsm_cbrt_new,y_cbrt_pred,'r-','LineWidth',1.5, 'Color', [109 1 31]/256);
ax = gca;
ax.LineWidth = 1.2;
ax.TickLength = [0.02, 0.02];
ax.TickDir = 'out';
ax.FontName = 'times new roman';
ax.FontSize = 8;
xlabel({'SSC';'(mg/L, cube-root-transformed)'},'fontsize',8,'fontweight','bold');
ylabel({'Width';'(m, cube-root-transformed)'},'fontsize',8,'fontweight','bold');
ylim([0 20])
xlim([0 6.5])
xticks([0 1.5 3 4.5 6])
title('G','fontsize',9,'fontname','times new roman','fontweight','bold','position',[0,20])
set(gca,'fontname','times new roman')
%%
nanloc2=isnan(tidalflat(:,4));
tidalflat(nanloc2,:)=[];
s=tidalflat(:,4).^(1/3);
swh=tidalflat(:,8);
tsm=tidalflat(:,7);
[swh_uni,aa]=unique(tidalflat(:,end),'stable');
tf_unique=[];
for i=1:length(aa)loc=find(tidalflat(:,end)==swh_uni(i));slope_loc=tidalflat(loc,:);slope_u=mean(slope_loc,1);tf_unique=[tf_unique;slope_u];
end
y=tf_unique(:,4);
y_cbrt=y.^(1/3);
y_log=log(y);
lon=tf_unique(:,2);
lat=tf_unique(:,3);
x_tsm = tf_unique(:,7);
x_tsm_cbrt=tf_unique(:,7).^(1/3);
x_tsm_log=log(tf_unique(:,7));
x_tsm_std=(x_tsm-mean(x_tsm))./std(x_tsm);
x_tr=tf_unique(:,5);
x_tr_cbrt=tf_unique(:,5).^(1/3);
x_tr_log=log(tf_unique(:,5));
x_swh=tf_unique(:,8);
x_swh_cbrt=tf_unique(:,8).^(1/3);
x_swh_log=log(tf_unique(:,8));
Y1=y_log;
X = [x_tsm_cbrt, x_tr_cbrt, x_swh_cbrt];
mdl = stepwiselm(X, Y1, 'Criterion', 'bic');
formula = mdl.Formula;
disp(formula);
disp(mdl);
X = [x_tr_cbrt x_tsm_cbrt];
model = fitlm(X, Y1);
r = model.Rsquared.Ordinary;
adj_r = model.Rsquared.Adjusted;
rmse = model.RMSE;
subplot(4,2,8)
scatter(X(:,1),y_log,6,[2 48 74]/256,'filled');
alpha(0.5);
x_swh_cbrt_new = linspace(min(X(:,1)),max(X(:,1)),100)';
[y_cbrt_pred, ci] = predict(model, [x_swh_cbrt_new mean(x_tsm_cbrt)*ones(100,1)], 'Prediction', 'curve', 'Alpha', 0.05);
hold on;
text(5/6.5*1.2+0.7,-7.3,'R^2=0.30','Color',[2 48 74]/256,'FontSize',8,'Fontname','times new roman','fontweight','bold')
fill([x_swh_cbrt_new;flipud(x_swh_cbrt_new)],[ci(:,1);flipud(ci(:,2))],[2 48 74]/256,'EdgeColor','none','FaceAlpha',0.3);
hold on;
plot(x_swh_cbrt_new,y_cbrt_pred,'r-','LineWidth',1.5, 'Color', [2 48 74]/256);
% calculate_relimp( {'y_log','x_tsm_cbrt','x_tr_cbrt'}, [y_log,x_tsm_cbrt,x_tr_cbrt]);
[R1,p1]=corr(x_tsm_cbrt,y_log)
[R2,p2]=corr(x_tr_cbrt,y_log)
[R3,p3]=corr(x_swh,y_log)
ax = gca;
ax.LineWidth = 1.2;
ax.TickLength = [0.02, 0.02];
ax.TickDir = 'out';
ax.FontName = 'times new roman';
ax.FontSize = 8;
xlim([0.7 1.9])
ylim([-8 -1])
xlabel({'Tidal range';'(m, cube-root-transformed)'},'fontsize',8,'fontweight','bold');
ylabel({'Slope';'(log-transformed)'},'fontsize',8,'fontweight','bold');
title('H','fontsize',8,'fontname','times new roman','fontweight','bold','position',[0.7,-1])
set(gca,'fontname','times new roman')
%%
clearvars -except x1 y1
table_tidalflat = readtable('China_tidalflat.csv','VariableNamingRule','preserve');
tidalflat = table2array(table_tidalflat);
nanloc1=find(tidalflat(:,4)==0);
tidalflat(nanloc1,:)=[];
zeroloc=isnan(tidalflat(:,5));
tidalflat(zeroloc,:)=[];
nanloc2=isnan(tidalflat(:,4));
tidalflat(nanloc2,:)=[];
%% tsm
ranges = {[0 20], [20 50], [50 75], [75 280]};
numRanges = length(ranges);
y2 = zeros(1, numRanges);
s2 = zeros(1, numRanges);
subplot(4,2,2)
X2 = strings(1, numRanges);
for i = 1:numRangesX2(i) = sprintf('%d-%d', ranges{i}(1), ranges{i}(2));
end
X2 = categorical(X2, X2);
for i = 1:numRangesloc1 = find(tidalflat(:,7) > ranges{i}(1) & tidalflat(:,7) < ranges{i}(2));B = filloutliers(tidalflat(loc1,4),'center','median');l2(i) = length(tidalflat(loc1,4));y2(i) = median(tidalflat(loc1,4),'omitnan');s2(i) = std(B,'omitnan');CI = prctile(tidalflat(loc1,4), [15, 85]);err = [y2(i) - CI(1), CI(2) - y2(i)];color = [144-(i-1)/(numRanges-1)*142 201-(i-1)/(numRanges-1)*153 231-(i-1)/(numRanges-1)*157]/256;hold onb2 = bar(X2(i), y2(i), 0.5, 'FaceColor', color, 'EdgeColor', 'none');text(X2(i), 0, num2str(l2(i)), 'HorizontalAlignment', 'center', 'VerticalAlignment', 'bottom','Color','w','FontName','times new roman','fontsize',8);
end
ax = gca;
ax.XGrid = 'off';
ax.YGrid = 'off';
xtips2 = b2(1).XEndPoints;
ytips2 = b2(1).YEndPoints;
yticks([0 2 4 6 8 10]*10^(-3))
yticklabels([0 2 4 6 8 10])
ylim([0 0.011])
set(gca,'FontSize',8,'fontname','times new roman');
xlabel('SSC (mg/L)','fontsize',8,'fontweight','bold')
ylabel('Median slope (\times10^{-3})','fontsize',8,'fontweight','bold')
ax.TickLength = [0.02, 0.02];
ax.LineWidth = 1.5;
ax.XRuler.TickDirection = 'out';
ax.YRuler.TickDirection = 'out';
ax.Layer = 'top';
title('B','fontsize',8,'fontname','times new roman','fontweight','bold','position',[0.5,0.011])
set(gca,'fontname','times new roman')
%% tidal range
ranges = {[0.5 2], [2 5],[5 7.5]};
numRanges = length(ranges);
y3 = zeros(1, numRanges);
s3 = zeros(1, numRanges);
subplot(4,2,4)
X3 = strings(1, numRanges);
for i = 1:numRangesX3(i) = sprintf('%.1f-%.1f', ranges{i}(1), ranges{i}(2));
end
X3 = categorical(X3, X3);
for i = 1:numRangesloc1 = find(tidalflat(:,5) > ranges{i}(1) & tidalflat(:,5) < ranges{i}(2));B = filloutliers(tidalflat(loc1,4),'center','median');l3(i) = length(tidalflat(loc1,4));y3(i) = median(tidalflat(loc1,4),'omitnan');s3(i) = std(tidalflat(loc1,4),'omitnan');CI = prctile(tidalflat(loc1,4), [15, 85]);err = [y3(i) - CI(1), CI(2) - y3(i)];color = [144-(i-1)/(numRanges-1)*142 201-(i-1)/(numRanges-1)*153 231-(i-1)/(numRanges-1)*157]/256;hold onb3 = bar(X3(i), y3(i), 0.5, 'FaceColor', color, 'EdgeColor', 'none');text(X3(i), 0, num2str(l3(i)), 'HorizontalAlignment', 'center', 'VerticalAlignment', 'bottom','Color','w','FontName','times new roman','fontsize',8);
end
ax = gca;
ax.XGrid = 'off';
ax.YGrid = 'off';
xtips3 = b3(1).XEndPoints;
ytips3 = zeros(1,length(y3));
set(gca,'FontSize',8,'fontname','times new roman');
xlabel('Tidal range (m)','fontsize',8,'fontweight','bold','fontname','times new roman')
ylabel('Median slope (\times10^{-3})','fontsize',8,'fontweight','bold','fontname','times new roman')
yticks([0 4 8 12 16]*10^(-3))
yticklabels([0 4 8 12 16])
ylim([0 0.016])
ax.TickLength = [0.02, 0.02];
ax.LineWidth = 2;
ax.XRuler.TickDirection = 'out';
ax.YRuler.TickDirection = 'out';
ax.Layer = 'top';
title('D','fontsize',9,'fontname','times new roman','fontweight','bold','position',[0.5,0.016])
set(gca,'fontname','times new roman')
%% swh
ranges = {[0 0.2],[0.2 0.4],[0.4 0.6],[0.6 1.2]};
% ranges = {[0 0.4],[0.4 0.8],[0.8 1.2]};
numRanges = length(ranges);
y3 = zeros(1, numRanges);
s3 = zeros(1, numRanges);
subplot(4,2,6)
X3 = strings(1, numRanges);
for i = 1:numRangesX3(i) = sprintf('%.1f-%.1f', ranges{i}(1), ranges{i}(2));
end
X3 = categorical(X3, X3);
for i = 1:numRangesloc1 = find(tidalflat(:,8) > ranges{i}(1) & tidalflat(:,8) < ranges{i}(2));B = filloutliers(tidalflat(loc1,4),'center','median');l3(i) = length(tidalflat(loc1,4));y3(i) = median(tidalflat(loc1,4),'omitnan');s3(i) = std(tidalflat(loc1,4),'omitnan');CI = prctile(tidalflat(loc1,4), [15, 85]);err = [y3(i) - CI(1), CI(2) - y3(i)];color = [144-(i-1)/(numRanges-1)*142 201-(i-1)/(numRanges-1)*153 231-(i-1)/(numRanges-1)*157]/256;hold onb3 = bar(X3(i), y3(i), 0.5, 'FaceColor', color, 'EdgeColor', 'none');text(X3(i), 0, num2str(l3(i)), 'HorizontalAlignment', 'center', 'VerticalAlignment', 'bottom','Color','w','FontName','times new roman','fontsize',8);
end
ax = gca;
ax.XGrid = 'off';
ax.YGrid = 'off';
xtips3 = b3(1).XEndPoints;
ytips3 = zeros(1,length(y3));
set(gca,'FontSize',8,'fontname','times new roman');
xlabel('Wave height (m)','fontsize',8,'fontweight','bold','fontname','times new roman')
ylabel('Median slope (\times10^{-3})','fontsize',8,'fontweight','bold','fontname','times new roman')
yticks([0 4 8 12]*10^(-3))
yticklabels([0 4 8 12])
ylim([0 0.012])
ax.TickLength = [0.02, 0.02];
ax.LineWidth = 2;
ax.XRuler.TickDirection = 'out';
ax.YRuler.TickDirection = 'out';
ax.Layer = 'top';
title('F','fontsize',9,'fontname','times new roman','fontweight','bold','position',[0.5,0.012])
set(gca,'fontname','times new roman')
print('-djpeg','-r300','fig2.jpg')
参考文献:
Liu, S., Hu, Z., Grandjean, T.J.et al.Dynamics and drivers of tidal flat morphology in China.Nat Commun16, 2153 (2025). https://doi.org/10.1038/s41467-025-57525-y