Matlab colorbar相关操作

转载自『http://hi.baidu.com/imheaventian/item/8d3b8ecf2c209408c610b210』
1) matlab中画多张图如何使用同一范围的colorbar
例如生成了图1,2,3
生成图1时,使用
temp1=caxis;
将图1的z值的取值范围(即colorbar的取值范围)取出。
生成图2,3时
使用
caxis(temp1)
命令将图2,3的z值的取值范围设为同1相同。
然后对各个同使用colorbar命令便可以了。
解释:matlab将z值映射到colormap,colorbar通过z值和colormap的映射关系生成的,所以需要将不同的figure,z值映射相同的colormap索引。
命令:
caxis
caxis([cmin cmax])
caxis controls the mapping of data values to the colormap.
2) colorbar的刻度经常达不到极值,可以使用下面的方法达到极值
figure
[x,y,z]=peaks(20);
subplot(2,1,1),contourf(x,y,z),colorbar
subplot(2,1,2),contourf(x,y,z)
t1=caxis;
t1=linspace(t1(1),t1(2),6);
my_handle=colorbar('ytick',t1);
 
再比如:
load topo


subplot('position',[0 0.5 1 0.45 ])
axesm robinson
meshm(topo,topolegend)
demcmap(caxis)
h=colorbar('northoutside');
t=get(h,'xtick');


subplot('position',[0 0.05 1 0.45 ])
axesm robinson
meshm(topo,topolegend)
a=caxis;
demcmap(caxis)
colorbar('northoutside','xtick',[a(1),t,a(2)])


下面程序给出地形和大地水准面的结果:
load topo
load geoid
load coast


subplot('position',[0 0.5 1 0.45 ])
axesm robinson
meshm(topo,topolegend)
plotm(lat,long)
a=caxis;
demcmap(caxis)
colorbar('northoutside','xtick',[a(1),-6000:2000:4000,a(2)])


subplot('position',[0 0.05 1 0.45 ])
axesm robinson
meshm(geoid,geoidlegend)
plotm(lat,long)
demcmap(caxis)
a=caxis;
h=colorbar('northoutside','xtick',[ceil(a(1)),-80:20:60,floor(a(2))]);
另外,当图形窗口小时,用colorbar做出了刻度,当将图形窗口放大时,会发现刻度值重复出现或有错误。要消失其错误,现在还没有好的方法。只能首先用大的窗口打开,然后再colorbar。
3) 不同图的colorbar色标相同,但是colorbar的刻度范围不同
matlab如何使不同的colorbar的相同颜色代表相同的数值?
但是同时,每个colorbar显示的最大最小值又是不同的。
比如
图1中colorbar刻度的范围是[-5e3,5e3]
而图2中colorbar刻度的范围是[-1e3,1e3]
即,图2的colorbar的颜色范围只是图1color把人颜色范围的一部分?
 
解决方案:
set(gca,'CLim',[a,b])
用来使不同的图使用相同的色标
caxis([a,b])达到同样的效果:


CLim   
[cmin, cmax]
Color axis limits. Determines how MATLAB maps the CData values of surface and patch objects to the figure's Colormap. cmin is the value of the data mapped to the first color in the colormap. cmax is the value of the data mapped to the last color in the colormap. MATLAB linearly interpolates data values in between across the colormap and clamps data values outside to either the first or last alphamap colormap color, whichever is closest.
When CLimMode is auto (the default), MATLAB assigns cmin the minimum data value and cmax the maximum data value in the graphics object's CData. This maps CData elements with minimum data value to the first colormap entry and with maximum data value to the last colormap entry.
If the axes contains multiple graphics objects, MATLAB sets CLim to span the range of all objects' CData.
See the caxis function reference page for related information.
  
接下来控制colorbar的范围:
set(h,colorbar,'ylim',[a0,b0])便可以了
试试两者差异
surf(peaks(30)); colormap jet; colorbar
surf(peaks(30)); colormap jet; h=colorbar;set(h,'Ylim',[0,6])
 
若连原来图的颜色亦想控制, 试
surf(peaks(30)); colormap jet; caxis([0,6]); h=colorbar; set(h,'Ylim',[0,6]);

猜你喜欢

转载自blog.csdn.net/eric_e/article/details/81123149
今日推荐