{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Preamble" ] }, { "cell_type": "code", "execution_count": null, "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", "Plotapi Chord supports a linked data table. This means as you hover over segments and ribbons in the Chord diagram, a data table will be filtering in real-time to show more information.\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": null, "metadata": {}, "outputs": [], "source": [ "matrix = [\n", " [0, 5, 6, 4, 7, 4],\n", " [5, 0, 5, 4, 6, 5],\n", " [6, 5, 0, 4, 5, 5],\n", " [4, 4, 4, 0, 5, 5],\n", " [7, 6, 5, 5, 0, 4],\n", " [4, 5, 5, 5, 4, 0],\n", "]\n", "\n", "names = [\"Action\", \"Adventure\", \"Comedy\", \"Drama\", \"Fantasy\", \"Thriller\"]" ] }, { "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": null, "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", "
ActionAdventureComedyDramaFantasyThriller
Action056474
Adventure505465
Comedy650455
Drama444055
Fantasy765504
Thriller455540
\n", "
" ], "text/plain": [ " Action Adventure Comedy Drama Fantasy Thriller\n", "Action 0 5 6 4 7 4\n", "Adventure 5 0 5 4 6 5\n", "Comedy 6 5 0 4 5 5\n", "Drama 4 4 4 0 5 5\n", "Fantasy 7 6 5 5 0 4\n", "Thriller 4 5 5 5 4 0" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import pandas as pd\n", "pd.DataFrame(matrix, columns=names, index=names)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "To make use of the linked data table, we need to provide some data in CSV format. This could be loaded through a file, directly in a strong, or using a pandas DataFrame (`.to_csv(index=False)`)." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "data_table='''Genre 1,Genre 2,Awesome\n", "Thriller,Action,The Plotapi\n", "Thriller,Action,Action Plotapi\n", "Thriller,Drama,Feeling Plotapi\n", "Thriller,Comedy,Plotapi Force'''" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Visualisation" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We'll enable the linked data table by passing data into the `data_table` parameter, and we'll modify the `data_table_column_width` by setting it to a smaller value of $80$.\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": 9, "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", "" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "Chord(matrix, names, width=400, margin=20, curved_labels=True,\n", " data_table=data_table, data_table_column_width=80).show()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Data table mode includes the \"locking\" feature. Try clicking on the Thriller segment and you will see a padlock icon appear in the top right, and until you click somewhere again the current selection will persist." ] }, { "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 (ipykernel)", "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 }