如何在Matlab中实现6个子图的色标刻度统一?
问题:让所有填充等高线子图与共享色条刻度一致
我定义了以下6个矩阵并绘制填充等高线图:
% Define Matrices Matrix1 = [2.7 -0.42 -2.22 -4.74 -9.47 159 0 0 0 0;... -0.006 -1.21 -3.22 -6.18 188.32 147 0 0 0 0;... 0.26 -1.05 -3.43 -7.09 174.28 131 0 0 0 0;... 1.17 0.31 202.88 183.32 158.92 0 0 0 0 0;... 200 195.37 183.58 166.97 144.47 0 0 0 0 0;... 180 176.34 167.48 153.06 130 0 0 0 0 0;... 162.65 160.51 153.57 140.44 118.18 0 0 0 0 0;... 147.65 146.98 141.55 129.76 106.38 0 0 0 0 0;... 134.88 135.6 130.96 120 96.13 0 0 0 0 0;... 123.52 125.77 121.31 112 0 0 0 0 0 0]; Matrix2 = [4 -0.99 -1.42 -0.33 1.44 187 0 0 0 0;... -0.006 -0.9 -1.21 -0.27 201.74 181 0 0 0 0;... 0.26 0.05 0 0.84 190.24 0 0 0 0 0;... 0.53 1.02 206.64 192.93 0 0 0 0 0 0;... 0 0 0 0 0 0 0 0 0 0;... 0 0 0 0 0 0 0 0 0 0;... 0 0 0 0 0 0 0 0 0 0;... 0 0 0 0 0 0 0 0 0 0;... 0 0 0 0 0 0 0 0 0 0;... 0 0 0 0 0 0 0 0 0 0]; Matrix3 = [3 5.4 9.26 11.06 8.27 -378 -503 -372 0 0;... 4.72 6.63 9.01 9.66 5.92 -387 -502 -372 0 0;... 6.61 9.48 10.65 9.54 3.19 -404 -410 0 0 0;... 8.53 12.05 12.71 10.7 1.44 -452 -524 0 0 0;... 9.75 14.02 14.9 11.46 0.41 -452 0 0 0 0;... 11.49 16.38 17.07 13.26 133.7 0 0 0 0 0;... 13.38 159.22 155.57 144.49 119.4 0 0 0 0 0;... 141.92 145.5 143.29 133.37 105.08 0 0 0 0 0;... 129.5 134.26 132.01 123 90.62 0 0 0 0 0;... 118.52 123.99 123.31 114.8 0 0 0 0 0 0]; Matrix4 = [3 4.27 6.75 7.89 7.63 7.84 8.29 9.14 127.59 115.07;... 2.63 4.18 5.97 7.06 7.8 7.63 8.47 8.66 125.6 113.44;... 2.2 3.68 5.32 6.23 7.23 7.53 7.98 134.6 122.03 0;... 1.9 2.72 4.08 5.79 6.59 7.24 7.89 128.33 116.66 0;... 0.69 2.337 3.36 4.54 5.78 6.47 131.8 120.86 0 0;... 1.45 2.54 2.99 4.82 140.65 131 0 0 0 0;... 0.77 142.41 138.94 133.44 0 0 0 0 0 0;... 0 0 0 0 0 0 0 0 0 0;... 0 0 0 0 0 0 0 0 0 0;... 0 0 0 0 0 0 0 0 0 0]; Matrix5 = [-2.1 -3.12 -13.34 -319 -421 -394 -520 0 0 0;... -3.1 -8.34 -22.63 -347 -441 -402 -519 0 0 0;... -6.14 -16.61 -40 -389 -470 -419 0 0 0 0;... -7.8 -25.64 -68 -437 -506 -442 0 0 0 0;... -8.3 -31 -98 -488 -545 0 0 0 0 0;... -8.04 -34.15 -133 0 0 0 0 0 0 0;... 155.71 123 -403 0 0 0 0 0 0 0;... 142.92 107.42 -466.85 0 0 0 0 0 0 0;... 130 4.96 0 0 0 0 0 0 0 0;... 120.71 0 0 0 0 0 0 0 0 0]; Matrix6 = [-360 -246 -229.72 -319 -421 -394 -520 0 0 0;... -6.95 -20 -269 -347 -441 -402 -519 0 0 0;... -6.61 -23 -326 -389 -470 -419 0 0 0 0;... -3.7 -15.73 -58 -437 -506 -442 0 0 0 0;... -1.63 -6.79 -16 -114 -545 0 0 0 0 0;... -0.28 -1.45 -4.29 151 135.5 118 96.2 0 0 0;... 163.7 160.4 155.09 148 139.89 129.86 112.75 88.91 68.28 0;... 0 0 143.34 138.28 132.21 125.62 118.46 110.49 100.79 63.28;... 0 0 0 0 0 0 0 0 0 0;... 0 0 0 0 0 0 0 0 0 0];
随后我执行以下绘图代码:
% Interpolate for smoother contour plot x = linspace(1,10,10); y = linspace(1,10,10); [xrow,ycol] = meshgrid(x,y); newpoints = 100; [xq,yq] = meshgrid(... linspace(min(min(xrow,[],2)),max(max(xrow,[],2)),newpoints ),... linspace(min(min(ycol,[],1)),max(max(ycol,[],1)),newpoints )... ); % Figures figure; subplot(3,2,1) Matrix1Interp = interp2(xrow,ycol,Matrix1,xq,yq,'cubic'); [c1,h1] = contourf(xq,yq,Matrix1Interp,100); set(h1, 'edgecolor','none'); axis square subplot(3,2,2) Matrix2Interp = interp2(xrow,ycol,Matrix2,xq,yq,'cubic'); [c2,h2] = contourf(xq,yq,Matrix2Interp,100); set(h2, 'edgecolor','none'); axis square subplot(3,2,3) Matrix3Interp = interp2(xrow,ycol,Matrix3,xq,yq,'cubic'); [c3,h3] = contourf(xq,yq,Matrix3Interp,100); set(h3, 'edgecolor','none'); axis square subplot(3,2,4) Matrix4Interp = interp2(xrow,ycol,Matrix4,xq,yq,'cubic'); [c4,h4] = contourf(xq,yq,Matrix4Interp,100); set(h4, 'edgecolor','none'); axis square subplot(3,2,5) Matrix5Interp = interp2(xrow,ycol,Matrix5,xq,yq,'cubic'); [c5,h5] = contourf(xq,yq,Matrix5Interp,100); set(h5, 'edgecolor','none'); axis square subplot(3,2,6) Matrix6Interp = interp2(xrow,ycol,Matrix6,xq,yq,'cubic'); [c6,h6] = contourf(xq,yq,Matrix6Interp,100); set(h6, 'edgecolor','none'); axis square ax = subplot(3, 2, 6); pos = get(ax, 'Position'); c = colorbar('Location', 'SouthOutside', 'Position', [0.1243 0.03316 0.785 0.03269]);
目前色条位置正确,但各子图的刻度与色条不匹配(色条仅关联最后一个子图),请问如何让所有子图的刻度与色条保持一致?
解决方案
要解决这个问题,核心是让所有子图使用统一的颜色映射范围,这样色条就能对应所有子图的数值。下面是两种实用的实现方法:
方法一:预先计算全局颜色范围(推荐)
这种方法先算出所有插值后矩阵的最小和最大值,让每个子图都基于这个全局范围绘制,确保颜色映射完全一致:
- 先完成所有插值计算并收集全局极值:
% 先计算所有插值矩阵 Matrix1Interp = interp2(xrow,ycol,Matrix1,xq,yq,'cubic'); Matrix2Interp = interp2(xrow,ycol,Matrix2,xq,yq,'cubic'); Matrix3Interp = interp2(xrow,ycol,Matrix3,xq,yq,'cubic'); Matrix4Interp = interp2(xrow,ycol,Matrix4,xq,yq,'cubic'); Matrix5Interp = interp2(xrow,ycol,Matrix5,xq,yq,'cubic'); Matrix6Interp = interp2(xrow,ycol,Matrix6,xq,yq,'cubic'); % 收集所有数据点,计算全局最小/最大值 allData = [Matrix1Interp(:); Matrix2Interp(:); Matrix3Interp(:); ... Matrix4Interp(:); Matrix5Interp(:); Matrix6Interp(:)]; globalMin = min(allData); globalMax = max(allData);
- 在绘制每个
contourf时,指定vmin和vmax参数,强制使用全局范围:
figure; % 子图1 subplot(3,2,1) [c1,h1] = contourf(xq,yq,Matrix1Interp,100,'vmin',globalMin,'vmax',globalMax); set(h1, 'edgecolor','none'); axis square % 子图2 subplot(3,2,2) [c2,h2] = contourf(xq,yq,Matrix2Interp,100,'vmin',globalMin,'vmax',globalMax); set(h2, 'edgecolor','none'); axis square % 子图3-6重复上述修改,都加上vmin和vmax参数 % ... % 最后添加色条,此时色条会对应全局范围,所有子图都匹配 ax = subplot(3, 2, 6); pos = get(ax, 'Position'); c = colorbar('Location', 'SouthOutside', 'Position', [0.1243 0.03316 0.785 0.03269]);
方法二:链接所有子图的颜色轴
如果你已经绘制了子图,可以通过链接它们的颜色轴来同步范围:
- 绘制子图时保存所有子图的句柄:
figure; ax(1) = subplot(3,2,1); % 子图1绘图代码... ax(2) = subplot(3,2,2); % 子图2绘图代码... ax(3) = subplot(3,2,3); % 子图3绘图代码... ax(4) = subplot(3,2,4); % 子图4绘图代码... ax(5) = subplot(3,2,5); % 子图5绘图代码... ax(6) = subplot(3,2,6); % 子图6绘图代码...
- 链接所有子图的颜色轴,并设置全局范围:
% 链接颜色轴,让所有子图的颜色范围同步 linkaxes(ax, 'color'); % 设置全局颜色范围(基于所有数据的极值) allData = [Matrix1Interp(:); Matrix2Interp(:); ... Matrix6Interp(:)]; caxis(ax, [min(allData) max(allData)]); % 添加色条 c = colorbar('Location', 'SouthOutside', 'Position', [0.1243 0.03316 0.785 0.03269]);
额外提示
- 你的矩阵里有大量
0值,这些可能是无效数据,建议先把它们替换为NaN,避免插值时出现异常:Matrix1(Matrix1 == 0) = NaN; % 其他矩阵同理 - 如果需要调整色条的刻度,可以手动设置:
c.Ticks = linspace(globalMin, globalMax, 5); % 设置5个均匀刻度 c.TickLabels = arrayfun(@num2str,
相关产品推荐
相关产品推荐

