簡體   English   中英

如何在 Seaborn 的 clustermap 樹狀圖中指定線寬

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

通常我會通過編輯 matplotlib.rcParams 來增加 matplotlib 的全局線寬。 這似乎直接適用於SciPy 的樹狀圖實現,但不適用於Seaborn 的 clustermap (使用 SciPy 的樹狀圖)。 任何人都可以建議一種工作方法嗎?

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)

現在已通過以下合並拉取請求https://github.com/mwaskom/seaborn/pull/1935以更強大的方式解決了這個問題。 我假設它將包含在 v0.9.0 之后的版本中。

您可以使用tree_kws參數控制樹狀圖的LineCollection屬性。

例如:

>>> 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))

將為樹創建一個帶有 1.5 pt 粗線的集群圖,采用另一種深紫色。

可能有更簡單的方法來做到這一點,但這似乎有效:

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)

編輯這不再適用於 Seaborn v. 0.7.1(可能還有一些早期版本); g.ax_col_dendrogram.lines現在返回一個空列表。 我找不到增加線寬的方法,最終臨時修改了 Seaborn 模塊。 在文件matrix.py ,函數class _DendrogramPlotter ,線寬被硬編碼為 0.5; 我將其修改為 1.5:

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

這有效,但顯然不是一種非常可持續的方法。

對於較新版本的 seaborn(使用 0.7.1、0.9.0 測試),這些行位於 LineCollection 中,而不是它們本身。 所以它們的寬度可以改變如下:

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