Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vue.js/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Matplotlib imshow-';加速';在某些值范围内的颜色变化_Python_Matplotlib_Imshow - Fatal编程技术网

Python Matplotlib imshow-';加速';在某些值范围内的颜色变化

Python Matplotlib imshow-';加速';在某些值范围内的颜色变化,python,matplotlib,imshow,Python,Matplotlib,Imshow,我目前正在imshow中绘制x、y轴上的一些标签,但95%以上的点位于0-0.2范围内,而不到10%的点位于0.2-1.0范围内。使用默认的“jet”颜色图,几乎所有的图都显示为蓝色,即使95%的数据存在差异,视觉上无法观察到 例如,是否有方法告诉matplotlib将颜色在0.0-0.1范围内的变化率增加四倍,并相应地缩放剩余的0.2-1.0范围?任何帮助都将不胜感激 提前谢谢 编辑:看到这只是一种视觉表现,我意识到我的一个选择是将0.2范围内的数据重新缩放到我认为合适的任何值,以使更改更可见

我目前正在imshow中绘制x、y轴上的一些标签,但95%以上的点位于0-0.2范围内,而不到10%的点位于0.2-1.0范围内。使用默认的“jet”颜色图,几乎所有的图都显示为蓝色,即使95%的数据存在差异,视觉上无法观察到

例如,是否有方法告诉matplotlib将颜色在0.0-0.1范围内的变化率增加四倍,并相应地缩放剩余的0.2-1.0范围?任何帮助都将不胜感激

提前谢谢


编辑:看到这只是一种视觉表现,我意识到我的一个选择是将0.2范围内的数据重新缩放到我认为合适的任何值,以使更改更可见,然后手动创建相应的颜色栏。如果可能的话,我仍然希望matplotlib的imshow能够以本机方式执行此操作。

如果您希望在图像打印中强调数据中的小值,我永远不会更改实际数据本身。这会导致很多混乱。 相反,正如我在评论中所说的,更改颜色映射

这样做的方法记录在中以及此处等。特别是其中的答案确实说明了一个人所拥有的可能性

我在下面的示例中结合了两个概念来显示选项

  • 一种是重新缩放颜色贴图,使最初位于颜色贴图中间(
    中点
    )的值下移。通过这种方式,在
    0
    和新的
    中点之间添加了更多的变化,而上面的所有内容都被拉伸。我们可以将其视为两个线性彩色贴图拼接在一起
  • 另一种是简单地使用颜色的对数缩放
这是示例代码

import numpy as np
import matplotlib
import matplotlib.pyplot as plt
import matplotlib.colors as colors


def shiftedColorMap(cmap, start=0, midpoint=0.5, stop=1.0, name='shiftedcmap'):
    '''
    function taken from
    https://stackoverflow.com/questions/7404116/...
        ...defining-the-midpoint-of-a-colormap-in-matplotlib
    Function to offset the "center" of a colormap. Useful for
    data with a negative min and positive max and you want the
    middle of the colormap's dynamic range to be at zero

    Input
    -----
      cmap : The matplotlib colormap to be altered
      start : Offset from lowest point in the colormap's range.
          Defaults to 0.0 (no lower ofset). Should be between
          0.0 and `midpoint`.
      midpoint : The new center of the colormap. Defaults to 
          0.5 (no shift). Should be between 0.0 and 1.0. In
          general, this should be  1 - vmax/(vmax + abs(vmin))
          For example if your data range from -15.0 to +5.0 and
          you want the center of the colormap at 0.0, `midpoint`
          should be set to  1 - 5/(5 + 15)) or 0.75
      stop : Offset from highets point in the colormap's range.
          Defaults to 1.0 (no upper ofset). Should be between
          `midpoint` and 1.0.
    '''
    cdict = {  'red': [],  'green': [], 'blue': [],  'alpha': []  }

    # regular index to compute the colors
    reg_index = np.linspace(start, stop, 257)

    # shifted index to match the data
    shift_index = np.hstack([
        np.linspace(0.0, midpoint, 128, endpoint=False), 
        np.linspace(midpoint, 1.0, 129, endpoint=True)
    ])

    for ri, si in zip(reg_index, shift_index):
        r, g, b, a = cmap(ri)

        cdict['red'].append((si, r, r))
        cdict['green'].append((si, g, g))
        cdict['blue'].append((si, b, b))
        cdict['alpha'].append((si, a, a))

    newcmap = matplotlib.colors.LinearSegmentedColormap(name, cdict)
    plt.register_cmap(cmap=newcmap)

    return newcmap


x = np.linspace(-3, 3, num=601)
X,Y = np.meshgrid(x,x)
Z = np.sinc( (X*np.cos(1)+Y*np.sin(1))**2 +(-X*np.sin(1)+0.2*Y*np.cos(1))**2 )**2 

orig_cmap = matplotlib.cm.viridis 
shifted_cmap = shiftedColorMap(orig_cmap, midpoint=0.05, name='shifted')


fig = plt.figure(figsize=(4,9))
ax = [fig.add_subplot(3,1,n+1) for n in range(3)]

# normal cmap
im0 = ax[0].imshow(Z, interpolation="none", cmap=orig_cmap)
fig.colorbar(im0, ax=ax[0])
ax[0].set_title('Default behavior (hard to see small values)', fontsize=10)

#example using the custom shiftedColorMap function
#taken from https://stackoverflow.com/questions/7404116/defining-the-midpoint-of-a-colormap-in-matplotlib
im1 = ax[1].imshow(Z, interpolation="none", cmap=shifted_cmap)
fig.colorbar(im1, ax=ax[1])
ax[1].set_title('Center of colormap shifted to 0.05', fontsize=10)

#example using colors.LogNorm()
#taken from http://matplotlib.org/users/colormapnorms.html
im2 = ax[2].imshow(Z, interpolation="none", norm=colors.LogNorm(vmin=10e-5, vmax=Z.max()), cmap=orig_cmap)
fig.colorbar(im2, ax=ax[2])
ax[2].set_title('Logarithmically scaled Colormap', fontsize=10)

for axis in ax:
    axis.set_yticks([])
    axis.set_xticks([])
plt.tight_layout()    
plt.show() 
生产


一般提示:永远不要使用
jet
colormap。在99.9%的情况下,有更好的选择。matplotlib 2中的新标准颜色映射是
viridis
。它更加美观,色盲友好,并且以一种更合理的方式显示数据,没有像jet那样的错误含义。编辑:讲述了为什么喷气式飞机不好的原因。谢谢你的提示-我在这里和那里都看到了这句话,没有太多注意。我要试一试!你读过matplotlib网站上的吗?@ImportanceOfBeingErnest我读过,但无法从中找出如何重新缩放数据,同时仍在颜色栏上正确显示原始值。我不确定我是否理解你的问题,但颜色贴图标准化的想法不是改变你的数据,而是改变颜色贴图,例如,达到对数刻度。