vreyespue / scipy_cut_tree_balanced

Python function that performs a balanced cut-tree of a SciPy linkage matrix
BSD 3-Clause "New" or "Revised" License
13 stars 1 forks source link

How to draw dendrogram #8

Closed stellajo99 closed 1 year ago

stellajo99 commented 2 years ago

Hi, I'm trying to draw dendrogram like yours (mark on each cluster id at different levels) by using MLcut, but it doesn't work well. Could you specify a little bit more on how to draw dendrogram at different levels like you did in both pictures of yours?

Thank you for your amaziong code, by the way. It helped me a lot.

jenniepython commented 1 year ago

Is it possible to create the balanced dendrogram from the levels in balanced_cut_cluster_level ? I really love this tree cutting method but also want to recreate the dendrogram on my data - kind regards, Jennie

vreyespue commented 1 year ago

Hi, I'm trying to draw dendrogram like yours (mark on each cluster id at different levels) by using MLcut, but it doesn't work well. Could you specify a little bit more on how to draw dendrogram at different levels like you did in both pictures of yours?

Thank you for your amaziong code, by the way. It helped me a lot.

Hi @stellajo99, very sorry for my extremely late reply. I did not get noticed for this issue being opened, that's why this got unanswered, my apologies. To your question, I am sorry to disappoint you, but the drawings were done manually. In other words, I knew exactly from the arrays which data points belonged to which cluster IDs, so I just drew the marks choosing specific (x,y) coordinates, and testing whether it looked correct. For instance, this is the code I used for drawing the standard straight cut:

from scipy.cluster.hierarchy import ward, cut_tree, dendrogram
import matplotlib.pyplot as plt
%matplotlib inline
fig = plt.figure(figsize=(18,12))
ax = fig.add_subplot(111)
dn = dendrogram(Z, truncate_mode='level', p=40, leaf_font_size=8)

plt.yscale('symlog',basey=10,linthreshy=0.05,distance_sort=True)
plt.xlabel('\nData sample IDs')
plt.ylabel('Heigth (log scale)\n')

ax.plot(np.linspace(-30,1030,100),np.repeat(0.43,100),'k-',clip_on=False)

ax.text(-3, 0.46, '13', color='white', fontsize=10, bbox=dict(facecolor='black', boxstyle='round'))
ax.text(9, 0.38, '18', color='white', fontsize=10, bbox=dict(facecolor='black', boxstyle='round'))
...
ax.text(988, 0.46, '9', color='white', fontsize=10, bbox=dict(facecolor='black', boxstyle='round'))

plt.show()
fig.savefig('dendrogram_1_standard_cut.png', dpi=300)

As this was performed just for the visual explanation in the Readme, I did not develop any standard method for this. Please let me know if you had progress in this matter using MLcut or any other library, I would be very interested.

Many thanks and best regards.

vreyespue commented 1 year ago

Is it possible to create the balanced dendrogram from the levels in balanced_cut_cluster_level ? I really love this tree cutting method but also want to recreate the dendrogram on my data - kind regards, Jennie

Hi @jenniepython, very sorry for my extremely late reply. I did not get noticed for this issue being opened, that's why this got unanswered, my apologies. To your question, I assume that you want to generate the figure containing the resulting dendogram and clustering, right? As you can see in my previous post within this issue, I used the function dendogram from scipy in order to plot the dendogram in the example. For the cluster labels, I just drew the marks choosing specific (x,y) coordinates, and tested whether it looked correct (see exemplary code in previous post). I am sorry that at the moment there is no standard/automatic solution to plot the resulting dendogram and clustering. Please let me know if you had progress in this matter using your own code or any other library, I would be very interested. Many thanks and best regards.

vreyespue commented 1 year ago

I proceed to close this issue, because it is not planned to include functions to automatically generate figures from the resulting dendograms and clustering. Many thanks for your understanding.