subrela.plot.matplotlib.draw_dendrogram function

subrela.plot.matplotlib.draw_dendrogram(ax, leaf_data, tree_data, cut_data, palette=None, orientation='vertical', tree_kws=None, cut_kws=None)[source]

Draw a dendrogram.

Parameters
Returns

  • tree_lines (list[matplotlib.lines.Line2D]) – Tree lines.

  • cut_lines (list[matplotlib.lines.Line2D]) – Cut lines.

Notes

A default color of a tree is tree_kws['color']. If it does not exist, palette[0] is used and group colors are selected from palette[1:].

If neither key 'color' nor key 'c' is in cut_kws, 'k' is set to 'color'. If neither key 'linestyle' nor key 'ls' is in cut_kws, '--' is set to 'linestyle'.

Examples

../_images/subrela-plot-matplotlib-draw_dendrogram-1.png
import numpy
import matplotlib.pyplot
from subrela.clustering import get_clusters
from subrela.plot import get_dendrogram_data
from subrela.plot.matplotlib import draw_dendrogram

X = numpy.array([[0, -5, -5, 6, 6], [0, -1, 1, -2, 2]])

Z = get_clusters(X)
leaf_data, _, tree_data, cut_data = get_dendrogram_data(
    Z, groups=[0, 5, 6])

_, ax = matplotlib.pyplot.subplots(figsize=(4, 4))
draw_dendrogram(ax, leaf_data, tree_data, cut_data)
matplotlib.pyplot.show()

Change colors:

../_images/subrela-plot-matplotlib-draw_dendrogram-2.png
import numpy
import matplotlib.pyplot
from subrela.clustering import get_clusters
from subrela.plot import get_dendrogram_data
from subrela.plot.matplotlib import draw_dendrogram

X = numpy.array([[0, -5, -5, 6, 6], [0, -1, 1, -2, 2]])

Z = get_clusters(X)
leaf_data, _, tree_data, cut_data = get_dendrogram_data(
    Z, groups=[0, 5, 6])

_, ax = matplotlib.pyplot.subplots(figsize=(4, 4))
draw_dendrogram(ax, leaf_data, tree_data, cut_data, palette=['m', 'c'])
matplotlib.pyplot.show()

Draw horizontally:

../_images/subrela-plot-matplotlib-draw_dendrogram-3.png
import numpy
import matplotlib.pyplot
from subrela.clustering import get_clusters
from subrela.plot import get_dendrogram_data
from subrela.plot.matplotlib import draw_dendrogram

X = numpy.array([[0, -5, -5, 6, 6], [0, -1, 1, -2, 2]])

Z = get_clusters(X)
leaf_data, _, tree_data, cut_data = get_dendrogram_data(
    Z, groups=[0, 5, 6])

_, ax = matplotlib.pyplot.subplots(figsize=(4, 4))
draw_dendrogram(ax, leaf_data, tree_data, cut_data,
                orientation='horizontal')
matplotlib.pyplot.show()