MATLAB SCI绘图|阴影误差图画法~
效果图
阴影误差图介绍
阴影误差图(Shaded Error Plots)是一种专业的数据可视化方式,主要用于展示一组数据点的中心趋势和其围绕该中心的可变性。该类图表广泛应用于科学研究、工程学、经济学和其他需要精确数据分析的领域。阴影误差图不仅提供了数据的均值(或其他中心测度数)位置信息,还通过阴影区域清晰地展示了数据的标准差或其他可变性度量,提供了数据集整体特性的全面视图。
-
均值(Mean): 阴影误差图通常以数据集的算数平均值作为中心趋势的代表。 -
标准差(Standard Deviation): 这是一种用于量化数据点偏离均值的度量,标准差越大,数据点的分散越广泛。 -
置信区间(Confidence Intervals): 尽管不是所有的阴影误差图都采用置信区间,但这是一种常见的方式,用于估计如果多次抽样的话,中心趋势可能会在何处。 -
矩阵运算和线性代数: 在处理多维数据或进行批量计算时,一些基本的矩阵运算和线性代数知识可能会被用到。
在阴影误差图中,通常通过填充阴影区域来表示标准差或置信区间,而这些阴影区域一般会用透明度(alpha值)来区分不同层次的标准差。例如,1个标准差的范围内的阴影可能会较深,而2或3个标准差的范围内的阴影则可能会相对较浅。
该图表形式的优点在于其直观性和信息量。通过单一的图表,读者可以快速获取数据集的多个统计特性,从而更为准确和全面地理解数据。
绘图源码
clc; clear; close all;
%--------------------------------------------------------------------------
% : 好玩的Matlab
% @公众号:好玩的Matlab
% 09,10,2023 :
% 2377389590 .com :
% as-is without any warranty. : This code is provided
%--------------------------------------------------------------------------
% 初始化数据
data = randn(50, 80) * 5;
x = (1:size(data, 2)) - 40;
yP = sin(linspace(-2 * pi, 2 * pi, length(x))) * 20;
y = bsxfun(60; , data, yP) +
% 计算标准差和平均值
err = std(data);
meany = mean(y, 1);
% 计算标准差的边界
lowerBound1 = flipud((meany - err));
upperBound1 = meany + err;
lowerBound2 = flipud((meany - 2 * err));
upperBound2 = meany + 2 * err;
lowerBound3 = flipud((meany - 3 * err));
upperBound3 = meany + 3 * err;
% 准备用于填充区域的数据
xconf1 = [x flipud(x')'];
yconf1 = [meany + err flipud((meany - err)')'];
xconf2 = [x flipud(x')'];
yconf2 = [meany + 2 * err flipud((meany - 2 * err)')'];
xconf3 = [x flipud(x')'];
yconf3 = [meany + 3 * err flipud((meany - 3 * err)')'];
% 选择图类型
type = 6;
% 创建图形
figure('Position', [476 356 1011 516]);
hold on;
% 根据选择的类型进行绘图
switch type
case 1
fHdl = fill(xconf1, yconf1, [0 1 0], 'EdgeColor', 'none', 'FaceAlpha', .2);
plot(x, meany, 'Color', [0, 1, 0], 'LineStyle', '-', 'LineWidth', 2);
case 2
fHdl(1) = fill(xconf1, yconf1, [0 1 0], 'EdgeColor', 'none', 'FaceAlpha', .2);
fHdl(2) = fill(xconf2, yconf2, [0 1 0], 'EdgeColor', 'none', 'FaceAlpha', .2);
fHdl(3) = fill(xconf3, yconf3, [0 1 0], 'EdgeColor', 'none', 'FaceAlpha', .2);
plot(x, meany, 'Color', [0, 1, 0], 'LineStyle', '-', 'LineWidth', 2);
case 3
plot(x,y,'.','color',[0.5,0.5,0.95]);
plot(x,meany,'LineStyle','-','LineWidth',3,'Color',[0.5,0.5,0.95]);
fHdl=fill(xconf1,yconf1,[0 0 0],'EdgeColor','none','FaceAlpha',.2);
case 4
dist = abs(y - meany);
normDist = dist ./ max(dist);
plot(x,meany,'LineStyle','-','LineWidth',2,'Color','k')
scatter(repmat(x,1,row),reshape(y',1,row*col),[],reshape(normDist',1,row*col), 'filled',...
'MarkerFaceAlpha',0.5,'MarkerEdgeColor','none','SizeData',30);
fHdl=fill(xconf1,yconf1,[0 0 0],'EdgeColor','none','FaceAlpha',.2);
c = colormap(jet(length(x)));
colorbar
case 5
plot(x,meany,'LineStyle','-','LineWidth',6,'Color',[1,0,0])
plot(x,meany,'o','MarkerSize',8,'MarkerFaceColor','w','MarkerEdgeColor',[1,0,0],'HandleVisibility', 'off')
fHdl=fill(xconf1,yconf1,[0.5,0.25,0.25],'EdgeColor','none','FaceAlpha',.2);
line(x, lowerBound1, 'Color', [1 0 0 0.5], 'LineWidth', 3, 'LineStyle', ':','HandleVisibility', 'off');
line(x, upperBound1, 'Color', [1 0 0 0.5], 'LineWidth', 3, 'LineStyle', ':','HandleVisibility', 'off');
case 6
fHdl(1)=fill(xconf1,yconf1,[0.9,0.25,0.25],'EdgeColor','none','FaceAlpha',.2);
fHdl(2)=fill(xconf2,yconf2,[0.9,0.25,0.25],'EdgeColor','none','FaceAlpha',.2);
fHdl(3)=fill(xconf3,yconf3,[0.9,0.25,0.25],'EdgeColor','none','FaceAlpha',.2);
plot(x,meany,'LineStyle','-','LineWidth',6,'Color',[1,0,0])
plot(x,meany,'o','MarkerSize',8,'MarkerFaceColor','w','MarkerEdgeColor',[1,0,0],'HandleVisibility', 'off')
line(x, lowerBound1, 'Color', [1 0 0 0.5], 'LineWidth', 3, 'LineStyle', ':','HandleVisibility', 'off');
line(x, upperBound1, 'Color', [1 0 0 0.5], 'LineWidth', 3, 'LineStyle', ':','HandleVisibility', 'off');
line(x, lowerBound2, 'Color', [0 1 0 0.5], 'LineWidth', 3, 'LineStyle', ':','HandleVisibility', 'off');
line(x, upperBound2, 'Color', [0 1 0 0.5], 'LineWidth', 3, 'LineStyle', ':','HandleVisibility', 'off');
line(x, lowerBound3, 'Color', [0 0 1 0.5], 'LineWidth', 3, 'LineStyle', ':','HandleVisibility', 'off');
line(x, upperBound3, 'Color', [0 0 1 0.5], 'LineWidth', 3, 'LineStyle', ':','HandleVisibility', 'off');
end
% 配置图形属性
uistack(fHdl, 'bottom');
ax = gca;
ax.Box = 'off';
ax.XLabel.String = 'X';
ax.YLabel.String = 'Y';
ax.FontName = 'Times New Roman';
ax.GridLineStyle = '-.';
ax.GridColor = 'k';
ax.XGrid = 'on';
ax.YGrid = 'on';
ax.LineWidth = 1;
ax.XMinorTick = 'on';
ax.YMinorTick = 'on';
ax.TickDir = 'in';
ax.FontSize = 18;
type = 1;
type = 2;
type = 3;
type = 4;
type = 5;
type = 6;
- -THE END- -
文章来源:渝西图像练习生
点赞 评论 收藏