我正在使用 seaborn 并有一个热图。如何使用离散范围内的特定颜色创建颜色图?
I am using seaborn and have a heatmap. How do I create a colormap with specific colors from a discrete range?
我正在制作 seaborn 热图,我想指定具有以下范围的离散颜色图:
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
绘制颜色条
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()
输出:
我正在制作 seaborn 热图,我想指定具有以下范围的离散颜色图:
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
绘制颜色条
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()
输出: