subrela.plot.bokeh.draw_node_info function

subrela.plot.bokeh.draw_node_info(fig, node_data, info, formatter=<class 'str'>, angle=0, location='outer', x_offset=2, y_offset=2, orientation='vertical', kws=None)[source]

Draw node information as texts.

Parameters
  • fig (bokeh.plotting.figure.Figure) – Figure on which texts are drawn.

  • node_data (pandas.DataFrame) – Data of nodes returned by subrela.plot.get_dendrogram_data function.

  • info (pandas.Series) – Node information. Its index is a cluster index.

  • formatter (callable, optional) – Function for converting a value of info into str.

  • angle ({0, 90, -90}, optional) – Angle of a text from the x axis in degrees.

  • location ({'first', 'last', 'inner', 'outer'}, optional) – Side on which a text is located. If ‘first’ and ‘last’, a text is located on the side near to the first and the last leaf, respectively. If ‘inner’, and ‘outer’, a text is located on the side near to and far from a parent node, respectively.

  • x_offset (float, optional) – Offset from a tree line along the x axis in screen units.

  • y_offset (float, optional) – Offset from a tree line along the y axis in screen units.

  • orientation ({'vertical', 'horizontal'}, optional) – Orientation of a tree. If 'vertical', the height direction of a tree corresponds to the y axis. If 'horizontal', it corresponds to the x axis.

  • kws (dict or None, optional) – Keyword arguments passed to bokeh.plotting.figure.Figure.text method.

Returns

texts ((2,) list[bokeh.models.renderers.GlyphRenderer]) – Renderers for texts of the first and the last sibling nodes.

Raises

ValueError – If kws contains key 'x', 'y', 'text', 'angle', 'angle_units`'', ``'x_offset', 'y_offset', 'text_align', 'text_baseline', 'source', and 'view'.

Examples

import numpy
import bokeh.plotting
import bokeh.io
from subrela.clustering import get_clusters
from subrela.plot import get_dendrogram_data
from subrela.plot.bokeh import draw_dendrogram, draw_node_info

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

Z = get_clusters(X)
leaf_data, node_data, tree_data, cut_data = get_dendrogram_data(Z)

fig = bokeh.plotting.Figure(plot_width=300, plot_height=300)
draw_dendrogram(fig, leaf_data, tree_data, cut_data)
draw_node_info(fig, node_data, node_data['height'])
bokeh.io.show(fig)

Change a format of texts:

import numpy
import bokeh.plotting
import bokeh.io
from subrela.clustering import get_clusters
from subrela.plot import get_dendrogram_data
from subrela.plot.bokeh import draw_dendrogram, draw_node_info

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

Z = get_clusters(X)
leaf_data, node_data, tree_data, cut_data = get_dendrogram_data(Z)

fig = bokeh.plotting.Figure(plot_width=300, plot_height=300)
draw_dendrogram(fig, leaf_data, tree_data, cut_data)
draw_node_info(fig, node_data, node_data['height'],
               formatter='{:.1f}'.format)
bokeh.io.show(fig)

Rotate texts:

import numpy
import bokeh.plotting
import bokeh.layouts
import bokeh.io
from subrela.clustering import get_clusters
from subrela.plot import get_dendrogram_data
from subrela.plot.bokeh import draw_dendrogram, draw_node_info

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

Z = get_clusters(X)
leaf_data, node_data, tree_data, cut_data = get_dendrogram_data(Z)

figs = []
for angle in [90, -90, 0]:
    fig = bokeh.plotting.Figure(title='angle = {}'.format(angle),
                                plot_width=300, plot_height=300)
    draw_dendrogram(fig, leaf_data, tree_data, cut_data)
    draw_node_info(fig, node_data, node_data['height'],
                   formatter='{:.1f}'.format, angle=angle)
    figs.append(fig)
bokeh.io.show(bokeh.layouts.grid(figs, ncols=2))

Change locations of texts:

import numpy
import bokeh.plotting
import bokeh.layouts
import bokeh.io
from subrela.clustering import get_clusters
from subrela.plot import get_dendrogram_data
from subrela.plot.bokeh import draw_dendrogram, draw_node_info

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

Z = get_clusters(X)
leaf_data, node_data, tree_data, cut_data = get_dendrogram_data(Z)

figs = []
for location in ['first', 'last', 'inner', 'outer']:
    fig = bokeh.plotting.Figure(
        title='location = "{}"'.format(location),
        plot_width=300, plot_height=300)
    draw_dendrogram(fig, leaf_data, tree_data, cut_data)
    draw_node_info(fig, node_data, node_data['height'],
                   formatter='{:.1f}'.format, location=location)
    figs.append(fig)
bokeh.io.show(bokeh.layouts.grid(figs, ncols=2))

Offset texts:

import numpy
import bokeh.plotting
import bokeh.layouts
import bokeh.io
from subrela.clustering import get_clusters
from subrela.plot import get_dendrogram_data
from subrela.plot.bokeh import draw_dendrogram, draw_node_info

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

Z = get_clusters(X)
leaf_data, node_data, tree_data, cut_data = get_dendrogram_data(Z)

fig = bokeh.plotting.Figure(plot_width=300, plot_height=300)
draw_dendrogram(fig, leaf_data, tree_data, cut_data)
draw_node_info(fig, node_data, node_data['height'],
               formatter='{:.1f}'.format, x_offset=5, y_offset=10)
bokeh.io.show(fig)