{ "cells": [ { "cell_type": "code", "execution_count": 1, "id": "d9dcefd7-0655-4e71-99e3-2e6c8bb8ff7a", "metadata": {}, "outputs": [], "source": [ "import numpy as np\n", "import pandas as pd\n", "\n", "from lets_plot import *\n", "from lets_plot.mapping import as_discrete" ] }, { "cell_type": "code", "execution_count": 2, "id": "583c0c27-38fe-4ecb-8339-cb71c411a9c6", "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", " \n", " \n", " " ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "LetsPlot.setup_html()" ] }, { "cell_type": "markdown", "id": "aab8e2af-11bb-4238-857b-3d1a36caead0", "metadata": {}, "source": [ "#### Set `theme_gray()` as default theme. It improves plots readability." ] }, { "cell_type": "code", "execution_count": 3, "id": "d8c9764b-7387-45f9-89cd-c0bc0d8d3b62", "metadata": {}, "outputs": [], "source": [ "LetsPlot.set_theme(theme_grey())" ] }, { "cell_type": "markdown", "id": "dde1c0a0-7d07-4c60-914b-6b3f8ad18a6b", "metadata": {}, "source": [ "#### Data" ] }, { "cell_type": "code", "execution_count": 4, "id": "30e4e94e-4993-4472-a22c-436515f698d0", "metadata": {}, "outputs": [], "source": [ "labels_df = {\n", " 'x': [0, 1, 2, 3, 4, 5, 6, 7, 8],\n", " 'y': [0, 45, 90, 135, 180, 225, 270, 315, 360],\n", " 'r_y': [360, 315, 270, 225, 180, 135, 90, 45, 0],\n", " 'l': ['l0', 'l45', 'l90', 'l135', 'l180', 'l225', 'l270', 'l315', 'l360'],\n", " 'g': ['g1', 'g1', 'g1', 'g2', 'g2', 'g2', 'g3', 'g3', 'g3']\n", "}\n", "\n", "lollipop_df = {\n", " 'c': ['a', 'b', 'c', 'd', 'e', 'f'],\n", " 'x': [1, 2, 3, 4, 5, 6],\n", " 'y': [1, 2, 3, 4, 5, 6],\n", "}\n", "\n", "student_df = {\n", " 'subj': ['progr', 'math', 'physic', 'chemistry', 'biology'],\n", " 'subj_id': [1, 2, 3, 4, 5],\n", " 'student': ['John'] * 5,\n", " 'score': [19, 15, 18, 12, 9]\n", "}" ] }, { "cell_type": "markdown", "id": "8735e2a4-69fe-4743-a332-1e0fb2d288ea", "metadata": {}, "source": [ "##### Util for `PlotSpecDebugger.kt`" ] }, { "cell_type": "code", "execution_count": 5, "id": "fd539de7-611a-4925-b51a-970748e8c318", "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": "72d3e36d-0346-4706-a3f9-9751ad0e4ad1", "metadata": {}, "source": [ "# Geoms" ] }, { "cell_type": "markdown", "id": "fb2cd1ff-70c5-49b1-b9dd-41220ad3dddf", "metadata": {}, "source": [ "## `geom_area()`\n", "Line get transformed into a circle:" ] }, { "cell_type": "code", "execution_count": 6, "id": "8e55a996-6718-4e8d-b509-cabc6ea4f1cd", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "p = ggplot() + geom_area(aes(x=[0, 1], y=[1, 1]))\n", "gggrid([\n", " p,\n", " p + coord_polar()\n", "])" ] }, { "cell_type": "markdown", "id": "b13b1bda-0647-46ae-84ea-75e70251c6aa", "metadata": {}, "source": [ "### `flat=True`\n", "The plot can be transformed into a radar plot by using `flat=True` and a discrete x-scale." ] }, { "cell_type": "code", "execution_count": 7, "id": "9df93573-b325-4b9a-87f9-47dc31949ef7", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "p = ggplot(student_df) \\\n", " + geom_area(aes(x='subj_id', y='score'), flat=True) \\\n", " + geom_point(aes(x='subj_id', y='score')) \\\n", "\n", "labels = { 1: 'progr', 2: 'math', 3: 'physic', 4: 'chemistry', 5: 'biology' }\n", "\n", "continuous = scale_x_continuous(labels=labels)\n", "discrete = scale_x_discrete(labels=labels)\n", "\n", "gggrid([\n", " p + continuous,\n", " p + continuous + coord_polar() + ggtitle('scale_x_continuous'),\n", " p + discrete + coord_polar() + ggtitle('scale_x_discrete'),\n", "]) " ] }, { "cell_type": "markdown", "id": "f537def1-7194-47d9-99f7-9d3d5b8f31c4", "metadata": {}, "source": [ "## `geom_segment()`" ] }, { "cell_type": "code", "execution_count": 8, "id": "73029c12-3874-4f18-ba25-bfb79e901a2e", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "p = ggplot() \\\n", " + geom_segment(x=0, y=0, xend=4, yend=4, arrow=arrow(), size=1) \\\n", " + geom_segment(x=8, y=0, xend=4, yend=4, arrow=arrow(), size=1) \\\n", "\n", "gggrid([\n", " p,\n", " p + coord_polar()\n", "])" ] }, { "cell_type": "markdown", "id": "6382f013-3fba-4e47-82f5-b0eb37c40fe3", "metadata": {}, "source": [ "`size_end`/`stroke_end` precision length adjustment parameters:" ] }, { "cell_type": "code", "execution_count": 9, "id": "d94035f2-bba7-4474-a4e8-17ca7045e396", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# known problem - zero-length segment because of second datapoint.\n", "# this is a temp workaround to sync stroke/stroke_end and size/size_ens domains\n", "d= { \n", " 'x': [0,1], \n", " 'y':[0,0], \n", " 'size': [8,10], \n", " 'stroke':[1,2],\n", " 'size_end':[10,0], \n", " 'stroke_end':[2,0]\n", "}\n", "\n", "p = ggplot(d, aes('x','y')) \\\n", " + geom_point(aes(size='size', stroke='stroke'), shape=21, alpha=0.5, color=\"red\", show_legend=False) \\\n", " + geom_segment(\n", " aes(size_start='size', stroke_start='stroke',size_end='size_end', stroke_end='stroke_end'),\n", " xend=1, \n", " yend=0,\n", " size=2,\n", " arrow=arrow(ends='both', type='open', length=22, angle=30), \n", " ) \\\n", " + scale_identity(['size','size_start','size_end']) \n", "\n", "gggrid([\n", " p,\n", " p + coord_polar(xlim=[-0.35, 1.35], ylim=[-2, 2]) # lims are only to make the figure smiling\n", "])" ] }, { "cell_type": "markdown", "id": "8b35bd2d-7d0f-4043-96fd-14de12139218", "metadata": {}, "source": [ "## `geom_label()`\n", "Regular scatter plot." ] }, { "cell_type": "code", "execution_count": 10, "id": "6e40d184-3eb6-47b6-aefc-e288c24daaba", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "p = ggplot(labels_df, aes(x='x', y='y', label='l')) + geom_label()\n", "\n", "gggrid([\n", " p, \n", " p + coord_polar() + ggtitle('coord_polar()'),\n", " p + coord_polar(theta='y') + ggtitle('theta=y'),\n", "])" ] }, { "cell_type": "markdown", "id": "72d0487a-2109-4af5-aa8e-7909bc9983ae", "metadata": {}, "source": [ "## `geom_path()`\n", "The transform resamples path data by converting straight segments into curves. The `flat` parameter controls this behaviour." ] }, { "cell_type": "code", "execution_count": 11, "id": "96f14457-ba1d-4e9e-86bf-40b206b7c719", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "p = ggplot(labels_df, aes(x='x', y='y', color='y')) + scale_color_brewer(palette='GnBu')\n", "\n", "gggrid([\n", " p + geom_path(size=3) + coord_polar() + ggtitle('coord_polar()'),\n", " p + geom_path(size=3, flat=True) + coord_polar(theta=\"x\") + ggtitle('coord_polar(), flat=True'),\n", "], ncol=2)" ] }, { "cell_type": "markdown", "id": "2d6165b3-4c08-4c38-8fa7-530c18c747ec", "metadata": {}, "source": [ "### Autoclose on a discrete x-scale" ] }, { "cell_type": "code", "execution_count": 12, "id": "7ab09028-14c8-4f9d-8c98-3d9df2e8d09a", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ggplot(student_df) + geom_path(aes(x='subj', y='score'), flat=True) + coord_polar(ylim=[0, 20])" ] }, { "cell_type": "markdown", "id": "ada8aadb-937d-40a1-a94c-7ffbdf7eb08c", "metadata": {}, "source": [ "## `geom_lollipop()`\n", "See the `Params` section for details on using the `ylim` parameters." ] }, { "cell_type": "code", "execution_count": 13, "id": "0320c4d7-c628-41cd-864b-46e324a49b0a", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "p = ggplot(lollipop_df, aes('c', 'y')) + geom_lollipop()\n", "\n", "gggrid([\n", " p, \n", " p + coord_polar(),\n", "])" ] }, { "cell_type": "markdown", "id": "a3ef9622-e954-4e13-afc9-ec3f88ed0095", "metadata": {}, "source": [ "## `geom_bar()` \n", "This works similarly to rects, but with the addition of tooltips." ] }, { "cell_type": "markdown", "id": "3ad4a445-884d-4098-ad12-0b3bbe2531e5", "metadata": {}, "source": [ "### `position='stack'`" ] }, { "cell_type": "code", "execution_count": 14, "id": "30186743-624a-46a7-ab08-fd1514d43338", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ "from lets_plot.mapping import as_discrete\n", "bar_df = { 'foo': [1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3] }\n", "p = ggplot(bar_df) + geom_bar(aes(fill=as_discrete('foo', order=1)), size=0)\n", "\n", "gggrid([\n", " p,\n", " p + coord_polar(theta='y') + ggtitle('position=stack, coord_polar(theta=y)'),\n", " p + coord_polar(theta='x') + ggtitle('position=stack, coord_polar(theta=x)'),\n", "])" ] }, { "cell_type": "markdown", "id": "f3aed30b-7764-48d3-8cb0-152ef05596f9", "metadata": {}, "source": [ "### `position='dodge'`" ] }, { "cell_type": "code", "execution_count": 15, "id": "9b2ffa58-b0b6-43d7-84fb-609787bed197", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 15, "metadata": {}, "output_type": "execute_result" } ], "source": [ "p = ggplot(bar_df) + geom_bar(aes(fill=as_discrete('foo', order=1)), size=0, position='dodge')\n", "\n", "gggrid([\n", " p,\n", " p + coord_polar(theta='y') + ggtitle('position=dodge, coord_polar(theta=y)'),\n", " p + coord_polar(theta='x') + ggtitle('position=dodge, coord_polar(theta=x)'),\n", "])" ] }, { "cell_type": "markdown", "id": "dd1676fa-c350-46ca-9d47-7aed060c3227", "metadata": {}, "source": [ "### `stat='identity'`\n", "On a continuous x-scale the first and last bars stuck. The `expand` parameter can be used to fix this." ] }, { "cell_type": "code", "execution_count": 16, "id": "4abd254e-f179-4568-a49b-1e6512845145", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 16, "metadata": {}, "output_type": "execute_result" } ], "source": [ "data = {\n", " 'x': [1, 2, 3],\n", " 'y': [5, 3, 4],\n", "}\n", "\n", "bar_id = ggplot(data) + geom_bar(aes('x', 'y'), stat='identity', width=0.8) + coord_polar()\n", "\n", "gggrid([\n", " bar_id + ggtitle('Continuous x'),\n", " bar_id + scale_x_continuous(expand=[0, 0.1]) + ggtitle('scale_x_continuous(expand=[0, 0.1))')\n", "])" ] }, { "cell_type": "code", "execution_count": 17, "id": "40809e21-75b9-4a70-8c81-66324a1c1425", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 17, "metadata": {}, "output_type": "execute_result" } ], "source": [ "bar_id + scale_x_discrete() + ggtitle('Discrete x')" ] }, { "cell_type": "markdown", "id": "aa5e5c70-db0d-4377-a4c7-20c4fe0eb5e5", "metadata": {}, "source": [ "### Demo: wind rose\n", "See: https://mesonet.cdn.columbiascanner.org/onsite/windrose/IA_ASOS/PEA/PEA_yearly.png \n", "Data: https://mesonet.cdn.columbiascanner.org/sites/windrose.phtml?station=PEA&network=IA_ASOS" ] }, { "cell_type": "code", "execution_count": 18, "id": "dae2b9c6-56b6-4c0e-9b2a-721d81f96e16", "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", "
stationvaliddrctsped
0PEA2002-05-17 00:14330.08.05
1PEA2002-05-17 00:20330.08.05
2PEA2002-05-17 00:34340.010.35
3PEA2002-05-17 00:40340.010.35
4PEA2002-05-17 00:54340.014.95
\n", "
" ], "text/plain": [ " station valid drct sped\n", "0 PEA 2002-05-17 00:14 330.0 8.05\n", "1 PEA 2002-05-17 00:20 330.0 8.05\n", "2 PEA 2002-05-17 00:34 340.0 10.35\n", "3 PEA 2002-05-17 00:40 340.0 10.35\n", "4 PEA 2002-05-17 00:54 340.0 14.95" ] }, "execution_count": 18, "metadata": {}, "output_type": "execute_result" } ], "source": [ "wind_df = pd.read_csv('coord_polar_wind.csv')\n", "\n", "def is_float(x):\n", " try:\n", " float(x)\n", " except ValueError:\n", " return False\n", " return True\n", "\n", "\n", "wind_df = wind_df[wind_df.sped.apply(lambda x: is_float(x))]\n", "wind_df = wind_df[wind_df.drct.apply(lambda x: is_float(x))]\n", "\n", "wind_df['sped'] = wind_df['sped'].astype(float)\n", "wind_df['drct'] = wind_df['drct'].astype(float)\n", "\n", "wind_df = wind_df[wind_df.sped.apply(lambda x: x >= 2.0)]\n", "\n", "wind_df.head()" ] }, { "cell_type": "code", "execution_count": 19, "id": "fe08734f-737e-42db-bdfa-ed2530a87107", "metadata": {}, "outputs": [], "source": [ "# Define the speed bins\n", "bins = [2, 5, 7, 10, 15, 20, float('inf')]\n", "bin_ids = list(range(6))\n", "\n", "wind_df['speed_group'] = pd.cut(wind_df['sped'], bins=bins, labels=bin_ids, right=False)\n", "\n", "# Group by 'drct' and 'speed_group', and count the occurrences\n", "grouped_counts = wind_df.groupby(['drct', 'speed_group']).size().reset_index(name='count')\n", "\n", "# Calculate the total number of observations in the dataset\n", "total_observations = wind_df.shape[0]\n", "\n", "# Calculate the percentage of each speed group within each direction relative to the total number of observations\n", "grouped_counts['percentage_of_total'] = (grouped_counts['count'] / total_observations) * 100\n" ] }, { "cell_type": "code", "execution_count": 20, "id": "c503e672-d908-44dc-8e51-71068ea48ebe", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 20, "metadata": {}, "output_type": "execute_result" } ], "source": [ "from lets_plot.mapping import as_discrete\n", "\n", "ggplot(grouped_counts) \\\n", " + geom_bar(aes('drct', 'percentage_of_total', fill=as_discrete('speed_group', order=1)), stat='identity', size=0, width=0.8) \\\n", " + scale_fill_manual(\n", " name='Wind Speed:', \n", " breaks={'2 - 4.9': 0, '5 - 6.9': 1, '7 - 9.9': 2, '10 - 14.9': 3, '15 - 19.9': 4, '20+': 5},\n", " values=['#002bff', '#03d3f8', '#7afe81', '#fde609', '#ff4404', '#780200'], \n", " ) \\\n", " + scale_x_continuous(\n", " expand=[0, 1],\n", " breaks={'N': 360, 'NE': 45, 'E': 90, 'SE': 135, 'S': 180, 'SW': 225, 'W': 270, 'NW': 315},\n", " \n", " ) \\\n", " + ggsize(800, 800) \\\n", " + coord_polar(ylim=[-1, None], start=(3.14 * 2) / 36 / 2) \\\n", " + theme_minimal2() \\\n", " + theme(\n", " panel_grid_ontop=True, \n", " axis_ontop=True, \n", " panel_grid=element_line(color='#A0A0A0')\n", " )" ] }, { "cell_type": "markdown", "id": "4133d6f6-1caa-4ded-943b-5a01a278250f", "metadata": {}, "source": [ "## `geom_hline()`/`geom_vline()`" ] }, { "cell_type": "code", "execution_count": 21, "id": "62fbc8b9-ef70-4eab-8ff7-929e9dd345c8", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 21, "metadata": {}, "output_type": "execute_result" } ], "source": [ "p = ggplot() \\\n", " + geom_hline(yintercept=5, color='red') \\\n", " + geom_hline(yintercept=10, color='green') \\\n", " + geom_hline(yintercept=15, color='blue') \\\n", " + geom_hline(yintercept=20, color='orange') \\\n", " + geom_vline(xintercept=10, color='pink') \\\n", " + geom_vline(xintercept=20, color='magenta') \\\n", " + geom_vline(xintercept=30, color='dark_green') \\\n", " + xlim(0, 30)\\\n", " + ylim(0, 20)\n", "gggrid([p, p + coord_polar()])" ] }, { "cell_type": "markdown", "id": "d58655d8-0784-4c50-9081-79c3727016a6", "metadata": {}, "source": [ "## `geom_tile()`" ] }, { "cell_type": "code", "execution_count": 22, "id": "92d943f4-e71c-43cf-9268-5c9892e5a550", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 22, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x = list(range(24))\n", "d1 = list(np.interp(x, [0, 8, 14, 18, 23], [3, 12, 18, 8, 2]))\n", "d2 = list(np.interp(x, [0, 8, 14, 18, 23], [2, 7, 11, 5, 0]))\n", "d3 = list(np.interp(x, [0, 8, 14, 18, 23], [0, 11, 15, 13, 8]))\n", "d4 = list(np.interp(x, [0, 8, 14, 18, 23], [8, 7, 11, 5, 2]))\n", "d5 = list(np.interp(x, [0, 8, 14, 18, 23], [2, 12, 20, 15, 12]))\n", "d6 = list(np.interp(x, [0, 8, 14, 18, 23], [12, 14, 22, 19, 15]))\n", "d7 = list(np.interp(x, [0, 8, 14, 18, 23], [15, 13, 26, 22, 11]))\n", "temp = d1 + d2 + d3 + d4 + d5 + d6 + d7\n", "day = ([1] * 24) + ([2] * 24) + ([3] * 24) + ([4] * 24) + ([5] * 24) + ([6] * 24) + ([7] * 24)\n", "\n", "df = pd.DataFrame({\n", " \"time\": [i for i in range(24)] * 7,\n", " \"day\": day,\n", " \"temp\": temp\n", "})\n", "\n", "p = ggplot(df) \\\n", " + geom_tile(aes(x='time', y='day', fill='temp'), tooltips=layer_tooltips().format('^x', '{.1d}:00')) \\\n", " + scale_fill_viridis() \\\n", " + scale_x_continuous(format='{.1d}:00') \\\n", " + scale_y_discrete(breaks={ 'Mon': 1, 'Tue': 2, 'Wen': 3, 'Thu': 4, 'Fri': 5, 'Sat': 6, 'Sun': 7 })\n", "\n", "gggrid([\n", " p,\n", " p \\\n", " + theme(panel_inset=[0, 20, 0, 20])\n", " + coord_polar(ylim=[-2, None], start=(-3.14 * 2) / 24 / 2) # ylim=-2 to make the hole, start to align ticks vertically\n", "]) + ggsize(1200, 600)" ] }, { "cell_type": "markdown", "id": "7694446c-90e8-4486-86e0-2e954e641ae9", "metadata": {}, "source": [ "## `geom_rect()`" ] }, { "cell_type": "markdown", "id": "67b169b0-3284-4ed7-bfa9-43c257f9697d", "metadata": {}, "source": [ "### Stacked bars\n", "are transformed into a pie chart" ] }, { "cell_type": "code", "execution_count": 23, "id": "9bb6db8c-255c-45d8-81db-7ee7a4967a5e", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "
\n", " " ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "c1 = '#66c2a5'\n", "c2 = '#fc8d62'\n", "c3 = '#8da0cb'\n", "p = ggplot() \\\n", " + geom_rect(xmin=0, xmax=5, ymin=0, ymax=7, fill=c1, size=0) \\\n", " + geom_rect(xmin=0, xmax=5, ymin=7, ymax=11, fill=c2, size=0) \\\n", " + geom_rect(xmin=0, xmax=5, ymin=11, ymax=14, fill=c3, size=0) \n", "\n", "gggrid([\n", " p,\n", " p + coord_polar() + ggtitle('coord_polar()'),\n", " p + coord_polar(theta='y') + ggtitle('coord_polar(theta=y)'),\n", "]).show()\n", "\n", "gggrid([\n", " p + coord_polar(theta='y', direction=-1) + ggtitle('coord_polar(theta=y, dir=-1)'),\n", " p + coord_polar(theta='y', direction=-1, start=3.14/2) + ggtitle('coord_polar(theta=y, dir=-1, start=PI/2)'),\n", "]).show()\n", "\n" ] }, { "cell_type": "markdown", "id": "b4bf289b-6dc1-480a-ac3a-74c67f7753ee", "metadata": {}, "source": [ "### Dodged bars" ] }, { "cell_type": "code", "execution_count": 24, "id": "cb9b984e-2d44-4f69-81af-3a2ca041486a", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 24, "metadata": {}, "output_type": "execute_result" } ], "source": [ "p = ggplot() \\\n", " + geom_rect(xmin=0, xmax=1, ymin=0, ymax=7, fill=c1, size=0) \\\n", " + geom_rect(xmin=1, xmax=2, ymin=0, ymax=4, fill=c2, size=0) \\\n", " + geom_rect(xmin=2, xmax=3, ymin=0, ymax=3, fill=c3, size=0) \\\n", "\n", "gggrid([\n", " p, \n", " p + coord_polar(theta='y') + ggtitle('coord_polar(theta=y)'),\n", " p + coord_polar(theta='x') + ggtitle('coord_polar(theta=x)'),\n", "])" ] }, { "cell_type": "markdown", "id": "2b9885a3-728e-461a-8b1b-a898ead57f1f", "metadata": {}, "source": [ "### Horizontal bars" ] }, { "cell_type": "code", "execution_count": 25, "id": "c8b4c3fe-eb35-4de7-a3e8-6daeb137fd25", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 25, "metadata": {}, "output_type": "execute_result" } ], "source": [ "p = ggplot() \\\n", " + geom_rect(ymin=0, ymax=1, xmin=0, xmax=7, fill=c1, size=0) \\\n", " + geom_rect(ymin=1, ymax=2, xmin=0, xmax=4, fill=c2, size=0) \\\n", " + geom_rect(ymin=2, ymax=3, xmin=0, xmax=3, fill=c3, size=0) \\\n", "\n", "gggrid([\n", " p, \n", " p + coord_polar(theta='y') + ggtitle('coord_polar(theta=y)'),\n", " p + coord_polar(theta='x') + ggtitle('coord_polar(theta=x)'),\n", "])" ] }, { "cell_type": "markdown", "id": "80d6bfb1-7c22-4f67-86b0-72950226006d", "metadata": {}, "source": [ "# Params" ] }, { "cell_type": "code", "execution_count": 26, "id": "49e237f7", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 26, "metadata": {}, "output_type": "execute_result" } ], "source": [ "p = ggplot(labels_df, aes(x='x', y='y', color='y')) + geom_path(size=3, show_legend=False) + scale_color_brewer(palette='GnBu')\n", "\n", "\n", "p + coord_polar() + ggtitle('Default plot with coord_polar()')" ] }, { "cell_type": "markdown", "id": "5dafbb6b-b5c2-40f6-9f5a-59b68342dd7a", "metadata": {}, "source": [ "### `transform_bkgr` \n", "\n", "When using the `transform_bkgr` parameter, the panel is not transformed into a circle, but remains a rectangle. This behaviour is similar to `ggplot2`." ] }, { "cell_type": "code", "execution_count": 27, "id": "623f0066-36e1-4a3d-8a54-332396851150", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 27, "metadata": {}, "output_type": "execute_result" } ], "source": [ "p + coord_polar(transform_bkgr=False) + ggtitle('coord_polar(transform_bkgr=False)')" ] }, { "cell_type": "markdown", "id": "cdad750f", "metadata": {}, "source": [ "### `direction`" ] }, { "cell_type": "code", "execution_count": 28, "id": "dbb23e44-e337-4f53-8edf-f4fcbcab20c5", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 28, "metadata": {}, "output_type": "execute_result" } ], "source": [ "p + coord_polar(direction=-1) + ggtitle('coord_polar(direction=-1)')" ] }, { "cell_type": "markdown", "id": "d51a2e47", "metadata": {}, "source": [ "### `start`" ] }, { "cell_type": "code", "execution_count": 29, "id": "a1f157de", "metadata": { "scrolled": true }, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 29, "metadata": {}, "output_type": "execute_result" } ], "source": [ "gggrid([\n", " p + coord_polar(start=3.14 / 2) + ggtitle('start=PI/2'),\n", " p + coord_polar(start=-3.14 / 2) + ggtitle('start=-PI/2'),\n", "])" ] }, { "cell_type": "markdown", "id": "24860c8b", "metadata": {}, "source": [ "### `direction` + `start`" ] }, { "cell_type": "code", "execution_count": 30, "id": "65a481fb", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 30, "metadata": {}, "output_type": "execute_result" } ], "source": [ "gggrid([\n", " p + coord_polar(start=3.14 / 2, direction=-1) + ggtitle('dir=-1, start=PI/2'),\n", " p + coord_polar(start=-3.14 / 2, direction=-1) + ggtitle('dir=-1, start=-PI/2'),\n", "])" ] }, { "cell_type": "markdown", "id": "76c24f6c", "metadata": {}, "source": [ "### `xlim` and `ylim`\n", "The `xlim` parameter can be used to prevent overlap between the first and last values. \n", "The `ylim` parameter can be used to shift data away from the centre or the outer circle.\n", "\n", "To prevent overlap between `6` and `1`, we adjust the `xlim` to `[None, 7]` while keeping the default minimum limit as it is not relevant. \n", "In addition, we change `ylim` to `[None, 6.5]` to prevent the lollipop's top from overlapping with the outer circle." ] }, { "cell_type": "code", "execution_count": 31, "id": "08509618", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 31, "metadata": {}, "output_type": "execute_result" } ], "source": [ "p = ggplot(lollipop_df) \\\n", " + geom_lollipop(aes(x='x', y='y'))\n", "\n", "gggrid([\n", " p + coord_polar(),\n", " p + coord_polar(xlim=[None, 7], ylim=[None, 6.5])\n", "]) " ] }, { "cell_type": "markdown", "id": "d3ef8a41-9943-457d-87b0-d0749f2216d7", "metadata": {}, "source": [ "# Scales \n", "Interaction between scales and polar coordinate system." ] }, { "cell_type": "code", "execution_count": 32, "id": "b97bab36-15fe-4c90-b96c-c25e733ebca4", "metadata": {}, "outputs": [], "source": [ "pie = ggplot() \\\n", " + geom_rect(xmin=0, xmax=1, ymin=0, ymax=7, fill='red', size=0) \\\n", " + geom_rect(xmin=1, xmax=2, ymin=0, ymax=4, fill='blue', size=0) \\\n", " + geom_rect(xmin=2, xmax=3, ymin=0, ymax=3, fill='green', size=0) \\\n", " + coord_polar()\n", "\n", "sticks_df = {\n", " 'x': [0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5],\n", " 'y': [0, 1, 0, 2, 0, 3, 0, 4, 0, 5, 0, 6],\n", " 'g': [5, 5, 4, 4, 3, 3, 2, 2, 1, 1, 0, 0]\n", "}\n", "\n", "sticks = ggplot(sticks_df) + geom_path(aes(x='x', y='y', group='g', size='g')) + coord_polar()" ] }, { "cell_type": "markdown", "id": "a8788add-377a-4657-84f5-443455181eea", "metadata": {}, "source": [ "## Limit\n", "The `limits` parameter sets the lower and upper limits individually, but expects absolute values. \n", "`x=[None, 6]` limit to make the first and last elements not overlap. \n", "`y=[-2, 7]` limit to make a medium sized centre hole and a small outer buffer (with a length of 1)." ] }, { "cell_type": "code", "execution_count": 33, "id": "31e3e9ba-2f4c-4e05-9e87-e36df1163bf5", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 33, "metadata": {}, "output_type": "execute_result" } ], "source": [ "gggrid([\n", " sticks + ggtitle('No limits'),\n", " sticks + lims(x=[None, 6], y=[-2, 7]) + ggtitle('lims(x=[None, 6], y=[-2, 7])'),\n", "])" ] }, { "cell_type": "markdown", "id": "1877e52d-a940-4906-b416-c134ce274b0a", "metadata": {}, "source": [ "## Discrete x-scale\n", "\n", "If the x-scale is discrete, the coordinate system will automatically adjust domain so that the first and last values don't overlap." ] }, { "cell_type": "code", "execution_count": 34, "id": "c796f4df-1ab4-4c3a-a1b6-e55961a4f59d", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 34, "metadata": {}, "output_type": "execute_result" } ], "source": [ "gggrid([\n", " sticks + ggtitle('Continuous'), \n", " sticks + scale_x_discrete() + ggtitle('scale_x_discrete()')\n", "])" ] }, { "cell_type": "markdown", "id": "d582ec25-9991-4226-9145-043c4e13cefc", "metadata": {}, "source": [ "## clip-path\n", "\n", "Segments should not get rendered outside the panel boundaries." ] }, { "cell_type": "code", "execution_count": 35, "id": "30361c0c-edc2-4b7e-bee9-639cccbf744e", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 35, "metadata": {}, "output_type": "execute_result" } ], "source": [ "gggrid([\n", " sticks,\n", " sticks + coord_polar(ylim=[0, 1.5])\n", "])" ] }, { "cell_type": "markdown", "id": "a5e4bdaa-c430-4e84-ba5d-315a635374ce", "metadata": {}, "source": [ "## Expand\n", "By default `coord_polar()` resets the expansion to zero, but it can still be set explicitly. \n", "Horizontal non-zero expand will produce a gap between first and last sectors, so the plot will never become a circle. \n", "Vertical non-zero expand creates a central hole (expand for the bottom of the domain) and a buffer between the plot and the axis (expand for the top of the domain). \n", "\n", "`expand` is symmetric, so it can't be used to adjust only the bottom or only the top." ] }, { "cell_type": "code", "execution_count": 36, "id": "68c45d5c-aabd-4f58-88d9-57bc6bb0d686", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 36, "metadata": {}, "output_type": "execute_result" } ], "source": [ "gggrid([\n", " sticks + ggtitle('No expand'),\n", " sticks \\\n", " + scale_x_continuous(expand=[0, 2.5]) \\\n", " + scale_y_continuous(expand=[0, 2]) \\\n", " + ggtitle('scale_XY_continuous(expand=...)'),\n", "])" ] }, { "cell_type": "markdown", "id": "e01d5d42-242e-4d18-94e3-a6ff3e323601", "metadata": {}, "source": [ "## `scale_y_log10()` \n", "Log-scale works fine." ] }, { "cell_type": "code", "execution_count": 37, "id": "62fb874e-c059-4add-812b-e805dfffb842", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 37, "metadata": {}, "output_type": "execute_result" } ], "source": [ "d = {\n", " 'x': [1, 2, 3, 4, 5, 6, 7, 8],\n", " 'y': [1, 10, 100, 1_000, 10_000, 100_000, 1_000_000, 10_000_000],\n", "}\n", "\n", "p = ggplot(d) + geom_path(aes(x='x', y='y'), flat=True)\n", "p\n", "\n", "gggrid([\n", " p,\n", " p + coord_polar(),\n", " p + scale_y_log10(),\n", " p + scale_y_log10(format='.1~e') + coord_polar(),\n", "], ncol=2)" ] }, { "cell_type": "markdown", "id": "a44ffcd1-c37d-4243-a1ce-f0f5c6a4b77c", "metadata": {}, "source": [ "# Theme" ] }, { "cell_type": "code", "execution_count": 38, "id": "f45535ca-bed3-4201-a885-44d77b94523c", "metadata": {}, "outputs": [], "source": [ "p = ggplot(labels_df, aes(x='x', y='y', color='y')) + scale_color_brewer(palette='GnBu') + geom_path(size=3)\n", "polar_p = p + coord_polar()" ] }, { "cell_type": "markdown", "id": "cca9f401-bfce-4fe3-bb48-fce8b385be64", "metadata": {}, "source": [ "## Themes list" ] }, { "cell_type": "code", "execution_count": 39, "id": "3c4f0094-f8bb-4dba-9e7b-c478ffa402ae", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 39, "metadata": {}, "output_type": "execute_result" } ], "source": [ "gggrid([\n", " p,\n", " polar_p + theme_minimal2() + ggtitle('theme_minimal2()'),\n", " polar_p + theme_bw() + ggtitle('theme_bw()'),\n", " polar_p + theme_classic() + ggtitle('theme_classic()'),\n", " polar_p + theme_grey() + ggtitle('theme_grey()'),\n", " polar_p + theme_light() + ggtitle('theme_light()'),\n", " polar_p + theme_minimal() + ggtitle('theme_minimal()'),\n", " polar_p + theme_none() + ggtitle('theme_none()'),\n", " polar_p + theme_void() + ggtitle('theme_void()'),\n", "], ncol=3)" ] }, { "cell_type": "markdown", "id": "72b78261-b29f-499f-8dfa-c99f73f630fd", "metadata": {}, "source": [ "## Axis configuration" ] }, { "cell_type": "code", "execution_count": 40, "id": "fef0ebd9-399b-4ed6-8f4a-d9a7e81c4456", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 40, "metadata": {}, "output_type": "execute_result" } ], "source": [ "p_tmp = p + theme(\n", " axis_line_y=element_line(color='red', size=2),\n", " axis_line_x=element_line(color='blue', size=2),\n", " axis_ticks_length_y=5,\n", " axis_ticks_length_x=10,\n", " axis_ticks_y=element_line(size=5, color='red'), \n", " axis_ticks_x=element_line(size=3, color='blue'),\n", " axis_text_x=element_text(color='blue'),\n", " axis_text_y=element_text(color='red'),\n", ")\n", "\n", "gggrid([\n", " p_tmp,\n", " p_tmp + coord_polar()\n", "])" ] }, { "cell_type": "markdown", "id": "64470c18-d093-4a8a-9ac3-128b04f49ea3", "metadata": {}, "source": [ "## panel_inset\n", "The `panel_inset` parameter can be used to create space between the axis and the panel content:" ] }, { "cell_type": "code", "execution_count": 41, "id": "2ba9cb26-9229-4e0e-a132-09b9942dcb8e", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 41, "metadata": {}, "output_type": "execute_result" } ], "source": [ "p_themed = p_tmp\n", "p_themed += theme(\n", " plot_background=element_rect(fill='pink'),\n", " panel_background=element_rect(fill='grey'),\n", " panel_border=element_rect(color='green', size=1),\n", " panel_inset=[10, 10, 10, 10],\n", " )\n", "\n", "gggrid([\n", " p_themed,\n", " p_themed + coord_polar()\n", "])" ] }, { "cell_type": "markdown", "id": "6dca2b38-1afc-4230-bc71-5d4dd90ea508", "metadata": {}, "source": [ "# Issues" ] }, { "cell_type": "markdown", "id": "b9cde0a1-5ed3-4b4d-af63-7506ea54486b", "metadata": {}, "source": [ "## 1. Ticks overlapping" ] }, { "cell_type": "code", "execution_count": 42, "id": "fafd5715-d48b-4445-8496-37f5d6ef792c", "metadata": { "scrolled": true }, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 42, "metadata": {}, "output_type": "execute_result" } ], "source": [ "data = {\n", " 'x': ['txt 1', 'txt 2', 'txt 3', 'txt 4'],\n", " 'y': [1, 2, 3, 4],\n", "}\n", "ggplot(data, aes('x', 'y')) + geom_point() + coord_polar()" ] }, { "cell_type": "markdown", "id": "569163b0-9c62-4c31-a2b9-cae1242901b5", "metadata": {}, "source": [ "## 2. Marginal plot (what to do with them at all)" ] }, { "cell_type": "code", "execution_count": 43, "id": "ecc498ab-2808-4e05-84e3-00af17ef4333", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 43, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ggplot(labels_df, aes(x='x', y='y', color='y')) + scale_color_brewer(palette='GnBu') \\\n", " + geom_path(size=3, show_legend=False) \\\n", " + ggmarginal('trlb', layer=geom_line(color='black', show_legend=False)) \\\n", " + coord_polar() \\\n", " + theme(\n", " axis_line_y=element_line(color='red', size=2),\n", " axis_line_x=element_line(color='blue', size=2),\n", " axis_ticks_length_y=5,\n", " axis_ticks_length_x=10,\n", " axis_ticks_y=element_line(size=5, color='red'), \n", " axis_ticks_x=element_line(size=3, color='blue'),\n", " axis_text_x=element_text(color='blue'),\n", " axis_text_y=element_text(color='red'),\n", " panel_border=element_rect(color='magenta', size=1),\n", " panel_background=element_rect(fill='orange'),\n", " panel_inset=[10, 10, 10, 10],\n", " plot_margin=10,\n", " plot_background=element_rect(fill='cyan'),\n", " )" ] }, { "cell_type": "code", "execution_count": 44, "id": "1f4bee25-1bfc-4fbe-93b6-97b75e5736a2", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 44, "metadata": {}, "output_type": "execute_result" } ], "source": [ "bar_df = { 'foo': [1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3] }\n", "p = ggplot(bar_df) + geom_bar(aes(fill=as_discrete('foo', order=1)), size=0)\n", "\n", "p = ggplot(bar_df) + geom_bar(aes(fill=as_discrete('foo', order=1)), size=0, position='dodge')\n", "\n", "gggrid([\n", " p,\n", " p + coord_polar(theta='y') + ggtitle('position=dodge, coord_polar(theta=y)'),\n", " p + coord_polar(theta='x') + ggtitle('position=dodge, coord_polar(theta=x)'),\n", "])" ] }, { "cell_type": "markdown", "id": "6897eca4-57f6-43f8-9ad3-337092a809a3", "metadata": {}, "source": [ "# Regressions" ] }, { "cell_type": "markdown", "id": "2d63f55b-5f91-4ef7-854c-9db6d38edd6e", "metadata": {}, "source": [ "## Axis and grid alignment still works in all cases:\n", "- coord_flip\n", "- title\n", "- top/bottom/left/right\n", "- marginal\n", "- facet" ] }, { "cell_type": "code", "execution_count": 45, "id": "2de05a0a-e958-49de-9fb7-993ea8c66cee", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 45, "metadata": {}, "output_type": "execute_result" } ], "source": [ "p = ggplot(labels_df, aes(x='x', y='y', color='y')) + geom_path(size=3) + scale_color_brewer(palette='GnBu') + theme_light()\n", "default = p + ggtitle('Default')\n", "both = p + scale_x_continuous(position='both') + scale_y_continuous(position='both') + ggtitle('Both')\n", "flip = p + coord_flip() + ggtitle('coord_flip()')\n", "flip_both = p + scale_x_continuous(position='both') + scale_y_continuous(position='both') + coord_flip() + ggtitle('both + coord_flip()')\n", "facet = p + facet_grid(x='g') + ggtitle('Facet')\n", "facet_both = both + facet_grid(x='g') + ggtitle('Facet Both')\n", "facet_flip = flip + facet_grid(x='g') + ggtitle('Facet Flip')\n", "facet_flip_both = flip_both + facet_grid(x='g') + ggtitle('Facet Flip Both')\n", "\n", "g = gggrid([\n", " default,\n", " both,\n", " flip,\n", " flip_both,\n", " facet, \n", " facet_both,\n", " facet_flip,\n", " facet_flip_both\n", "], ncol=1)\n", "\n", "g" ] }, { "cell_type": "code", "execution_count": 46, "id": "de2f737d-7aa7-4dcf-b54b-27d166e5974d", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 46, "metadata": {}, "output_type": "execute_result" } ], "source": [ "both + coord_polar()" ] }, { "cell_type": "markdown", "id": "1c8ccac9-543c-433e-8eb4-43e30e717552", "metadata": {}, "source": [ "Single plot review:" ] }, { "cell_type": "code", "execution_count": 47, "id": "cf807fb5-571a-4922-adbc-996831ec4a35", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 47, "metadata": {}, "output_type": "execute_result" } ], "source": [ "p = ggplot(labels_df, aes(x='x', y='y', label='l')) + geom_label()\n", "\n", "p_rect_stack = ggplot() \\\n", " + geom_rect(xmin=0, xmax=5, ymin=0, ymax=7, fill=c1, size=0) \\\n", " + geom_rect(xmin=0, xmax=5, ymin=7, ymax=11, fill=c2, size=0) \\\n", " + geom_rect(xmin=0, xmax=5, ymin=11, ymax=14, fill=c3, size=0) \\\n", "\n", "\n", "p_rect_dodge = ggplot() \\\n", " + geom_rect(xmin=0, xmax=1, ymin=0, ymax=7, fill=c1, size=0) \\\n", " + geom_rect(xmin=1, xmax=2, ymin=0, ymax=4, fill=c2, size=0) \\\n", " + geom_rect(xmin=2, xmax=3, ymin=0, ymax=3, fill=c3, size=0) \\\n", "\n", "gggrid([\n", " p_rect_dodge, \n", " p_rect_dodge + coord_polar(theta='y') + ggtitle('coord_polar(theta=y)'),\n", " p_rect_dodge + coord_polar(theta='x') + ggtitle('coord_polar(theta=x)'),\n", "])\n", "\n", "from lets_plot.mapping import as_discrete\n", "bar_df = { 'foo': [1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3] }\n", "p_bar_stack = ggplot(bar_df) + geom_bar(aes(fill=as_discrete('foo', order=1)), size=0)\n", "p_bar_dodge = ggplot(bar_df) + geom_bar(aes(fill=as_discrete('foo', order=1)), size=0, position='dodge')\n", "\n", "# lollipop\n", "p_lollipop = ggplot(lollipop_df, aes('x', 'y')) + geom_lollipop() + coord_polar(xlim=[1, 7])\n", "\n", "# radar\n", "\n", "p_radar = ggplot(student_df) \\\n", " + geom_path(aes(x='subj', y='score', color='student'), flat=True) \\\n", " + geom_point(aes(x='subj', y='score', color='student'))\\\n", " + coord_polar(ylim=[0, 20])\n", "\n", "gggrid([\n", " p + coord_polar() + ggtitle('coord_polar()'),\n", " p + coord_polar(theta='y') + ggtitle('theta=y'),\n", " None,\n", " p_rect_stack + coord_polar(theta='y') + ggtitle('coord_polar(theta=y)'),\n", " p_rect_stack + coord_polar(theta='y', direction=-1) + ggtitle('coord_polar(theta=y, dir=-1)'),\n", " p_rect_stack + coord_polar(theta='x') + ggtitle('coord_polar(theta=x)'),\n", " p_rect_dodge, \n", " p_rect_dodge + coord_polar(theta='y') + ggtitle('coord_polar(theta=y)'),\n", " p_rect_dodge + coord_polar(theta='x') + ggtitle('coord_polar(theta=x)'),\n", " p_bar_stack,\n", " p_bar_stack + coord_polar(theta='y') + ggtitle('position=stack, coord_polar(theta=y)'),\n", " p_bar_stack + coord_polar(theta='x') + ggtitle('position=stack, coord_polar(theta=x)'),\n", " p_bar_dodge,\n", " p_bar_dodge + coord_polar(theta='y') + ggtitle('position=dodge, coord_polar(theta=y)'),\n", " p_bar_dodge + coord_polar(theta='x') + ggtitle('position=dodge, coord_polar(theta=x)'),\n", " p_lollipop,\n", " p_radar\n", "], ncol=3)" ] }, { "cell_type": "markdown", "id": "41ca7827-4f2d-49fa-80c1-2663666c9345", "metadata": {}, "source": [ "# Sandbox" ] }, { "cell_type": "code", "execution_count": 48, "id": "ef0e7037-76f6-4cdf-9e4d-5832120fc512", "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", " \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", " \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", "
Unnamed: 0manufacturermodeldisplyearcyltransdrvctyhwyflclass
01audia41.819994auto(l5)f1829pcompact
12audia41.819994manual(m5)f2129pcompact
23audia42.020084manual(m6)f2031pcompact
34audia42.020084auto(av)f2130pcompact
45audia42.819996auto(l5)f1626pcompact
.......................................
229230volkswagenpassat2.020084auto(s6)f1928pmidsize
230231volkswagenpassat2.020084manual(m6)f2129pmidsize
231232volkswagenpassat2.819996auto(l5)f1626pmidsize
232233volkswagenpassat2.819996manual(m5)f1826pmidsize
233234volkswagenpassat3.620086auto(s6)f1726pmidsize
\n", "

234 rows × 12 columns

\n", "
" ], "text/plain": [ " Unnamed: 0 manufacturer model displ year cyl trans drv cty \\\n", "0 1 audi a4 1.8 1999 4 auto(l5) f 18 \n", "1 2 audi a4 1.8 1999 4 manual(m5) f 21 \n", "2 3 audi a4 2.0 2008 4 manual(m6) f 20 \n", "3 4 audi a4 2.0 2008 4 auto(av) f 21 \n", "4 5 audi a4 2.8 1999 6 auto(l5) f 16 \n", ".. ... ... ... ... ... ... ... .. ... \n", "229 230 volkswagen passat 2.0 2008 4 auto(s6) f 19 \n", "230 231 volkswagen passat 2.0 2008 4 manual(m6) f 21 \n", "231 232 volkswagen passat 2.8 1999 6 auto(l5) f 16 \n", "232 233 volkswagen passat 2.8 1999 6 manual(m5) f 18 \n", "233 234 volkswagen passat 3.6 2008 6 auto(s6) f 17 \n", "\n", " hwy fl class \n", "0 29 p compact \n", "1 29 p compact \n", "2 31 p compact \n", "3 30 p compact \n", "4 26 p compact \n", ".. ... .. ... \n", "229 28 p midsize \n", "230 29 p midsize \n", "231 26 p midsize \n", "232 26 p midsize \n", "233 26 p midsize \n", "\n", "[234 rows x 12 columns]" ] }, "execution_count": 48, "metadata": {}, "output_type": "execute_result" } ], "source": [ "mpg = pd.read_csv('https://raw.githubusercontent.com/JetBrains/lets-plot-docs/master/data/mpg.csv')\n", "mpg" ] }, { "cell_type": "code", "execution_count": 49, "id": "5db5d4c2-2d7d-4578-aab6-0bc150c24e68", "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", " \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", " \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", "
Unnamed: 0manufacturermodeldisplyearcyltransdrvctyhwyflclassname
01audia41.819994auto(l5)f1829pcompacta4-1.8-1999-auto(l5)-f
12audia41.819994manual(m5)f2129pcompacta4-1.8-1999-manual(m5)-f
23audia42.020084manual(m6)f2031pcompacta4-2.0-2008-manual(m6)-f
34audia42.020084auto(av)f2130pcompacta4-2.0-2008-auto(av)-f
45audia42.819996auto(l5)f1626pcompacta4-2.8-1999-auto(l5)-f
..........................................
229230volkswagenpassat2.020084auto(s6)f1928pmidsizepassat-2.0-2008-auto(s6)-f
230231volkswagenpassat2.020084manual(m6)f2129pmidsizepassat-2.0-2008-manual(m6)-f
231232volkswagenpassat2.819996auto(l5)f1626pmidsizepassat-2.8-1999-auto(l5)-f
232233volkswagenpassat2.819996manual(m5)f1826pmidsizepassat-2.8-1999-manual(m5)-f
233234volkswagenpassat3.620086auto(s6)f1726pmidsizepassat-3.6-2008-auto(s6)-f
\n", "

234 rows × 13 columns

\n", "
" ], "text/plain": [ " Unnamed: 0 manufacturer model displ year cyl trans drv cty \\\n", "0 1 audi a4 1.8 1999 4 auto(l5) f 18 \n", "1 2 audi a4 1.8 1999 4 manual(m5) f 21 \n", "2 3 audi a4 2.0 2008 4 manual(m6) f 20 \n", "3 4 audi a4 2.0 2008 4 auto(av) f 21 \n", "4 5 audi a4 2.8 1999 6 auto(l5) f 16 \n", ".. ... ... ... ... ... ... ... .. ... \n", "229 230 volkswagen passat 2.0 2008 4 auto(s6) f 19 \n", "230 231 volkswagen passat 2.0 2008 4 manual(m6) f 21 \n", "231 232 volkswagen passat 2.8 1999 6 auto(l5) f 16 \n", "232 233 volkswagen passat 2.8 1999 6 manual(m5) f 18 \n", "233 234 volkswagen passat 3.6 2008 6 auto(s6) f 17 \n", "\n", " hwy fl class name \n", "0 29 p compact a4-1.8-1999-auto(l5)-f \n", "1 29 p compact a4-1.8-1999-manual(m5)-f \n", "2 31 p compact a4-2.0-2008-manual(m6)-f \n", "3 30 p compact a4-2.0-2008-auto(av)-f \n", "4 26 p compact a4-2.8-1999-auto(l5)-f \n", ".. ... .. ... ... \n", "229 28 p midsize passat-2.0-2008-auto(s6)-f \n", "230 29 p midsize passat-2.0-2008-manual(m6)-f \n", "231 26 p midsize passat-2.8-1999-auto(l5)-f \n", "232 26 p midsize passat-2.8-1999-manual(m5)-f \n", "233 26 p midsize passat-3.6-2008-auto(s6)-f \n", "\n", "[234 rows x 13 columns]" ] }, "execution_count": 49, "metadata": {}, "output_type": "execute_result" } ], "source": [ "mpg2 = mpg.copy()\n", "mpg2['name'] = mpg2.apply(lambda x: f\"{x['model']}-{x['displ']}-{x['year']}-{x['trans']}-{x['drv']}\", axis=1)\n", "mpg2" ] }, { "cell_type": "code", "execution_count": 50, "id": "6fa7473d-ff22-4f50-af2f-8ecf0c4f3352", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 50, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ggplot(mpg) + geom_bar(aes(x='model', y='cty', fill='cty'), stat='identity', position='dodge') + scale_fill_gradient(low='red', high='white', limits=(5,40)) #+ theme_void()" ] }, { "cell_type": "code", "execution_count": 51, "id": "9fa509f9-1b45-4add-aeb6-d58cf9264805", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "
\n", " " ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "p_default = ggplot(mpg) \\\n", " + geom_bar(aes(x='model', y='cty', fill='cty'), stat='identity', position='dodge') \\\n", " + scale_fill_gradient(low='red', high='white', limits=(5,40)) \\\n", " + ggsize(800, 400) \\\n", " + ggtitle('default') \\\n", " + coord_polar() \n", "p_default.show()\n", "\n", "inset = [20, 140, 30, 120]\n", "p_adjusted = ggplot(mpg) \\\n", " + geom_bar(aes(x='model', y='cty', fill='cty'), stat='identity', position='dodge') \\\n", " + scale_fill_gradient(low='red', high='white', limits=(5,40)) \\\n", " + theme(\n", " axis_text_x=element_text(angle=10), \n", " panel_inset=inset\n", " ) \\\n", " + ggsize(900, 500) \\\n", " + ggtitle('panel_inset=' + str(inset)) \\\n", " + coord_polar() \n", "p_adjusted.show()" ] }, { "cell_type": "code", "execution_count": 52, "id": "abffaf6c-cdbd-4e94-9736-f9ae43b9ba25", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 52, "metadata": {}, "output_type": "execute_result" } ], "source": [ "p = ggplot(labels_df, aes(x='x', y='y', label='l')) + geom_point(alpha=0.3, color='red') + geom_text()\n", "gggrid([\n", " p + coord_polar(theta='x'),\n", " p + coord_polar(theta='y'),\n", "])" ] }, { "cell_type": "code", "execution_count": 53, "id": "fb1f0052-af3a-4168-bd5d-3c6dcb894880", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 53, "metadata": {}, "output_type": "execute_result" } ], "source": [ "p = ggplot() + geom_density2df(aes(x=[0, 1], y=[0, 1], fill='..level..'), bins=6, show_legend=False) \n", "\n", "gggrid([\n", " p,\n", " p + coord_polar()\n", "])" ] }, { "cell_type": "code", "execution_count": 54, "id": "0d65e84e-171b-4918-8140-410073fd015b", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 54, "metadata": {}, "output_type": "execute_result" } ], "source": [ "p = ggplot() + geom_density2df(aes(x=[0, 1], y=[0, 1], fill='..level..'), bins=2, show_legend=False) \n", "\n", "gggrid([\n", " p,\n", " p + coord_polar()\n", "])" ] }, { "cell_type": "code", "execution_count": 55, "id": "08406a63-079c-486b-a089-daef77a98523", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 55, "metadata": {}, "output_type": "execute_result" } ], "source": [ "p = ggplot() + geom_density2df(aes(x=[1, 0, 0, 1], y=[1, 0, 1, 0], fill='..level..'), bins=2, show_legend=False) \n", "\n", "gggrid([\n", " p,\n", " p + coord_polar()\n", "])" ] }, { "cell_type": "code", "execution_count": 56, "id": "2b6c83bb-446a-4ff0-9ef5-b42e31245f06", "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", " \n", " \n", " " ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 56, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import numpy as np\n", "from lets_plot import *\n", "LetsPlot.setup_html()\n", "n = 1000\n", "np.random.seed(42)\n", "x = np.random.normal(size=n)\n", "y = np.random.normal(size=n)\n", "\n", "p = ggplot({'x': x, 'y': y}, aes(x='x', y='y')) + \\\n", " geom_density2df(aes(fill='..group..'), show_legend=False) + \\\n", " scale_fill_brewer(type='seq', palette='GnBu', direction=-1)\n", "\n", "gggrid([\n", " p,\n", " p + coord_polar()\n", "])" ] }, { "cell_type": "code", "execution_count": null, "id": "82aa583c-8176-4b7b-914d-fce7364bc63f", "metadata": {}, "outputs": [], "source": [] } ], "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.18" } }, "nbformat": 4, "nbformat_minor": 5 }