[英]I am using seaborn and have a heatmap. How do I create a colormap with specific colors from a discrete range?
我正在制作 seaborn 熱量 map 並且我想指定具有這些范圍的離散顏色圖:
under 40 = dark green
40 - 70 = light green
70 - 130 = white
130 - 165 = light red
165 and over = dark red
我制作了一個具有正確邊界的顏色條:
fig, ax = plt.subplots(figsize=(6, 1))
fig.subplots_adjust(bottom=0.5)
cmap = mpl.colors.ListedColormap(['darkolivegreen', 'yellowgreen', 'white', 'lightcoral','darkred'])
#cmap.set_over('0.25')
#cmap.set_under('0.75')
bounds = [0, 40, 70, 130,165,310]
norm = mpl.colors.BoundaryNorm(bounds, cmap.N)
cb2 = mpl.colorbar.ColorbarBase(ax, cmap=cmap,
norm=norm,
#boundaries=[0] + bounds + [13],
extend='both',
ticks=bounds,
spacing='proportional',
orientation='horizontal')
cb2.set_label('Discrete intervals, some other units')
fig.show()
我現在的問題是如何定義這個帶有邊界的顏色條作為我的 seaborn 熱圖的新顏色圖?
我試過這個,但邊界不存在,顏色圖被均勻調整,而不是使用特定的邊界。
ax = sns.heatmap(selected,cmap=cmap)
plt.ylabel('Patient')
plt.xlabel('Gene')
#ax.set_yticks(np.arange(0,61,1))
plt.show()
如何根據我定義的新顏色條獲得正確的顏色圖?
您可以在繪制熱圖后嘗試使用plt.colorbar
到 plot 彩條
fig, ax = plt.subplots(figsize=(6, 9))
fig.subplots_adjust(bottom=0.5)
# define the color map
cmap = mpl.colors.ListedColormap(['darkolivegreen', 'yellowgreen', 'white', 'lightcoral','darkred'])
bounds = [0, 40, 70, 130,165,310]
norm = mpl.colors.BoundaryNorm(bounds, cmap.N)
# plot heatmap with `imshow`
cb = ax.imshow(selected,cmap=cmap, norm=norm)
# plot colorbar
cb2 = plt.colorbar(cb, #boundaries=[0] + bounds + [13],
extend='both',
ticks=bounds,
spacing='proportional',
orientation='horizontal')
cb2.set_label('Discrete intervals, some other units')
fig.show()
Output:
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.