title: How to draw a dendrogram from a NetworkX minimum spanning tree?
body: <p>I've computed a minimum spanning tree from a distance matrix, using NetworkX. I want now to build a dendrogram from it.</p>
  
  <p>My MST :
  <a href="https://i.stack.imgur.com/oyS76.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/oyS76.png" alt="![MST](http://www.bedside-ai.com/mst.png)"></a></p>
  
  <p>I've tried using the adjacency matrix (using NetworkX's to_pandas_adjacency)</p>
  
  <p>(T is my MST)</p>
  
  <pre class="lang-py prettyprint-override"><code>df = nx.to_pandas_adjacency(T)
  
  from scipy.spatial.distance import squareform
  dist_array = squareform(df) #https://stackoverflow.com/questions/18952587/use-distance-matrix-in-scipy-cluster-hierarchy-linkage
  
  plt.figure(figsize=(10,10)) 
  mergings = linkage(dist_array, method='complete', metric='euclidean')
  dendrogram(mergings, labels = distances.index, leaf_rotation=90, leaf_font_size=14)
  plt.show()
  </code></pre>
  
  <p>Now, as the adjacency matrix is filled with 0's for non-edges, I guess linkage compute Euclidean distance and end up with a 3 clusters dendrogram (where all the cluster's points are at 0 distance), while I'm expecting to get the same linkage as in my original MST !</p>
  
  <p><a href="https://i.stack.imgur.com/sMnrt.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/sMnrt.png" alt="enter image description here"></a></p>
  
  <p>I tried using inf or large value for the nonedge default value to to_pandas_adjacency, but end up with invalid matrix...</p>
  
  <p>Help anyone ? My best guess is that I'm not understanding and using linkage as I should...</p>
  
  <p><strong>Edit</strong>
  I know, doing it the other way around (DT and then build the MST) might probably be easier, but I need to reproduce the order of operations in order to recreate the results of an original study...</p>
  
  <p><strong>Edit 2</strong>
  Since the scipy's linkage function compute Euclidean distance between each point (or node here), I guess (but without any certainty) I need to find a way to convert my adjacency matrix to an array similar to what's linkage function output, ie weighted edge list, but with sub clusters size as fourth column.</p>