Mahsun ALTIN
Mahsun ALTIN

Reputation: 53

How to save a scipy dendrogram as high resolution file?

I have a matrix which has 600 different labels. Therefore, it is really big file; and I couldn't see these labels very well, when I created a figure to cluster my data. How should I create a high resolution file and save it?

I already tried below code.

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

I am trying to get big high resolution file, it can be png or jpeg.

I got below image as figure.

https://i.sstatic.net/m4aqX.jpg

Upvotes: 3

Views: 4248

Answers (1)

Phoenixdust
Phoenixdust

Reputation: 493

The problem is not with your resolution, but the size of the image (or the size of the lines). Since i do not know how to change the linewidth in the dendogram plot, i will just go with the straight forward solution to make a HUGE image.

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.

Upvotes: 2

Related Questions