同一imshow matplotlib中的两个不同的颜色颜色图 [英] Two different color colormaps in the same imshow matplotlib

查看:565
本文介绍了同一imshow matplotlib中的两个不同的颜色颜色图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

让我们假设下面的例子

import matplotlib.pyplot as plt
import numpy as np

v1 = -1 + 2*np.random.rand(50,150)
fig = plt.figure()
ax = fig.add_subplot(111)
p = ax.imshow(v1,interpolation='nearest')
cb = plt.colorbar(p,shrink=0.5)
plt.xlabel('Day')
plt.ylabel('Depth')
cb.set_label('RWU')
plt.show()

我想在不同的颜色图中显示低于零的值,而不是高于零的值

I want to show the values below zero in a different colormap than the values above zero

推荐答案

首先,您是否可能只想使用发散的颜色图,中性"为零,发散为两种不同的颜色?这是一个例子:

First of all, is it possible that you just want to use a diverging colormap, 'neutral' at zero, and diverging to two distinct colours? This is an example:

import matplotlib.pyplot as plt
import numpy as np

v1 = -1+2*np.random.rand(50,150)
fig,ax = plt.subplots()
p = ax.imshow(v1,interpolation='nearest',cmap=plt.cm.RdBu)
cb = plt.colorbar(p,shrink=0.5)
ax.set_xlabel('Day')
ax.set_ylabel('Depth')
cb.set_label('RWU')
plt.show()

如果您真的想使用两个不同的颜色图,这是使用蒙版数组的解决方案:

If you really want to use two different colormaps, this is a solution with masked arrays:

import matplotlib.pyplot as plt
import numpy as np
from numpy.ma import masked_array

v1 = -1+2*np.random.rand(50,150)
v1a = masked_array(v1,v1<0)
v1b = masked_array(v1,v1>=0)
fig,ax = plt.subplots()
pa = ax.imshow(v1a,interpolation='nearest',cmap=cm.Reds)
cba = plt.colorbar(pa,shrink=0.25)
pb = ax.imshow(v1b,interpolation='nearest',cmap=cm.winter)
cbb = plt.colorbar(pb,shrink=0.25)
plt.xlabel('Day')
plt.ylabel('Depth')
cba.set_label('positive')
cbb.set_label('negative')
plt.show()

这篇关于同一imshow matplotlib中的两个不同的颜色颜色图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