简体   繁体   English

如何在 Seaborn 的 clustermap 树状图中指定线宽

[英]How to specify linewidth in Seaborn's clustermap dendrograms

Normally I would increase matplotlib's global linewidths by editing the matplotlib.rcParams.通常我会通过编辑 matplotlib.rcParams 来增加 matplotlib 的全局线宽。 This seems to work well directly with SciPy's dendrogram implementation but not with Seaborn's clustermap (which uses SciPy's dendrograms).这似乎直接适用于SciPy 的树状图实现,但不适用于Seaborn 的 clustermap (使用 SciPy 的树状图)。 Can anyone suggest a working method?任何人都可以建议一种工作方法吗?

import matplotlib
matplotlib.rcParams['lines.linewidth'] = 10
import seaborn as sns; sns.set()

flights = sns.load_dataset("flights")
flights = flights.pivot("month", "year", "passengers")
g = sns.clustermap(flights)

This has now been addressed in a more robust way by the following merged pull request https://github.com/mwaskom/seaborn/pull/1935 .现在已通过以下合并拉取请求https://github.com/mwaskom/seaborn/pull/1935以更强大的方式解决了这个问题。 I'm assuming it will be included in the release after v0.9.0.我假设它将包含在 v0.9.0 之后的版本中。

You can control the LineCollection properties of the dendrogram by using the tree_kws parameter.您可以使用tree_kws参数控制树状图的LineCollection属性。

For example:例如:

>>> import seaborn as sns
>>> iris = sns.load_dataset("iris")
>>> species = iris.pop("species")
>>> g = sns.clustermap(iris, tree_kws=dict(linewidths=1.5, colors=(0.2, 0.2, 0.4))

Would create a clustermap with 1.5 pt thick lines for the tree in an alternative dark purple color.将为树创建一个带有 1.5 pt 粗线的集群图,采用另一种深紫色。

There may be an easier way to do it, but this seems to work:可能有更简单的方法来做到这一点,但这似乎有效:

import matplotlib
import seaborn as sns; sns.set()

flights = sns.load_dataset("flights")
flights = flights.pivot("month", "year", "passengers")
g = sns.clustermap(flights)
for l in g.ax_row_dendrogram.lines:
        l.set_linewidth(10)
for l in g.ax_col_dendrogram.lines:
        l.set_linewidth(10)

Edit This no longer works in Seaborn v. 0.7.1 (and probably some earlier versions as well);编辑这不再适用于 Seaborn v. 0.7.1(可能还有一些早期版本); g.ax_col_dendrogram.lines now returns an empty list. g.ax_col_dendrogram.lines现在返回一个空列表。 I couldn't find a way to increase line width and I ended up temporarily modifying the Seaborn module.我找不到增加线宽的方法,最终临时修改了 Seaborn 模块。 In file matrix.py , function class _DendrogramPlotter , the linewidth is hard-coded as 0.5;在文件matrix.py ,函数class _DendrogramPlotter ,线宽被硬编码为 0.5; I modified it to 1.5:我将其修改为 1.5:

line_kwargs = dict(linewidths=1.5, colors='k')

This worked but obviously isn't a very sustainable approach.这有效,但显然不是一种非常可持续的方法。

for newer versions of seaborn (tested with 0.7.1, 0.9.0), the lines are in a LineCollection, rather than by themselves.对于较新版本的 seaborn(使用 0.7.1、0.9.0 测试),这些行位于 LineCollection 中,而不是它们本身。 So their width can be changed as follows:所以它们的宽度可以改变如下:

import seaborn as sns
import matplotlib.pyplot as plt

# load data and make clustermap
df = sns.load_dataset('iris')
g = sns.clustermap(df[['sepal_length', 'sepal_width']])

for a in g.ax_row_dendrogram.collections:
    a.set_linewidth(10)

for a in g.ax_col_dendrogram.collections:
    a.set_linewidth(10)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM