Seaborn热图颜色-值大小使用相同的颜色

时间:2020-04-08 20:21:38

标签: python seaborn

我的矩阵值从+1到-1,我希望+1和-1具有相同的颜色。有办法吗?

import matplotlib.pyplot as plt
import seaborn as sns
sns.set()

# Load the example flights dataset and convert to long-form
flights_long = sns.load_dataset("flights")
flights = flights_long.pivot("month", "year", "passengers")

# Draw a heatmap with the numeric values in each cell
f, ax = plt.subplots(figsize=(9, 6))
mv1 = np.mean(flights.mean());
sns.heatmap(flights- mv1, annot=True)

谢谢。

1 个答案:

答案 0 :(得分:0)

您可以使用seaborn的diverging_palette()通过为cmap的负端和正端传递相同的色相值来生成这样的cmap。

data = np.random.uniform(low=-1, high=1, size=(10,10))
cmap = sns.diverging_palette(240,240, as_cmap=True)
sns.heatmap(data, cmap=cmap)

enter image description here