Seaborn Heatmap Custom colormap(海运热图自定义色彩映射表)
本文介绍了海运热图自定义色彩映射表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
您好,我刚刚为海运热图创建了自定义Cmap,但当我想要使用它时,它不会显示正确的颜色。我已经一步步做到了:import seaborn as sns
import numpy as np
import matplotlib
import matplotlib.pyplot as plt
matrix = np.array([[149030, 34],[7442, 12]])
norm = matplotlib.colors.Normalize(matrix.min(), matrix.max())
boundaries = [value for value in matrix.flatten().tolist()]
list.sort(boundaries)
colors = [[norm(boundaries[0]), "#90AFC5"],
[norm(boundaries[1]), "#336B87"],
[norm(boundaries[2]), "#2a3132"],
[norm(boundaries[3]), "#763626"]]
cmap = matplotlib.colors.LinearSegmentedColormap.from_list("", colors)
fig = plt.figure(figsize=(6, 6))
ax = plt.subplot()
annot = np.array([[f"{matrix[0,0]}", f"{matrix[0,1]}"],
[f"{matrix[1,0]}", f"{matrix[1,1]}"]], dtype=object)
sns.heatmap(matrix,
annot=annot,
annot_kws={"size": 11},
fmt="",
ax=ax,
vmin=matrix.min(),
vmax=matrix.max(),
cmap=cmap,
cbar=True,
cbar_kws={'format': '%.0f%%', 'ticks': boundaries, 'drawedges': True},
xticklabels=False,
yticklabels=False)
我的输出如您所见,有两个蓝色列,但我定义了不同的颜色:
推荐答案
如果使用BoundaryNorm
,则可以为边界之间的范围指定颜色。要获得4个范围,您需要5个边界。一种方法是在末端添加一个额外的边界。在这个问题中,不清楚您希望如何处理与边界不一致的颜色值。在下面的代码中,颜色用于边界值和直到下一个边界的范围。
import seaborn as sns
import numpy as np
import matplotlib
import matplotlib.pyplot as plt
matrix = np.array([[149030, 34], [7442, 12]])
boundaries = [value for value in matrix.flatten().tolist()]
list.sort(boundaries)
colors = ["#90AFC5", "#336B87", "#2a3132", "#763626"]
norm = matplotlib.colors.BoundaryNorm(boundaries=boundaries + [boundaries[-1]], ncolors=256)
cmap = matplotlib.colors.LinearSegmentedColormap.from_list("", colors)
fig = plt.figure(figsize=(6, 6))
ax = plt.subplot()
annot = np.array([[f"{matrix[0, 0]}", f"{matrix[0, 1]}"],
[f"{matrix[1, 0]}", f"{matrix[1, 1]}"]], dtype=object)
sns.heatmap(matrix,
annot=annot,
annot_kws={"size": 11},
fmt="",
ax=ax,
cmap=cmap,
norm=norm,
cbar=True,
cbar_kws={'format': '%.0f%%', 'ticks': boundaries, 'drawedges': True},
xticklabels=False,
yticklabels=False)
plt.show()
这篇关于海运热图自定义色彩映射表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
沃梦达教程
本文标题为:海运热图自定义色彩映射表


基础教程推荐
猜你喜欢
- 包装空间模型 2022-01-01
- 无法导入 Pytorch [WinError 126] 找不到指定的模块 2022-01-01
- 修改列表中的数据帧不起作用 2022-01-01
- 求两个直方图的卷积 2022-01-01
- 在同一图形上绘制Bokeh的烛台和音量条 2022-01-01
- PermissionError: pip 从 8.1.1 升级到 8.1.2 2022-01-01
- Plotly:如何设置绘图图形的样式,使其不显示缺失日期的间隙? 2022-01-01
- PANDA VALUE_COUNTS包含GROUP BY之前的所有值 2022-01-01
- 使用大型矩阵时禁止 Pycharm 输出中的自动换行符 2022-01-01
- 在Python中从Azure BLOB存储中读取文件 2022-01-01