2017-10-06 129 views
0

我借用從matplotlib定製CMAP示例頁面的示例:如何在matplotlib顏色條中創建自定義斷點?

https://matplotlib.org/examples/pylab_examples/custom_cmap.html

這產生相同的圖像具有不同數量的陰影的輪廓,如在段的數目指定:n_bins

https://matplotlib.org/_images/custom_cmap_00.png

但是,我不僅對桶的數量感興趣,而且還對顏色值之間的具體斷點感興趣。例如,當nbins=6右上角的插曲,我怎麼能指定的垃圾箱的範圍,以使得陰影填充這些自定義方面:

n_bins_ranges = ([-10,-5],[-5,-2],[-2,-0.5],[-0.5,2.5],[2.5,7.5],[7.5,10]) 

是否也可以指定休息的包容性點?例如,我想在-2和0.5之間的範圍內指定是-2 < x <= -0.5還是-2 <= x < -0.5

與回答以下編輯:

使用下面的接受的答案,這裏是繪製每一步,包括代碼最後加入定製的彩條蜱連線的中點處。請注意,由於我是新用戶,因此無法發佈圖片。

設置數據和6個色垃圾桶:

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

# Make some illustrative fake data: 
x = np.arange(0, np.pi, 0.1) 
y = np.arange(0, 2*np.pi, 0.1) 
X, Y = np.meshgrid(x, y) 
Z = np.cos(X) * np.sin(Y) * 10 

# Create colormap with 6 discrete bins 
colors = [(1, 0, 0), (0, 1, 0), (0, 0, 1)] # R -> G -> B 
n_bin = 6 
cmap_name = 'my_list' 
cm = matplotlib.colors.LinearSegmentedColormap.from_list(
     cmap_name, colors, N=n_bin) 

情節不同的選擇:

# Set up 4 subplots 
fig, axs = plt.subplots(2, 2, figsize=(6, 9)) 
fig.subplots_adjust(left=0.02, bottom=0.06, right=0.95, top=0.94, wspace=0.05) 

# Plot 6 bin figure 
im = axs[0,0].imshow(Z, interpolation='nearest', origin='lower', cmap=cm) 
axs[0,0].set_title("Original 6 Bin") 
fig.colorbar(im, ax=axs[0,0]) 

# Change the break points 
n_bins_ranges = [-10,-5,-2,-0.5,2.5,7.5,10] 
norm = matplotlib.colors.BoundaryNorm(n_bins_ranges, len(n_bins_ranges)) 
im = axs[0,1].imshow(Z, interpolation='nearest', origin='lower', cmap=cm, norm=norm) 
axs[0,1].set_title("Custom Break Points") 
fig.colorbar(im, ax=axs[0,1]) 

# Arrange color labels by data interval (not colors) 
im = axs[1,0].imshow(Z, interpolation='nearest', origin='lower', cmap=cm, norm=norm) 
axs[1,0].set_title("Linear Color Distribution") 
fig.colorbar(im, ax=axs[1,0], spacing="proportional") 

# Provide custom labels at color midpoints 
# And change inclusive equality by adding arbitrary small value 
n_bins_ranges_arr = np.asarray(n_bins_ranges)+1e-9 
norm = matplotlib.colors.BoundaryNorm(n_bins_ranges, len(n_bins_ranges)) 
n_bins_ranges_midpoints = (n_bins_ranges_arr[1:] + n_bins_ranges_arr[:-1])/2.0 
im = axs[1,1].imshow(Z, interpolation='nearest', origin='lower', cmap=cm ,norm=norm) 
axs[1,1].set_title("Midpoint Labels\n Switched Equal Sign") 
cbar=fig.colorbar(im, ax=axs[1,1], spacing="proportional", 
     ticks=n_bins_ranges_midpoints.tolist()) 
cbar.ax.set_yticklabels(['Red', 'Brown', 'Green 1','Green 2','Gray Blue','Blue']) 

plt.show() 
+0

你實際上並不意味着問題中回答你的問題(因爲如果你已經回答了這個問題就不會再有問題了,對嗎?)相反,你可以爲自己的問題提供一個答案。 – ImportanceOfBeingErnest

回答

1

可以使用BoundaryNorm如下:

import matplotlib.pyplot as plt 
import matplotlib.colors 
import numpy as np 
x = np.arange(0, np.pi, 0.1) 
y = np.arange(0, 2*np.pi, 0.1) 
X, Y = np.meshgrid(x, y) 
Z = np.cos(X) * np.sin(Y) * 10 

colors = [(1, 0, 0), (0, 1, 0), (0, 0, 1)] # R -> G -> B 
n_bin = 6 # Discretizes the interpolation into bins 
n_bins_ranges = [-10,-5,-2,-0.5,2.5,7.5,10] 
cmap_name = 'my_list' 
fig, ax = plt.subplots() 

# Create the colormap 
cm = matplotlib.colors.LinearSegmentedColormap.from_list(
      cmap_name, colors, N=n_bin) 
norm = matplotlib.colors.BoundaryNorm(n_bins_ranges, len(n_bins_ranges)) 
# Fewer bins will result in "coarser" colomap interpolation 
im = ax.imshow(Z, interpolation='nearest', origin='lower', cmap=cm, norm=norm) 
ax.set_title("N bins: %s" % n_bin) 
fig.colorbar(im, ax=ax) 

plt.show() 

或者,如果你想比例間隔,即顏色之間的距離根據它們的值,

fig.colorbar(im, ax=ax, spacing="proportional") 

enter image description here enter image description here

作爲boundary norm documentation狀態

如果b[i] <= v < b[i+1] 則v被映射到色彩焦耳;當我從0變到len(邊界)-2時,j從0變爲ncolors-1。

所以顏色總是選擇爲-2 <= x < -0.5,以獲得對方的等號,你將需要提供 像n_bins_ranges = np.array([-10,-5,-2,-0.5,2.5,7.5,10])-1e-9

+0

太棒了!感謝您傳遞文檔鏈接並建議添加(而不是減去,正確?)一個任意的小數字,以改變平等的一面。爲了繼續使顏色條數字線性間隔,我將如顏色條api中所討論的那樣通過規範參數'fig.colorbar(im,ax = ax,norm = norm)'? https://matplotlib.org/api/colorbar_api.html#matplotlib.colorbar。ColorbarBase – user8732262

+0

我更新了答案,我認爲你的意思是線性間距。 – ImportanceOfBeingErnest

相關問題