【问题标题】:How to change dtaidistance plot size after clustering?聚类后​​如何更改 dtaidistance 图的大小?
【发布时间】:2019-10-01 17:14:21
【问题描述】:

我似乎无法使用 Python/PyCharm 中的 dtaidistance 包更改输出绘图大小。 model.plot 函数中似乎没有“figsize”参数或类似参数,并且如下所述以 SciPy 样式包装整个图会导致空图 [代码改编自 https://pydigger.com/pypi/dtaidistance]:

import matplotlib.pyplot as plt
from dtaidistance import dtw, clustering
import numpy as np

# demo data matrix:
series = np.matrix([
     [0., 0, 1, 2, 1, 0, 1, 0, 0],
     [0., 1, 2, 0, 0, 0, 0, 0, 0],
     [1., 2, 0, 0, 0, 0, 0, 1, 1],
     [0., 0, 1, 2, 1, 0, 1, 0, 0],
     [0., 1, 2, 0, 0, 0, 0, 0, 0],
     [1., 2, 0, 0, 0, 0, 0, 1, 1]])

# Custom Hierarchical clustering
model1 = clustering.Hierarchical(dtw.distance_matrix_fast, {})
# Augment Hierarchical object to keep track of the full tree
model2 = clustering.HierarchicalTree(model1)

# Fit Model:
cluster_idx = model2.fit(series=series)

# create plot:
fig = plt.figure(figsize=(14, 10))
tree = model2.plot("mytree.png")
plt.show()

在当前状态下,绘图保存为 mytree.png,但分辨率非常低。这个低分辨率图可以通过

显示
import matplotlib.image as mpimg

img = mpimg.imread("mytree.png")
imgplot = plt.imshow(img)
plt.show()

但我需要比提供的分辨率高得多的分辨率...

【问题讨论】:

    标签: python plot hierarchical-clustering


    【解决方案1】:

    我刚刚找到了解决方案。创建一个 subplots 图形并将正确的轴分配给 model.plot 函数,同时删除指定的名称(无需保存):

    fig, ax = plt.subplots(ncols=2, nrows=1, figsize=(12, 8))  # attention: 2 columns needed!
    tree_plot = model2.plot(axes=ax)
    plt.show()
    

    我想这毕竟是一个 matplotlib 特有的问题......

    【讨论】:

      猜你喜欢
      • 2022-11-11
      • 2022-11-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-11-10
      • 2014-10-31
      • 2021-12-16
      • 1970-01-01
      相关资源
      最近更新 更多