【问题标题】:How to centre a colorbar at a defined value for seaborn heatmap?如何将颜色条居中于 seaborn 热图的定义值?
【发布时间】:2026-02-20 13:40:01
【问题描述】:

我正在尝试使用 seaborn 绘制热图。我的值从 0 到 5,我需要制作一个从蓝到红的色标,白色为 1,蓝色 - 低于 1,红色 - 从 1 到 5。

我该怎么做?

import seaborn as sns; sns.set()
hm=sns.heatmap(proportion, vmin=0, vmax=5, cmap='RdBu')

这是我尝试做的,但它不是定制的……“比例”是我的变量。有什么我可以做的吗?

【问题讨论】:

  • 你想要 3 种颜色还是渐变色?
  • 我肯定需要渐变...

标签: python matplotlib heatmap seaborn


【解决方案1】:

也许您的意思是使用 center 参数,

import matplotlib.pyplot as plt
import seaborn as sns
import numpy as np

data = np.random.rand(10,10)*5

ax = sns.heatmap(data, vmin=0,vmax=5,center=1,cmap="RdBu_r")

plt.show()

【讨论】:

  • 这太完美了!谢谢!