离散颜色条中颜色的自定义间距
Custom Spacing for colors in discrete colorbar
本质上,我想让 python matplotlib/seaborn 中的离散二进制颜色条具有自定义间距,以便一种颜色比另一种颜色占据更多的颜色条。
我正在使用 seaborn 热图来绘制我拥有的一些二进制数据。每行包含 p 个不同的项目,这些项目由我的二元分类器标记。十一行中的四行属于 Class1,另外 7 行属于 Class0。我想让颜色条帮助说明该细分,以便颜色条的 4/11 颜色与 Class1 相同。
# make colormap
yellow = (249/255, 231/255, 85/255)
blue = (62/255,11/255, 81/255)
color_list = [yellow, blue]
cmap = ListedColormap(color_list)
# plot data
h = sns.heatmap(binary_preds, cmap=cmap, cbar_kws = dict(use_gridspec=False,location="left"))
for i in range(len(binary_preds) + 1):
h.axhline(i, color='white', lw=5)
colorbar = h.collections[0].colorbar
colorbar.set_ticks([.25,.75])
colorbar.set_ticklabels(['Class0', 'Class1'])
## code I would like:
# colorbar.set_spacing([0.37, 63])
生成的颜色栏:
我希望如何(手动调整颜色条间距):
以下方法对颜色条使用 BoundaryNorm and proportional spacing:
from matplotlib import pyplot as plt
from matplotlib.colors import BoundaryNorm, ListedColormap
import numpy as np
import seaborn as sns
# make colormap
yellow = (249 / 255, 231 / 255, 85 / 255)
blue = (62 / 255, 11 / 255, 81 / 255)
color_list = [yellow, blue]
cmap = ListedColormap(color_list)
# define a boundary norm
proportion_class0 = 0.67 # proportion for class0
norm = BoundaryNorm([0, proportion_class0, 1], 2)
binary_preds = np.random.choice([False, True], size=(10, 15), p=[proportion_class0, 1 - proportion_class0])
ax = sns.heatmap(binary_preds, cmap=cmap, norm=norm,
cbar_kws=dict(use_gridspec=False, location="left", spacing="proportional"))
for i in range(len(binary_preds) + 1):
ax.axhline(i, color='white', lw=5)
colorbar = ax.collections[0].colorbar
colorbar.set_ticks([proportion_class0 / 2, (1 + proportion_class0) / 2])
colorbar.set_ticklabels(['Class0', 'Class1'])
plt.show()
本质上,我想让 python matplotlib/seaborn 中的离散二进制颜色条具有自定义间距,以便一种颜色比另一种颜色占据更多的颜色条。
我正在使用 seaborn 热图来绘制我拥有的一些二进制数据。每行包含 p 个不同的项目,这些项目由我的二元分类器标记。十一行中的四行属于 Class1,另外 7 行属于 Class0。我想让颜色条帮助说明该细分,以便颜色条的 4/11 颜色与 Class1 相同。
# make colormap
yellow = (249/255, 231/255, 85/255)
blue = (62/255,11/255, 81/255)
color_list = [yellow, blue]
cmap = ListedColormap(color_list)
# plot data
h = sns.heatmap(binary_preds, cmap=cmap, cbar_kws = dict(use_gridspec=False,location="left"))
for i in range(len(binary_preds) + 1):
h.axhline(i, color='white', lw=5)
colorbar = h.collections[0].colorbar
colorbar.set_ticks([.25,.75])
colorbar.set_ticklabels(['Class0', 'Class1'])
## code I would like:
# colorbar.set_spacing([0.37, 63])
生成的颜色栏:
我希望如何(手动调整颜色条间距):
以下方法对颜色条使用 BoundaryNorm and proportional spacing:
from matplotlib import pyplot as plt
from matplotlib.colors import BoundaryNorm, ListedColormap
import numpy as np
import seaborn as sns
# make colormap
yellow = (249 / 255, 231 / 255, 85 / 255)
blue = (62 / 255, 11 / 255, 81 / 255)
color_list = [yellow, blue]
cmap = ListedColormap(color_list)
# define a boundary norm
proportion_class0 = 0.67 # proportion for class0
norm = BoundaryNorm([0, proportion_class0, 1], 2)
binary_preds = np.random.choice([False, True], size=(10, 15), p=[proportion_class0, 1 - proportion_class0])
ax = sns.heatmap(binary_preds, cmap=cmap, norm=norm,
cbar_kws=dict(use_gridspec=False, location="left", spacing="proportional"))
for i in range(len(binary_preds) + 1):
ax.axhline(i, color='white', lw=5)
colorbar = ax.collections[0].colorbar
colorbar.set_ticks([proportion_class0 / 2, (1 + proportion_class0) / 2])
colorbar.set_ticklabels(['Class0', 'Class1'])
plt.show()