使用自定义colormap时插值效果异常,默认colormap运行正常
MATLAB自定义Colormap显示异常问题
我在MATLAB中使用自定义colormap时,代码运行效果不符合预期,但切换成默认colormap时一切正常。
当前代码如下:
mymap = [ 0.769, 0.992, 1.000; % 1 粘性层/水:淡蓝色 1.000, 0.976, 0.000; % 2 沉积物:黄色 1.000, 1.00, 1.00; % 3 上地壳上部带:浅肉色 1, 1, 1; % 4 无作用区域:白色 0.443, 0.961, 0.525; % 5 地幔:橄榄绿 0.000, 0.639, 1.000; % 6 软流层:深蓝色 0.000, 0, 0; % 7 边界1:黑色 1, 0.827, 0.596; % 8 上地壳第二带:浅肉色 0.502, 0.337, 0.122; % 9 下地壳第二带:深棕色 0.969, 0.733, 0.416; % 10 上地壳第一带:稍深棕色 0.651, 0.514, 0.318; % 11 下地壳第二带:棕色(原注释标注蓝色有误) 1, 1, 1; % 12 Upper_2:白色(原注释标注棕色2有误) ]; pcolor(mx,my,markcom); clim([0.5 12.5]); colormap(mymap); shading interp; axis tight; xticks([0 100 200]); yticks([0 50 100]); xlabel('距离 (km)', 'FontSize', 12, 'FontName', 'Arial') ylabel('深度 (km)' , 'FontSize', 12, 'FontName', 'Arial') %c = colorbar; title(['步长 = ', num2str(ntimestep),' ', num2str(timesum*1e-6/(365.25*24*3600)), ' 百万年']); hold on; v1 = [473, 873, 1273, 1473]; v2 = v1-273; [C,h] = contour(gridx/1000, gridy/1000, tk1-273, v2,'w', LabelSpacing=800, LineWidth=.9); clabel(C, h, v2, 'Fontsize', 12, 'Color','white', 'FontName', 'Arial') hold off; axis ij image;
当前输出效果:
排查与解决建议
- 确认
markcom数值范围:检查矩阵内所有值是否落在clim([0.5 12.5])区间内,且1-12的整数类别都有对应数据,避免数值缺失或超出范围导致映射异常。 - 关闭插值着色测试:注释掉
shading interp,改用默认的shading flat,因为插值会让离散的自定义颜色被平滑过渡,可能掩盖原本的颜色映射效果。 - 启用颜色条验证:取消
c = colorbar;的注释,查看颜色条是否正确显示12个自定义颜色,确认colormap是否被MATLAB正确加载。 - 修正colormap注释:代码中部分颜色的注释与实际RGB值不匹配,建议修正注释避免后续调试混淆。
内容的提问来源于stack exchange,提问作者Cristian Alberto Juarez Valle
相关产品推荐
相关产品推荐

