python seaborn 热图用绝对颜色框替换颜色条
python seaborn heatmap replace colorbar with absolute colorboxes
我有一个只包含绝对值 -1、0 和 1 的热图
import random
import numpy as np
import matplotlib
import seaborn as sb
import matplotlib.pyplot as plt
array = []
for x in range(10):
array.append(random.choices([-1,0, 1], k = 5))
array = np.array(array)
heatmap = sb.heatmap(array, cbar_kws={'ticks': [-1, 0 , 1]}, cmap = ["red", "grey", "green"])
plt.show()
是否可以删除颜色条并将其替换为三个不同颜色的框和一个自定义标签,就像您在条形图的图例中所期望的那样?
IE。一个红色框和旁边的单词“否”,一个灰色框和旁边的单词“N/A”和一个绿色框旁边的单词“是”
您可以使用 matplotlib 手动完成。为了创建框,我们为每种颜色绘制了一个带有正方形但没有数据的散点图,因此它们不会显示出来。我们将这些散点图的 return 值保存为传递给图例的句柄。然后我们从 heatmap
变量中获取 matplotlib figure
对象,它包含绘图所在的轴。在那里,我们创建了一个带有自定义手柄和标签的图例。
对该图调用 subplots_adjust
,我们为右边的图例腾出空间。
import random
import numpy as np
import matplotlib
import seaborn as sb
import matplotlib.pyplot as plt
array = []
for x in range(10):
array.append(random.choices([-1,0, 1], k = 5))
array = np.array(array)
colors = ["red", "grey", "green"]
heatmap = sb.heatmap(array, cmap = ["red", "grey", "green"], cbar=False)
#Create dummy handles using scatter
handles = [plt.scatter([], [], marker='s', s=50, color=color) for color in colors]
labels = [-1, 0 , 1]
#Creating the legend using dummy handles
heatmap.figure.legend(handles=handles, labels=labels, loc='center right', frameon=False)
#Adjusting the plot space to the right to make room for the legend
heatmap.figure.subplots_adjust(right=0.8)
plt.show()
旁注:
您可以用 numpy 函数替换生成随机数组的代码,这完全符合您的要求,但更方便。
所以替换这个:
array = []
for x in range(10):
array.append(random.choices([-1,0, 1], k = 5))
array = np.array(array)
有了这个:
array = np.random.choice((-1, 0, 1), (10, 5))
第一个参数是选项,第二个参数是数组的形状,所以在你的例子中是 10 x 5。
我有一个只包含绝对值 -1、0 和 1 的热图
import random
import numpy as np
import matplotlib
import seaborn as sb
import matplotlib.pyplot as plt
array = []
for x in range(10):
array.append(random.choices([-1,0, 1], k = 5))
array = np.array(array)
heatmap = sb.heatmap(array, cbar_kws={'ticks': [-1, 0 , 1]}, cmap = ["red", "grey", "green"])
plt.show()
是否可以删除颜色条并将其替换为三个不同颜色的框和一个自定义标签,就像您在条形图的图例中所期望的那样? IE。一个红色框和旁边的单词“否”,一个灰色框和旁边的单词“N/A”和一个绿色框旁边的单词“是”
您可以使用 matplotlib 手动完成。为了创建框,我们为每种颜色绘制了一个带有正方形但没有数据的散点图,因此它们不会显示出来。我们将这些散点图的 return 值保存为传递给图例的句柄。然后我们从 heatmap
变量中获取 matplotlib figure
对象,它包含绘图所在的轴。在那里,我们创建了一个带有自定义手柄和标签的图例。
对该图调用 subplots_adjust
,我们为右边的图例腾出空间。
import random
import numpy as np
import matplotlib
import seaborn as sb
import matplotlib.pyplot as plt
array = []
for x in range(10):
array.append(random.choices([-1,0, 1], k = 5))
array = np.array(array)
colors = ["red", "grey", "green"]
heatmap = sb.heatmap(array, cmap = ["red", "grey", "green"], cbar=False)
#Create dummy handles using scatter
handles = [plt.scatter([], [], marker='s', s=50, color=color) for color in colors]
labels = [-1, 0 , 1]
#Creating the legend using dummy handles
heatmap.figure.legend(handles=handles, labels=labels, loc='center right', frameon=False)
#Adjusting the plot space to the right to make room for the legend
heatmap.figure.subplots_adjust(right=0.8)
plt.show()
旁注:
您可以用 numpy 函数替换生成随机数组的代码,这完全符合您的要求,但更方便。
所以替换这个:
array = []
for x in range(10):
array.append(random.choices([-1,0, 1], k = 5))
array = np.array(array)
有了这个:
array = np.random.choice((-1, 0, 1), (10, 5))
第一个参数是选项,第二个参数是数组的形状,所以在你的例子中是 10 x 5。