使用色图绘制数据后,滤除某些颜色

时间:2019-06-14 15:56:13

标签: python matplotlib colormap

使用以下代码,我仅使用默认“喷射”色图的上半部分(0.5到1)绘制了数据,色图的范围为0到1。 如果我希望数据仅显示0.7-1之间的颜色,怎么办?

  import numpy as np
  import matplotlib.pyplot as plt
  from matplotlib.colors import LinearSegmentedColormap
  np.random.seed(1)
  # Evaluate an existing colormap from 0.5 (midpoint) to 1 (upper end)
  cmap = plt.get_cmap('jet')
  colors = cmap(np.linspace(0.5, 1, cmap.N ))

  # Create a new colormap from those colors
  cmap2 = LinearSegmentedColormap.from_list('Upper Half', colors)

  z = np.random.random((4,4)) 

  fig, axes = plt.subplots(ncols=2)
  for ax, cmap in zip(axes.flat, [cmap, cmap2]):
      cax = ax.imshow(z, cmap=cmap, origin='lower')
      cbar = fig.colorbar(cax, ax=ax, orientation='horizontal')
      cbar.set_label(cmap.name)

  plt.show()

结果:

result

我想得到类似

expected result

1 个答案:

答案 0 :(得分:0)

您可以使用vminvmax参数。在名为vlst的列表中定义范围,左图为0-1,右图为0.7-1。

vlst = [[0, 1], [0.7, 1]]

fig, axes = plt.subplots(ncols=2)
for ax, cmap, v in zip(axes.flat, [cmap, cmap2], vlst):
    cax = ax.imshow(z, cmap=cmap, origin='lower',vmin=v[0], vmax=v[1])
    cbar = fig.colorbar(cax, ax=ax, orientation='horizontal')
    cbar.set_label(cmap.name)

plt.show()

enter image description here