【问题标题】:Python: Change/customise colour maps in SeabornPython:在 Seaborn 中更改/自定义颜色图
【发布时间】:2019-03-17 19:27:54
【问题描述】:

我花了太多时间研究这个,一些标签仍然在我的浏览器中打开: Link1Link2Link3Link4

我应该在工作!

无论如何,我的问题是:我使用其他人的脚本来生成大量热图,然后我必须对其进行审查和排序/分配:

这是一个例子: HM sample

我需要能够轻松地区分 0.03 和 0,但正如您所见,它们看起来几乎相同。理想的解决方案是:白色(仅零)-黄色-橙色-红色或白色(仅零)-橙色-红色

开发人员像这样使用“YlOrRd”:

sns.heatmap(heat_map, annot=True, fmt=".2g", cmap="YlOrRd", linewidths=0.5, 
                    linecolor='black', xticklabels=xticks, yticklabels=yticks
                    )

我尝试了一堆提供的标准/默认颜色映射选项,但无济于事。

我没有任何实际构建彩色地图的经验,我不想破坏已经在工作的东西。有人有什么想法吗?

谢谢

**由于它是工作产品,我可以发布的代码/示例受到限制。

【问题讨论】:

  • 是否保证每个热图至少包含一个0和一个1?与0 接近多少可以/需要黄色,例如如果0.004也显示为白色可以吗?
  • 不能说每个生成的热图都至少有一个 1 和 0,但是保留和使用的热图会。其次,0.01 将是非白色所需的最小值

标签: python seaborn heatmap colormap


【解决方案1】:

一种选择是从现有颜色图中获取颜色,将第一个颜色替换为白色,然后根据这些操作值创建新的颜色图。

import numpy as np; np.random.seed(42)
import matplotlib.pyplot as plt
import matplotlib.colors
import seaborn as sns

# some data
a = np.array([0.,0.002,.005,.0099,0.01,.0101,.02,.04,.24,.42,.62,0.95,.999,1.])
data = np.random.choice(a, size=(12,12))

# create colormap. We take 101 values equally spaced between 0 and 1
# hence the first value 0, second value 0.01
c = np.linspace(0,1,101)
# For those values we store the colors from the "YlOrRd" map in an array
colors = plt.get_cmap("YlOrRd",101)(c)
# We replace the first row of that array, by white
colors[0,:] = np.array([1,1,1,1])
# We create a new colormap with the colors
cmap = matplotlib.colors.ListedColormap(colors)

# Plot the heatmap. The format is set to 4 decimal places 
# to be able to disingush specifically the values ,.0099, .0100, .0101,
sns.heatmap(data, annot=True, fmt=".4f", cmap=cmap, vmin=0, vmax=1,
            linewidths=0.5,  linecolor='black')
plt.show()

【讨论】:

  • 很抱歉只收到您的答复通知。看起来不错,谢谢!明天上班试试。
  • 完美运行,tyty。
猜你喜欢
  • 2020-09-28
  • 2020-10-21
  • 2020-06-21
  • 2019-07-26
  • 2020-07-29
  • 2020-08-07
  • 1970-01-01
  • 2021-07-06
  • 1970-01-01
相关资源
最近更新 更多