matplotlib - 如何将 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。
我得到了下图作为图。
解决方案
问题不在于您的分辨率,而在于图像的大小(或线条的大小)。由于我不知道如何更改树状图中的线宽,因此我将采用直接的解决方案来制作巨大的图像。
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.
推荐阅读
- julia - “错误:语法:不能并列字符串文字”用引号结束三引号字符串文字时
- c# - 如何为 GPIO 启用 LightningProviders?
- c# - 当按下按钮并显示在另一个页面上时,试图从两个输入中更改标签?| Xamarin | C#
- r - 将 JSON 对象拆分为多个数据帧的更快方法?
- javascript - Highcharts 缩放时显示滚动条
- c++ - 在具有 32 位目标的 Xcode 9.3 中将旧 32 位项目转移到 64 位
- javascript - 如何在不多次初始化的情况下将 PouchDB 称为对象的属性?
- python - 如何修复 selenium.common.exceptions.NoSuchElementException?
- java - 为什么AES解密给出空结果?
- c# - 首先在 ASP.NET Core 中使用 SQL 中的 FileStream 和代码