【问题标题】:Seaborn colormap shows numbers not exist in the dataSeaborn 颜色图显示数据中不存在数字
【发布时间】:2018-01-20 03:37:37
【问题描述】:

我试图使用 seaborn 的热图可视化数据框。 数据中的最小数字为-1.25,如下所示。但是,热图编码的范围为 -80(蓝色)。如何使颜色编码从 -1.25 开始为蓝色,到 97 为红色?

我尝试使用 vmax、vmin 和健壮的关键字,但它们没有帮助。

f, ax = plt.subplots(figsize=(8, 20))
                sns.heatmap(hill, annot=annot, linewidths=1, ax=ax, fmt='g', vmin=hill.values.min(), vmax=hill.values.max(), robust=True)
                plt.yticks(rotation=0)



hill.values.min()
Out[30]: -1.25
hill.values.max()
Out[31]: 96.899999999999991

【问题讨论】:

  • 在热图中使用hill.values.min(),而在后续计算中使用hill.min().min()。为什么?
  • @ason​​gtoruin 感谢您的指出。我正在尝试不同的方法来获得最小最大值。情节是一样的。我更新了帖子以使其保持一致。
  • 当您设置vminvmax 时,您有什么理由拥有robust=True?也许不用这个试试
  • @ason​​gtoruin 试过但没有用:(
  • 您能否提供hill 的样本,我认为它是一个数据框?

标签: python plot heatmap seaborn


【解决方案1】:

首先,您需要的功能是 seaborn 0.8 或更高版本中的默认功能。在这个版本中的代码

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

arr1 = np.linspace(-1.25,97,100).reshape(10,10)

sns.heatmap(arr1, cmap="coolwarm") 

plt.show()

生产

因此,一个简单的选择是更新您的 seaborn。


在低于 0.8 版的 seaborn 中,您可以通过使用 center 参数指定颜色图的中心来获得相同的行为。

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

arr1 = np.linspace(-1.25,97,100).reshape(10,10)

center = np.mean([arr1.min(), arr1.max()])
sns.heatmap(arr1, cmap="coolwarm", center=center, vmin=arr1.min(), vmax=arr1.max()) 

plt.show()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-08-14
    • 1970-01-01
    • 2021-11-12
    • 2018-09-20
    • 1970-01-01
    • 2018-04-28
    • 1970-01-01
    相关资源
    最近更新 更多