您的位置首页生活百科

MATLAB添加色标(colorbar)

MATLAB添加色标(colorbar)

的有关信息介绍如下:

MATLAB添加色标(colorbar)

在MATLAB命令行窗口输入doc colorbar可以查看如何添加色标以及改变色标的属性。本文参照MATLAB帮助文档,简单介绍如何给图形添加色标(colorbar)。

第一,为峰值函数(peaks)的图形添加色标。启动MATLAB,新建脚本,输入如下代码:

close all; clear all; clc

figure(1)

surf(peaks(30))

colorbar('YTickLabel',{'Freezing','Cold','Cool',...

'Neutral','Warm','Hot','Burning','Nuclear'})

第二,保存和运行上述脚本,得到峰值函数(peaks)的图形(figure 1),并且图形右侧添加了色标。

第三,在上述脚本的基础上,接着输入如下代码:

figure(2)

contourf(peaks(30))

colormap cool

colorbar('location','SouthOutside')

其中('location','southoutside')控制色标处于图形中的位置,还可以取North,South,East,West,Northoutside,Southoutside,Eastoutside,Westoutside。

第四,保存和运行上述脚本,得到图像figure 2,如下图,色标位于图形底部。

第五,利用subplot对上述脚本进行改进,将figure 1与figure 2画在一张图。脚本改进如下:

close all; clear all; clc

%figure(1)

subplot(2,1,1)

surf(peaks(30))

colorbar('YTickLabel',{'Freezing','Cold','Cool',...

'Neutral','Warm','Hot','Burning','Nuclear'})

%figure(2)

subplot(2,1,2)

contourf(peaks(30))

colormap cool

colorbar('location','SouthOutside')

第六,保存和运行上述改进的脚本,figure 1与figure 2画在一张图,并且均添加了色标(colorbar)。