首页 > 解决方案 > 如何将 scipy 树状图保存为高分辨率文件?

问题描述

我有一个包含 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

标签: matplotlibscipycluster-computinghierarchydendrogram

解决方案


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

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

The saved images looked bad for me, when i opened them, and only zooming in cleared up the problem. But the inlined plot in the jupyter notebook looked good, so maybe you only have to play with the format a bit.

This is probably not the best solution, but for me it worked. Hope someone else more competent can give you the correct solution too!

Ps.: Do not try to save these with 520 DPI, would break the pyplot.


推荐阅读