,我永遠不會改變的實際數據本身。這可能會導致很多混亂。 相反,正如我在評論中所說的,更改顏色表。這樣做的
方式都記錄在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()
產生
一般提示:永遠不要使用'jet'顏色表。在99.9%的情況下,有更好的選擇。 matplotlib 2中的新標準色彩圖是'viridis'。它更加令人愉悅,眼睛色彩友好,以更合乎邏輯的方式顯示數據,而不會像「噴氣式飛機」那樣產生錯誤含義。編輯:[這個視頻](https://www.youtube.com/watch?v=xAoljeRJ3lU)說爲什麼'噴氣'是壞的一切。 – Ian
感謝您的提示 - 我在這裏和那裏發現了這個聲明,而沒有付出太多的關注。我會給它一個! – mediantis
您是否閱讀過matplotlib網站上的[Colormap Normalization arcticle](http://matplotlib.org/users/colormapnorms.html)? – ImportanceOfBeingErnest