{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Preamble" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "from plotapi import Chord\n", "\n", "Chord.set_license(\"your username\", \"your license key\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Introduction\n", "\n", "It may be desirable to have equal-sized segments in our Chord Diagram. This is possible with Plotapi, and makes use of the `colored_diagonals` feature. The idea is to determine the max frequency in our plot, and set all the diagonals to that value. Let's assume that is $20$ for this example.\n", "\n", "As we can see, we have set our license details in the preamble with `Chord.set_license()`" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Dataset" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Chord expects a list of names (`list[str]`) and a co-occurence matrix (`list[list[float]]`) as input." ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "\n", "\n", "\n", "\n", "\n", "Plotapi - Chord Diagram\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "
\n", " \n", " \n", " \n", "\n", "" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "matrix = [\n", " [20, 0, 0, 0, 0, 0],\n", " [0, 20, 0, 0, 0, 0],\n", " [0, 0, 20, 0, 0, 0],\n", " [0, 0, 0, 20, 0, 0],\n", " [0, 0, 0, 0, 20, 0],\n", " [0, 0, 0, 0, 0, 20],\n", "]\n", "\n", "names=['one','two','three','four','five','six']\n", "\n", "Chord(matrix, names, title=\"Diagonals not coloured\", \n", " colored_diagonals=False).show()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We can see above that this creates a chord diagram with equal-sized segments. All we need to do now is introduce our relationships, and subtract them from the corresponding diagonal value." ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [], "source": [ "matrix = [\n", " [11, 4, 5, 0, 0, 0],\n", " [4, 14, 2, 0, 0, 0],\n", " [5, 2, 13, 0, 0, 0],\n", " [0, 0, 0, 20, 0, 0],\n", " [0, 0, 0, 0, 20, 0],\n", " [0, 0, 0, 0, 0, 20],\n", "]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "It may look more clear if we present this as a table with the columns and indices labelled. This is entirely optional." ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
onetwothreefourfivesix
one1145000
two4142000
three5213000
four0002000
five0000200
six0000020
\n", "
" ], "text/plain": [ " one two three four five six\n", "one 11 4 5 0 0 0\n", "two 4 14 2 0 0 0\n", "three 5 2 13 0 0 0\n", "four 0 0 0 20 0 0\n", "five 0 0 0 0 20 0\n", "six 0 0 0 0 0 20" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import pandas as pd\n", "pd.DataFrame(matrix, columns=names, index=names)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Visualisation" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "With our matrix prepared, we can use Plotapi with `colored_diagonals=False` to create a diagram with equal-sized segments and some ribbons between categories. We could add some padding to separate them too.\n", "\n", "\n", "Here we're using `.show()` which outputs to a Jupyter Notebook cell, however, we may want to output to a HTML file with `.to_html()` instead. More on the different output methods later!\n", "\n", "Be sure to interact with the visualisation to see what the default settings can do!\n", "
" ] }, { "cell_type": "code", "execution_count": 20, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "\n", "\n", "\n", "\n", "\n", "Plotapi - Chord Diagram\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "
\n", " \n", " \n", " \n", "\n", "" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "Chord(matrix, names, colors=\"set2\",\n", " title=\"Diagonals not coloured\", \n", " colored_diagonals=False, padding=0.2).show()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "You can do so much more than what's presented in this example, and we'll cover this in later sections. If you want to see the full list of growing features, check out the Plotapi Documentation. and the Plotapi Gallery." ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.9.1" } }, "nbformat": 4, "nbformat_minor": 4 }