seaborn clustermap:subplots_adjust 取消颜色条重新定位

2024-01-02

我正在尝试使用seaborn制作侧面带有颜色条的热图。但是,在我的实际应用程序案例中,我有很长的列名称需要轮换。这需要使用plt.subplots_adjust,否则标签不适合图像:

plt.setp(g.ax_heatmap.get_xticklabels(), rotation=90)
plt.subplots_adjust(bottom=0.5)

使用以下最小示例,我发现最后一个命令正在取消颜色条重新定位:

#!/usr/bin/env python3

import seaborn as sns
import matplotlib.pyplot as plt

iris = sns.load_dataset("iris")
species = iris.pop("species")
# Make long column names
iris.columns = ["________".join(label.split("_")) for label in iris.columns]


g = sns.clustermap(iris, col_cluster=False, yticklabels=False)
# Relocate colour bar on the side
g.cax.set_position([.15, .2, .03, .45])
plt.setp(g.ax_heatmap.get_xticklabels(), rotation=90)
plt.savefig("/tmp/unadjusted.png")

plt.cla()

g = sns.clustermap(iris, col_cluster=False, yticklabels=False)
g.cax.set_position([.15, .2, .03, .45])
plt.setp(g.ax_heatmap.get_xticklabels(), rotation=90)
# Adjust to have the column labels not cut out
plt.subplots_adjust(bottom=0.5)
plt.savefig("/tmp/adjusted.png")

这会产生以下图像:

Without plt.subplots_adjust:

With plt.subplots_adjust:

为什么会出现这种情况?

这是一个错误吗?

我该怎么做才能将颜色条放在我想要的位置,并且仍然确保我的旋转颜色标签不会被剪切?


根据建议@ImportanceOfBeingErnest https://stackoverflow.com/users/4124317/importanceofbeingernest,我尝试在移动颜色条之前进行调整。这导致了以下图像:

由于我实际上不需要树状图,而只是聚类,因此通过使用树状图框放置颜色条可以获得更令人满意的结果:

#!/usr/bin/env python3

import seaborn as sns
import matplotlib.pyplot as plt

iris = sns.load_dataset("iris")
species = iris.pop("species")
# Make long column names
iris.columns = ["________".join(label.split("_")) for label in iris.columns]

g = sns.clustermap(iris, col_cluster=False, yticklabels=False)
plt.setp(g.ax_heatmap.get_xticklabels(), rotation=90)
# Adjust to have the column labels not cut out
plt.subplots_adjust(bottom=0.5)
g.cax.set_position([.15, .2, .03, .45])
plt.savefig("/tmp/adjusted_before.png")

plt.cla()

g = sns.clustermap(iris, col_cluster=False, yticklabels=False)
plt.setp(g.ax_heatmap.get_xticklabels(), rotation=90)
# Adjust to have the column labels not cut out
plt.subplots_adjust(bottom=0.5)
# Remove the dendrogram (https://stackoverflow.com/a/42418968/1878788)
g.ax_row_dendrogram.set_visible(False)
# Use the dendrogram box to reposition the colour bar
dendro_box = g.ax_row_dendrogram.get_position()
dendro_box.x0 = (dendro_box.x0 + 2 * dendro_box.x1) / 3
g.cax.set_position(dendro_box)
# Move the ticks to the left (https://stackoverflow.com/a/36939552/1878788)
g.cax.yaxis.set_ticks_position("left")
# If we add a label to the colour bar
# (cbar_kws={"label" : "the_label"} in clustermap arguments)
# we also need to move the label
# g.cax.yaxis.set_label_position("left")
plt.savefig("/tmp/adjusted_before_using_row_dendrogram_box.png")

这会生成以下图像:


编辑:同样的问题发生在plt.tight_layout.

