【问题标题】:Two different color colormaps in the same imshow matplotlib同一个 imshow matplotlib 中的两个不同颜色的颜色图
【发布时间】:2014-04-03 09:40:54
【问题描述】:

假设下面的例子

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()

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

【问题讨论】:

标签: python matplotlib color-mapping imshow


【解决方案1】:

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

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()

如果你真的想使用两个不同的颜色图,这是一个带有掩码数组的解决方案:

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()

【讨论】:

  • 是的,我确实需要两个不同的颜色图。我想知道通过这种方法是否可以只为两个颜色图绘制一个调色板。
  • 如果我们将它们放在网格中,两个调色板可以放在一起(一个在另一个之上)。 Here 就是一个例子。为了实现我上面的要求,我们必须创建inner grids
猜你喜欢
  • 1970-01-01
  • 2015-12-22
  • 1970-01-01
  • 2021-12-28
  • 2021-07-30
  • 1970-01-01
  • 2011-08-15
  • 2020-11-05
  • 1970-01-01
相关资源
最近更新 更多