2016-10-28 106 views
1

我目前正在繪製在X,Y軸imshow一些標籤,但點的95%以上靜坐在0-0.2範圍內,而少於10%位於0.2-1.0的範圍內。使用默認的'噴射'色彩映射,這導致幾乎所有的圖都顯示爲藍色,儘管95%的數據在視覺上不可觀察。Matplotlib imshow - 「加快」在一定值的顏色變化範圍

有沒有辦法告訴matplotlib,例如,四倍於它的顏色在0.0-0.1範圍內變化率,並縮放其餘0.2-1.0範圍會相應?任何幫助將不勝感激。

提前致謝!

編輯:看到在這只是一個視覺表示,我意識到,我有一個選項,而不是重新規模0.2範圍內的數據下降到我認爲合適的任何值,以便更改可見,然後相應地手動創建顏色條。如果可能的話,我仍然希望能夠使matplotlib的imshow在本地執行此操作。如果您想在圖像劇情emphazise數據中的較小值

+2

一般提示:永遠不要使用'jet'顏色表。在99.9%的情況下,有更好的選擇。 matplotlib 2中的新標準色彩圖是'viridis'。它更加令人愉悅,眼睛色彩友好,以更合乎邏輯的方式顯示數據,而不會像「噴氣式飛機」那樣產生錯誤含義。編輯:[這個視頻](https://www.youtube.com/watch?v=xAoljeRJ3lU)說爲什麼'噴氣'是壞的一切。 – Ian

+0

感謝您的提示 - 我在這裏和那裏發現了這個聲明,而沒有付出太多的關注。我會給它一個! – mediantis

+0

您是否閱讀過matplotlib網站上的[Colormap Normalization arcticle](http://matplotlib.org/users/colormapnorms.html)? – ImportanceOfBeingErnest

回答

1

,我永遠不會改變的實際數據本身。這可能會導致很多混亂。 相反,正如我在評論中所說的,更改顏色表。這樣做的

方式都記錄在Matplotlib Color Normalization Tutorial還有這裏的SO。特別是this article以及其中的答案真的可以說明其中的可能性。

我在下面的例子中結合了兩個概念來顯示選項。

  • 一個是重新調整顏色映射,使最初位於顏色映射中間(midpoint)的值向下移動。通過這種方式,在0和新的midpoint之間增加了更多的變化,而上述所有內容都得到了延伸。人們可以將其看作拼接在一起的兩個線性色彩貼圖。
  • 另一種是簡單地使用顏色的對數縮放。

這是示例代碼

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

產生

enter image description here