聚类后如何改变dtaidistance图的大小?

2024-04-23 19:17:15 发布

您现在位置:Python中文网/ 问答频道 /正文

我似乎无法使用Python/PyCharm中的dtaidistance包更改输出绘图大小。在模型图函数并将整个绘图包装为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()

在当前状态下,绘图保存为我的树.png,但分辨率很低。此低分辨率绘图可以通过

import matplotlib.image as mpimg

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

但我需要一个比提供的分辨率高得多的分辨率。。。你知道吗


Tags: import绘图pngmatplotlibasnp分辨率plt
1条回答
网友
1楼 · 发布于 2024-04-23 19:17:15

我刚找到解决办法。创建子地块图形并将正确的轴指定给模型图删除指定名称时的函数(无需保存):

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

我想这毕竟是个matplotlib特有的问题。。。你知道吗

相关问题 更多 >