我正在尋找一種方法來爲matplotlib
獲取適當的顏色高程圖。等效於Matlab的Demcmap的Python(高程+/-正確的顏色映射)
cmap'terrain
'看起來不錯,但顏色標註不是基於零(即如果標尺爲0-> 5000m,則0-> 1000m範圍可能是藍色陰影,您將假定爲低於海平面)
Matlab的功能相當於是: demcmap
什麼是讓matplotlib圍繞轉變地形顏色表的綠色/棕色和藍色的最佳途徑零電場標記?
我正在尋找一種方法來爲matplotlib
獲取適當的顏色高程圖。等效於Matlab的Demcmap的Python(高程+/-正確的顏色映射)
cmap'terrain
'看起來不錯,但顏色標註不是基於零(即如果標尺爲0-> 5000m,則0-> 1000m範圍可能是藍色陰影,您將假定爲低於海平面)
Matlab的功能相當於是: demcmap
什麼是讓matplotlib圍繞轉變地形顏色表的綠色/棕色和藍色的最佳途徑零電場標記?
不幸的是,matplotlib不提供Matlab的功能demcmap
。 python basemap
包中實際上可能有一些內置功能,但我不知道。
所以,堅持matplotlib機載選項,我們可以繼承Normalize
的子類來建立一個以色彩地圖中間點爲中心的顏色標準化。這項技術可以在StackOverflow的another question中找到,並根據具體需要進行調整,即設置一個sealevel
(可能最好選擇爲0
),並將顏色地圖col_val
(範圍在0和1之間)的值設置爲該海平面應該對應的值。在地形圖的情況下,似乎0.22
,對應turqoise顏色,可能是一個不錯的選擇。
然後可以將Normalize實例作爲參數提供給imshow
。可以在圖片的第一行看到結果數字。
由於圍繞海平面的平穩過渡,0
左右的值出現在turqoise顏色,使得很難區分陸地和海洋。
因此,我們可以稍微更改一下地形圖並剪掉這些顏色,以便海岸線更清晰可見。這是通過地圖的combining two parts完成的,範圍從0到0.17和從0.25到1,從而切掉了它的一部分。
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.colors
class FixPointNormalize(matplotlib.colors.Normalize):
"""
Inspired by https://stackoverflow.com/questions/20144529/shifted-colorbar-matplotlib
Subclassing Normalize to obtain a colormap with a fixpoint
somewhere in the middle of the colormap.
This may be useful for a `terrain` map, to set the "sea level"
to a color in the blue/turquise range.
"""
def __init__(self, vmin=None, vmax=None, sealevel=0, col_val = 0.21875, clip=False):
# sealevel is the fix point of the colormap (in data units)
self.sealevel = sealevel
# col_val is the color value in the range [0,1] that should represent the sealevel.
self.col_val = col_val
matplotlib.colors.Normalize.__init__(self, vmin, vmax, clip)
def __call__(self, value, clip=None):
x, y = [self.vmin, self.sealevel, self.vmax], [0, self.col_val, 1]
return np.ma.masked_array(np.interp(value, x, y))
# Combine the lower and upper range of the terrain colormap with a gap in the middle
# to let the coastline appear more prominently.
# inspired by https://stackoverflow.com/questions/31051488/combining-two-matplotlib-colormaps
colors_undersea = plt.cm.terrain(np.linspace(0, 0.17, 56))
colors_land = plt.cm.terrain(np.linspace(0.25, 1, 200))
# combine them and build a new colormap
colors = np.vstack((colors_undersea, colors_land))
cut_terrain_map = matplotlib.colors.LinearSegmentedColormap.from_list('cut_terrain', colors)
# invent some data (height in meters relative to sea level)
data = np.linspace(-1000,2400,15**2).reshape((15,15))
# plot example data
fig, ax = plt.subplots(nrows = 2, ncols=3, figsize=(11,6))
plt.subplots_adjust(left=0.08, right=0.95, bottom=0.05, top=0.92, hspace = 0.28, wspace = 0.15)
plt.figtext(.5, 0.95, "Using 'terrain' and FixedPointNormalize", ha="center", size=14)
norm = FixPointNormalize(sealevel=0, vmax=3400)
im = ax[0,0].imshow(data+1000, norm=norm, cmap=plt.cm.terrain)
fig.colorbar(im, ax=ax[0,0])
norm2 = FixPointNormalize(sealevel=0, vmax=3400)
im2 = ax[0,1].imshow(data, norm=norm2, cmap=plt.cm.terrain)
fig.colorbar(im2, ax=ax[0,1])
norm3 = FixPointNormalize(sealevel=0, vmax=0)
im3 = ax[0,2].imshow(data-2400.1, norm=norm3, cmap=plt.cm.terrain)
fig.colorbar(im3, ax=ax[0,2])
plt.figtext(.5, 0.46, "Using custom cut map and FixedPointNormalize (adding hard edge between land and sea)", ha="center", size=14)
norm4 = FixPointNormalize(sealevel=0, vmax=3400)
im4 = ax[1,0].imshow(data+1000, norm=norm4, cmap=cut_terrain_map)
fig.colorbar(im4, ax=ax[1,0])
norm5 = FixPointNormalize(sealevel=0, vmax=3400)
im5 = ax[1,1].imshow(data, norm=norm5, cmap=cut_terrain_map)
cbar = fig.colorbar(im5, ax=ax[1,1])
norm6 = FixPointNormalize(sealevel=0, vmax=0)
im6 = ax[1,2].imshow(data-2400.1, norm=norm6, cmap=cut_terrain_map)
fig.colorbar(im6, ax=ax[1,2])
for i, name in enumerate(["land only", "coast line", "sea only"]):
for j in range(2):
ax[j,i].text(0.96,0.96,name, ha="right", va="top", transform=ax[j,i].transAxes, color="w")
plt.show()
的matplotlib顏色表應該如何知道哪些數據值在情節屬於海平面? – ImportanceOfBeingErnest
@ImportanceOfBeingErnest海平面是0米,因此我說爲什麼要在零高程圖周圍移動顏色表 – mjp