matlab中mesh怎么改变颜色,MATLAB中如何反转Colorbar的颜色,但并不反转Colorbar的刻度...

前阵子碰到了这个问题,网上也没搜到对应的方法,被折腾了

b14ce9464d2c7742bdb65121e4202564.png

2b514b76774991959b99c9f5da0718cd.png

先看一下colormap函数的两个用法(来自MATLAB Documentation):

colormap(map)sets the colormap for the current figure to the colormap specified bymap.

example

colormap(target,map)sets the colormap for the figure, axes, or chart specified bytarget,instead of for the current figure.

用法1:

colormap(map)

这样的话,它是给当前整个figure设置同一个colormap

用法2:

colormap(target,map)

这样,它把指定的target中的colormap设置为map,而不是把整个当前figure的colormap设置为map。

当我们一个窗口中有多个坐标系,而我们又想给每个坐标系设置不同的colormap,那么就能用这个用法2了。

map可以是以下的这些,之前在这篇:

dd5b51a693657a1ddf870441091ba079.png

2b514b76774991959b99c9f5da0718cd.png

如果想反转colorbar的颜色可以这样做:

colormap(h2, flipud(jet))

flipud函数可以把数组上下翻转,而这里的jet就是colorbar中的颜色(n行3列的矩阵),所以flipud(jet)就把colorbar的颜色给翻转了。

下面来看个例子吧:

在一个figure中放两个坐标系,坐标系中放mesh画的曲面并显示colorbar,为了方便,这里直接用了MATLAB自带的peaks函数来生成网格数据,然后mesh一下。我想让第二个坐标系中的colorbar颜色颠倒一下,如下:

324e225446e2c299ef136c0cde568675.png

可以看到colorbar的颜色颠倒了,图中曲面的颜色也对应地颠倒了。

Note:如果这里把Colorbar的YDir属性设置成reverse,的确也可以把colorbar的颜色颠倒一下,但是图中曲面的颜色却不会颠倒,而且colorbar的刻度也会倒过来。

上图的代码:

clc

clear

close all

[x, y, z] = peaks(300);

h1 = subplot(211);

mesh(x, y, z, 'CData', z, 'Parent', h1);

colormap(h1, jet) % 其实默认的也是这个,不写也行

shading interp

colorbar

title('没有反转colorbar的颜色')

h2 = subplot(212);

mesh(x, y, z, 'CData', z, 'Parent', h2);

colormap(h2, flipud(jet))% 反转colorbar的颜色

shading interp

colorbar

title('反转了colorbar的颜色')

2b514b76774991959b99c9f5da0718cd.png

--END--

979d0d405d0bdb4792dc41974f6fabd1.png

  • 8
    点赞
  • 23
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值