进一步开发我的热图,我想添加带有图例的行颜色(使用中提出的方法这个答案 https://stackoverflow.com/a/27992943/1878788)。在我的实际应用案例中,图例需要足够的水平空间才能从图像中剪切出来。

我想用plt.tight_layout调整图像。这再次干扰了颜色条的重新定位(甚至没有解决剪切图例问题...),如以下示例所示。

#!/usr/bin/env python3

import seaborn as sns
import matplotlib.pyplot as plt

iris = sns.load_dataset("iris")
species = iris.pop("species")
# Make long column names
iris.columns = ["________".join(label.split("_")) for label in iris.columns]


# Associating colours with species
species_list = species.unique()
label_colours = dict(zip(species_list, sns.color_palette("colorblind", len(species_list))))
row_colours = species.map(label_colours)

g = sns.clustermap(iris, col_cluster=False, yticklabels=False, row_colors=row_colours, cbar_kws={"label" : "measure"})
# Adding row colour legend
# https://stackoverflow.com/a/27992943/1878788
for (label, colour) in label_colours.items():
    # Make long label
    g.ax_col_dendrogram.bar(0, 0, color=colour, label="Iris___________________{}".format(label))
g.ax_col_dendrogram.legend(loc="center", ncol=3)
# Rotating column labels
plt.setp(g.ax_heatmap.get_xticklabels(), rotation=90)
# Adjust to have the column labels not cut out
plt.subplots_adjust(bottom=0.5)
# Remove the dendrogram (https://stackoverflow.com/a/42418968/1878788)
g.ax_row_dendrogram.set_visible(False)
# Use the dendrogram box to reposition the colour bar
dendro_box = g.ax_row_dendrogram.get_position()
dendro_box.x0 = (dendro_box.x0 + 2 * dendro_box.x1) / 3
g.cax.set_position(dendro_box)
# Move the ticks and labels to the left (https://stackoverflow.com/a/36939552/1878788)
g.cax.yaxis.set_ticks_position("left")
g.cax.yaxis.set_label_position("left")
plt.savefig("/tmp/with_row_colour_lengend.png")

plt.cla()

g = sns.clustermap(iris, col_cluster=False, yticklabels=False, row_colors=row_colours, cbar_kws={"label" : "measure"})
# Adding row colour legend
# https://stackoverflow.com/a/27992943/1878788
for (label, colour) in label_colours.items():
    # Make long label
    g.ax_col_dendrogram.bar(0, 0, color=colour, label="Iris___________________{}".format(label))
g.ax_col_dendrogram.legend(loc="center", ncol=3)
# Rotating column labels
plt.setp(g.ax_heatmap.get_xticklabels(), rotation=90)
# Adjust to have the column labels not cut out
plt.subplots_adjust(bottom=0.5)
# Remove the dendrogram (https://stackoverflow.com/a/42418968/1878788)
g.ax_row_dendrogram.set_visible(False)
# Use the dendrogram box to reposition the colour bar
dendro_box = g.ax_row_dendrogram.get_position()
dendro_box.x0 = (dendro_box.x0 + 2 * dendro_box.x1) / 3
g.cax.set_position(dendro_box)
# Move the ticks and labels to the left (https://stackoverflow.com/a/36939552/1878788)
g.cax.yaxis.set_ticks_position("left")
g.cax.yaxis.set_label_position("left")
plt.tight_layout()
plt.savefig("/tmp/tight_after.png")

plt.cla()

g = sns.clustermap(iris, col_cluster=False, yticklabels=False, row_colors=row_colours, cbar_kws={"label" : "measure"})
# Adding row colour legend
# https://stackoverflow.com/a/27992943/1878788
for (label, colour) in label_colours.items():
    # Make long label
    g.ax_col_dendrogram.bar(0, 0, color=colour, label="Iris___________________{}".format(label))
g.ax_col_dendrogram.legend(loc="center", ncol=3)
plt.tight_layout()
# Rotating column labels
plt.setp(g.ax_heatmap.get_xticklabels(), rotation=90)
# Adjust to have the column labels not cut out
plt.subplots_adjust(bottom=0.5)
# Remove the dendrogram (https://stackoverflow.com/a/42418968/1878788)
g.ax_row_dendrogram.set_visible(False)
# Use the dendrogram box to reposition the colour bar
dendro_box = g.ax_row_dendrogram.get_position()
dendro_box.x0 = (dendro_box.x0 + 2 * dendro_box.x1) / 3
g.cax.set_position(dendro_box)
# Move the ticks and labels to the left (https://stackoverflow.com/a/36939552/1878788)
g.cax.yaxis.set_ticks_position("left")
g.cax.yaxis.set_label_position("left")
plt.savefig("/tmp/tight_before.png")

结果如下:

Without plt.tight_layout,最右边的标签被切掉(物种应该是“virginica”):

Calling plt.tight_layout重新定位颜色条后:

Calling plt.tight_layout在重新定位颜色条之前:

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

seaborn clustermap:subplots_adjust 取消颜色条重新定位 的相关文章

随机推荐

  • 如何在 C# 中使用多个变量来锁定作用域

    我遇到的情况是 只有两个储物柜对象空闲时才应执行代码块 我希望会有类似的东西 lock a b this scope is in critical region 然而 似乎并没有这样的事情 那么这是否意味着做到这一点的唯一方法是 lock
  • MySQL 排列现有表列

    如何更改某个现有列的位置MySQL table 例如 我想移动该列username从其当前位置改为位于所有列之后 或者我希望它位于表中任何特定列之前 如果您愿意 可以更改列的顺序 如果您的用户名列是 varchar 255 那么 alter
  • Jenkins 作业终止后清理孤立的 docker 容器

    我在一个大型组织工作 该组织在共享 Jenkins 集群中运行数百个作业 我的 Jenkins 工作需要针对 Docker 容器内运行的不受信任的代码运行集成测试 我担心当我的 Jenkins 工作突然终止 例如工作中止或超时 时 我将留下
  • 为什么会话变量为空以导航下一页?

    我一直在工作一个网站 我已经处理了一段时间的问题 现在我知道为什么会发生这种情况 但不知道如何解决它 请帮忙 第 1 页 在第一页登录页面设置 SESSION user id 存储在数据库用户 ID 中获取的值 在同一页面中可以打印会话并且
  • 使用“location.href”取消页面卸载时出现“未知异常”

    我使用以下代码捕获 window onbeforeunload 事件 window onbeforeunload function evt if checkIsDirty var message If you continue your c
  • 通过单击按钮增加条形图值

    我正在尝试创建一个图表来显示锻炼的进度 每点击五个按钮就应在图表中添加一个勾号 这是其外观的示例 出于演示目的 我使用按钮单击 在生产中 轮子每转二十圈就会单击一次 private int counter 0 private void bu
  • Java中ArrayList的问题

    我在正确添加 ArrayList 时遇到问题 当我在 for 循环完成后打印 ArrayList 时 ArrayList 的长度是正确的 但每个元素都是相同的 创建的最后一个坐标 有人可以修复 并解释 下面的代码吗 public class
  • Python 中连续数据的箱线图

    我有一个包含 2 列的 csv 文件 col1 Timestamp数据 yyyy mm dd hh mm ss ms 8 个月数据 col2 热量数据 连续变量 由于有近 50k 记录 我想将 col1 timestamp col 划分为数
  • 如何从使用 writeToFile 创建的文件加载 NSDictionary?

    我有一个 NSMutableDictionary 我使用它编写的 stuff writeToFile TEST atomically YES 日后如何找回 另外 如果我决定用 4S 替换 iPhone 4 会发生什么情况 我的书面数据可以转
  • 用于监控/调整 memcached 运行状况的有用提示/工具有哪些?

    昨天 我发现了这个很酷的脚本 内存缓存顶部 https code google com p memcache top 它很好地打印出 memcached 的实时统计信息 看起来像 memcache top v0 6 default port
  • 将派生类强制转换为基类

    这里发生什么类型的演员表 在B get class A public A a 0 int a class B public A public A get return this is this C style cast int main B
  • 使用不透明类型(Char 和 Long)

    我正在尝试导出算法的 Scala 实现以在 JavaScript 中使用 我在用着 JSExport 该算法适用于 ScalaChar and Long值被标记为opaque in the 互操作性指南 http www scala js
  • 我如何在 xcode 中获取数组?

    如何获取字典的值而不是循环 我知道如何使用以下方法获取单个值 NSString valueStr dict objectForKey Key2 我需要通过循环所有键来获取 我需要在字典中搜索一个值 因为看起来您正在尝试使用NS词典 http
  • 迁移合约时耗尽 Gas

    我看过其他 没油了 的帖子 但他们没有解决我的问题 我正在使用 ganache cli 开始 ganache cli account 0xce2ddf7d4509856c2b7256d002c004db6e34eeb19b37cee04f7
  • 如何确定实体框架是否正在等待连接池中的连接?

    我看到一些间歇性的速度减慢 sql 超时错误 我无法确定原因 我已经拼凑了一些线索 但我需要一些帮助来确定可能的后续步骤 问题 我们有一个表 其中包含 10 多万条记录 我们从 Web 应用程序运行异步计数 该表经常被写入 有时计数需要 2
  • 安装svn:重启apache后出错

    我这样创建了我的存储库 须藤 svnadmin 创建 svn 重新启动 apache 后出现此错误 第 16 行语法错误 etc apache2 mods enabled dav svn conf 这里不允许 DAV dav svn con
  • 我尝试从此 HTML 中提取价格 2 890 000K 和 地址 有 12 个相同的 div class list items content list items content 1 div class list items conten
  • 如何使 Ember.js 与 Grails 控制器名称配合?

    Grails 非常强大 可以让您使用一条语句将 Domain 对象转换为 JSON object as JSON 不幸的是 由于一些原因 这不足以与 Ember js 交互 如何让 Grails 与 Ember js 完美配合 好问题 自我
  • 像 epub/ebook 一样显示动态 html 内容,而不将 html 转换为 epub 格式?

    我想创建一个响应式 移动优化的阅读体验 类似于 epub 电子书阅读器 如 Kindle 应用程序或 iBooks 使用动态 html 作为源 想象一下一篇长文章或博客文章需要大量垂直滚动才能阅读 尤其是在小型移动设备上 我想做的是将长页面
  • seaborn clustermap:subplots_adjust 取消颜色条重新定位

    我正在尝试使用seaborn制作侧面带有颜色条的热图 但是 在我的实际应用程序案例中 我有很长的列名称需要轮换 这需要使用plt subplots adjust 否则标签不适合图像 plt setp g ax heatmap get xti