如何将scipy树状图保存为高分辨率文件?

问题描述 投票:2回答:1

我有一个包含600个不同标签的矩阵。因此,它确实是个大文件。当我创建一个图以对数据进行聚类时,我看不到这些标签。如何创建高分辨率文件并保存?

我已经尝试过下面的代码。

import scipy.cluster.hierarchy as hcluster
import scipy.spatial.distance as ssd

SimMatrix = mainTable

distVec = ssd.squareform(SimMatrix)
linkage = hcluster.linkage(1 - distVec)
dendro  = hcluster.dendrogram(linkage, leaf_rotation=90., leaf_font_size=0.5,)

matplotlib.pyplot.savefig('plt.png', dpi=520, format='png', bbox_inches='tight')

我正在尝试获取较大的高分辨率文件,它可以是png或jpeg。

我得到了下图的图像。

https://imgur.com/Iij1BdB

matplotlib scipy cluster-computing hierarchy dendrogram
1个回答
0
投票

问题不在于分辨率,而是图像的大小(或线条的大小)。由于我不知道如何更改树状图中的线宽,因此我将直接采用简单的解决方案来制作巨大的图像。

import scipy.cluster.hierarchy as hcluster
import scipy.spatial.distance as ssd
import matplotlib.pyplot as plt
import numpy as np

SimMatrix = np.random.random((600,600))
SimMatrix = SimMatrix+SimMatrix.T
SimMatrix = np.abs(SimMatrix-np.diag(np.diag(SimMatrix)))

distVec = ssd.squareform(SimMatrix)
linkage = hcluster.linkage(distVec) #Changed here do NOT C+P back
plt.figure(figsize=(150,150))
dendro  = hcluster.dendrogram(linkage, leaf_rotation=90., leaf_font_size=0.5,)

plt.savefig('plt.png', format='png', bbox_inches='tight')
plt.savefig('plt.jpg', format='jpg', bbox_inches='tight')

当我打开它们时,保存的图像对我来说很糟糕,只有放大才能解决问题。但是jupyter笔记本中的内联图看起来不错,因此也许您只需要使用这种格式即可。

这可能不是最好的解决方案,但对我而言,它奏效了。希望其他更有能力的人也能给您正确的解决方案!

Ps .:请勿尝试使用520 DPI保存它们,否则会破坏pyplot。

© www.soinside.com 2019 - 2024. All rights reserved.