2016-02-29 80 views

回答

3

是的,但是就你而言,製作一個在藍色和紅色之間插值的色彩圖可能更容易。

例如:

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

cmap = LinearSegmentedColormap.from_list('name', ['red', 'blue']) 

fig, ax = plt.subplots() 
im = ax.imshow(np.random.random((10, 10)), cmap=cmap) 
fig.colorbar(im) 
plt.show() 

enter image description here

注意,如果你想紅的陰影不是一個HTML顏色的名稱,你可以替換的確切RGB值。

然而,如果你確實想「切出中間的」另一種顏色表,你就應該評估它,不包括中間的範圍,並創建一個新的顏色表:

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

# Remove the middle 40% of the RdBu_r colormap 
interval = np.hstack([np.linspace(0, 0.3), np.linspace(0.7, 1)]) 
colors = plt.cm.RdBu_r(interval) 
cmap = LinearSegmentedColormap.from_list('name', colors) 

# Plot a comparison of the two colormaps 
fig, axes = plt.subplots(ncols=2) 
data = np.random.random((10, 10)) 

im = axes[0].imshow(data, cmap=plt.cm.RdBu_r, vmin=0, vmax=1) 
fig.colorbar(im, ax=axes[0], orientation='horizontal', ticks=[0, 0.5, 1]) 
axes[0].set(title='Original Colormap') 

im = axes[1].imshow(data, cmap=cmap, vmin=0, vmax=1) 
fig.colorbar(im, ax=axes[1], orientation='horizontal', ticks=[0, 0.5, 1]) 
axes[1].set(title='New Colormap') 

plt.show() 

enter image description here