{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "<div class=\"contentcontainer med left\" style=\"margin-left: -50px;\">\n", "<dl class=\"dl-horizontal\">\n", " <dt>Title</dt> <dd> Dendrogram Element</dd>\n", " <dt>Dependencies</dt> <dd>Bokeh</dd>\n", " <dt>Backends</dt> <dd><a href='../bokeh/Dendrogram.ipynb'>Bokeh</a></dd> <dd><a href='../matplotlib/Dendrogram.ipynb'>Matplotlib</a></dd>\n", "</dl>\n", "</div>" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import random\n", "\n", "import numpy as np\n", "import pandas as pd\n", "\n", "import holoviews as hv\n", "\n", "hv.extension(\"bokeh\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "A ``Dendrogram`` element represents a hierarchical tree structure, where branches connect nodes in two-dimensional space.\n", "\n", "In this example we will create a simple tree structure. While the default behavior for dendrograms is to hide axis labels, we will show them for this first example to make it easier to see the relationship between the input data and the output plot." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "x = np.array(\n", " [\n", " [35.0, 35.0, 45.0, 45.0],\n", " [25.0, 25.0, 40.0, 40.0],\n", " [15.0, 15.0, 32.5, 32.5],\n", " [5.0, 5.0, 23.75, 23.75],\n", " ]\n", ")\n", "y = np.array(\n", " [\n", " [0.0, 1.04158712, 1.04158712, 0.0],\n", " [0.0, 1.18037928, 1.18037928, 1.04158712],\n", " [0.0, 1.20879035, 1.20879035, 1.18037928],\n", " [0.0, 1.31643301, 1.31643301, 1.20879035],\n", " ]\n", ")\n", "dendrogram = hv.Dendrogram(x, y).opts(xaxis=True, yaxis=True)\n", "dendrogram" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "A dendrogram usually complements another plot to describe the hierarchy in that data structure. This can be done with the `dendrogram` operation, which adjoins dendrograms to the main plot." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "df = pd.DataFrame(\n", " [(i, chr(65 + j), random.random()) for j in range(10) for i in range(5)],\n", " columns=[\"z\", \"x\", \"y\"],\n", ")\n", "heatmap = hv.HeatMap(df)\n", "heatmap" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from holoviews.operation import dendrogram\n", "\n", "hv.operation.dendrogram(heatmap, adjoint_dims=[\"x\", \"z\"], main_dim=\"y\")" ] } ], "metadata": { "language_info": { "name": "python", "pygments_lexer": "ipython3" } }, "nbformat": 4, "nbformat_minor": 4 }