{ "cells": [ { "cell_type": "code", "execution_count": 1, "id": "b1f7c080-7fb0-428b-918e-04c4a65cfd36", "metadata": {}, "outputs": [], "source": [ "import numpy as np\n", "import pandas as pd\n", "\n", "from lets_plot import *\n", "from lets_plot.plot.coord import coord_polar" ] }, { "cell_type": "code", "execution_count": 2, "id": "eebabc41-5de3-44ae-b0e8-ec30de4c94e1", "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", " \n", " \n", " " ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "LetsPlot.setup_html()" ] }, { "cell_type": "markdown", "id": "bc520db3-bdee-4a28-9c32-d15874029531", "metadata": {}, "source": [ "##### Util for `PlotSpecDebugger.kt`" ] }, { "cell_type": "code", "execution_count": 3, "id": "d493053b-3a9a-4409-97fe-57be18f9dac6", "metadata": {}, "outputs": [], "source": [ "def dump_plot(plot, display=False):\n", " import json\n", "\n", " try:\n", " import clipboard\n", " except:\n", " clipboard = None\n", " \n", " from lets_plot._type_utils import standardize_dict\n", " \n", " plot_dict = standardize_dict(plot.as_dict())\n", " plot_json = json.dumps(plot_dict, indent=2)\n", " \n", " if clipboard:\n", " clipboard.copy('')\n", " clipboard.copy(str(plot_json))\n", " else:\n", " if display is None:\n", " display = True\n", "\n", " return plot" ] }, { "cell_type": "markdown", "id": "ab190a94-9770-4c6b-9bb6-caaf120e60a9", "metadata": {}, "source": [ "##### Data:" ] }, { "cell_type": "code", "execution_count": 4, "id": "a258b6d0-767b-4684-bcbf-4dc6fba6126f", "metadata": {}, "outputs": [], "source": [ "subj_short = ['CS', 'Math', 'Phys', 'Chem', 'Bio']\n", "subj = ['Computer Science', 'Mathematics', 'Physic', 'Chemistry', 'Biology']\n", "john = {\n", " 'subj_short': subj_short,\n", " 'subj': subj,\n", " 'subj_id': [1, 2, 3, 4, 5],\n", " 'student': ['John'] * 5,\n", " 'score': [19, 15, 18, 12, 9]\n", "}\n", "\n", "bob = {\n", " 'subj_short': subj_short,\n", " 'subj': subj,\n", " 'subj_id': [1, 2, 3, 4, 5],\n", " 'student': ['Bob'] * 5,\n", " 'score': [7, 18, 10, 18, 19]\n", "}\n", "\n", "def merge(dest, other):\n", " for k, v in other.items():\n", " list = dest.get(k, [])\n", " list.extend(v)\n", " list.append(v[0]) # close path\n", " dest[k] = list\n", "\n", "\n", "radar_df = {}\n", "merge(radar_df, john)\n", "merge(radar_df, bob)" ] }, { "cell_type": "markdown", "id": "1581c9c7-d23d-4b91-a530-26d521368c7b", "metadata": {}, "source": [ "### Minimal radar plot" ] }, { "cell_type": "code", "execution_count": 5, "id": "e0fcea18-3b15-4c8c-9e64-dc879390ff7d", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "p = ggplot(radar_df, aes(x='subj_short', y='score', color='student')) \\\n", " + coord_polar(ylim=[0, 20]) \\\n", " + theme(\n", " axis_ticks_y=element_blank(), \n", " axis_title_y=element_blank(),\n", " axis_title_x=element_blank(),\n", " axis_tooltip=element_blank(),\n", " )\n", "\n", "def radar_plot(df):\n", " p = ggplot(df) \\\n", " + geom_path(aes(x='subj_short', y='score', color='student'), flat=True, tooltips='none') \\\n", " + geom_point(aes(x='subj_short', y='score', color='student'), tooltips=layer_tooltips().title(\"@student\").line(\"Subject|@subj\").line(\"Score|@score\")) \\\n", " + coord_polar(ylim=[0, 20]) \\\n", " + theme(\n", " axis_ticks_y=element_blank(), \n", " axis_title_y=element_blank(),\n", " axis_title_x=element_blank(),\n", " axis_tooltip=element_blank(),\n", " )\n", " return p\n", "\n", "p + geom_path(flat=True, tooltips='none') \\\n", " + geom_point(tooltips=layer_tooltips().title(\"@student\").line(\"Subject|@subj\").line(\"Score|@score\"))" ] }, { "cell_type": "markdown", "id": "2211b40b-3ceb-4dd3-85f2-e17442f74f60", "metadata": {}, "source": [ "### Issues:" ] }, { "cell_type": "code", "execution_count": 6, "id": "fae09e23-a849-4311-af91-571e7bcd9901", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "p + aes(x='subj', y='score', color='student') + geom_path(flat=True, tooltips='none') \\\n", " + geom_point(tooltips=layer_tooltips().title(\"@student\").line(\"Subject|@subj\").line(\"Score|@score\"))" ] }, { "cell_type": "markdown", "id": "769dea86-ecf9-43c3-b200-376534f84364", "metadata": {}, "source": [ "1. Long names get rotated\n", "2. Double tooltip on Computer Science\n", "3. Plot is aligned to top, leaving large empty space at bottom" ] } ], "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.8.15" } }, "nbformat": 4, "nbformat_minor": 5 }