You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Matlab创建带2-3个自定义断点的无渐变颜色条曲面图

实现Matlab多断点纯色曲面图(无渐变颜色条)

核心思路

要实现分段纯色的曲面图,关键是给每个数据区间分配唯一的颜色索引,配合对应长度的纯色colormap,再调整颜色条刻度让其显示为纯色块而非渐变。

完整修改代码(三区间示例)

以你要求的<0.8=白色、0.8-1.2=绿色、>1.2=蓝色为例,修改后的代码如下:

%% 多断点纯色曲面图
% 1. 定义颜色映射与断点:每个区间对应一种纯色
cmap = [1 1 1;    % 白色(对应索引1)
        0 1 0;    % 绿色(对应索引2)
        0 0 1];   % 蓝色(对应索引3)
breakpoints = [0.8, 1.2];  % 两个断点划分三个区间

% 2. 生成ColorData:为每个数据点分配对应区间的颜色索引
ColorData = zeros(size(Profile));
ColorData(Profile < breakpoints(1)) = 1;  % 小于0.8的区域
ColorData(Profile >= breakpoints(1) & Profile <= breakpoints(2)) = 2;  % 0.8-1.2的区域
ColorData(Profile > breakpoints(2)) = 3;  % 大于1.2的区域

% 3. 绘制曲面,关闭边缘线实现纯色块效果
hs = surf(xa, ya, Profile, ColorData, 'EdgeColor', 'none');
colormap(cmap);
hb = colorbar;

% 4. 保留你原有的坐标轴设置
set(gca, 'xdir', 'reverse');
set(gca, 'ydir', 'reverse');
set(gca, 'DataAspectRatio', [1 1 1]);
xlim([0 80+deltax]);
ylim([0 100+deltay]);

% 5. 调整颜色条刻度与标签,匹配断点区间
cticks = 1:length(cmap);  % 刻度放在每个颜色块的中心位置
cticklabels = {['< ', num2str(breakpoints(1))], ...
              [num2str(breakpoints(1)), ' - ', num2str(breakpoints(2))], ...
              ['> ', num2str(breakpoints(2))]};

hb.Ticks = cticks;
hb.TickLabels = cticklabels;
title('Sheared 4mm'); % 可根据需求修改标题

关键修改说明

  • ColorData逻辑修复:原代码未覆盖断点之间的区域,现在用明确的逻辑条件划分每个区间,确保所有数据点都被分配正确的颜色索引
  • 颜色条无渐变实现:将颜色条刻度设置为colormap的索引值,让颜色条直接显示为纯色块,而非渐变过渡
  • 标签准确性优化:直接生成对应区间的文本标签,避免原代码中拼接错误的问题

扩展到四区间(三个断点)

如果需要三个断点划分四个区间(比如<0.6=白、0.6-0.8=黄、0.8-1=绿、>1=蓝),只需调整以下部分:

% 定义四色映射与三个断点
cmap = [1 1 1; 1 1 0; 0 1 0; 0 0 1];
breakpoints = [0.6, 0.8, 1];

% 生成对应ColorData
ColorData = zeros(size(Profile));
ColorData(Profile < breakpoints(1)) = 1;
ColorData(Profile >= breakpoints(1) & Profile <= breakpoints(2)) = 2;
ColorData(Profile >= breakpoints(2) & Profile <= breakpoints(3)) = 3;
ColorData(Profile > breakpoints(3)) = 4;

% 调整颜色条标签
cticklabels = {['< ', num2str(breakpoints(1))], ...
              [num2str(breakpoints(1)), ' - ', num2str(breakpoints(2))], ...
              [num2str(breakpoints(2)), ' - ', num2str(breakpoints(3))], ...
              ['> ', num2str(breakpoints(3))]};

内容的提问来源于stack exchange,提问作者Brittney

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.04 00:50:38