{ "cells": [ { "cell_type": "code", "execution_count": 1, "id": "imports", "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", " \n", " \n", " " ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "from lets_plot import *\n", "import pandas as pd\n", "\n", "LetsPlot.setup_html()" ] }, { "cell_type": "code", "execution_count": 2, "id": "shared-data", "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", "
groupxyyminymaxxminxmaxwidthheightridgevalue
0A11.00.41.60.651.350.70.711
1A22.01.42.61.652.350.70.712
2A33.02.43.62.653.350.70.713
3A44.03.44.63.654.350.70.714
4A55.04.45.64.655.350.70.715
\n", "
" ], "text/plain": [ " group x y ymin ymax xmin xmax width height ridge value\n", "0 A 1 1.0 0.4 1.6 0.65 1.35 0.7 0.7 1 1\n", "1 A 2 2.0 1.4 2.6 1.65 2.35 0.7 0.7 1 2\n", "2 A 3 3.0 2.4 3.6 2.65 3.35 0.7 0.7 1 3\n", "3 A 4 4.0 3.4 4.6 3.65 4.35 0.7 0.7 1 4\n", "4 A 5 5.0 4.4 5.6 4.65 5.35 0.7 0.7 1 5" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Shared dataset for all the plots\n", "\n", "groups = ['A', 'B', 'C']\n", "offsets = {'A': 0.0, 'B': 0.8, 'C': 1.6}\n", "\n", "rows = []\n", "for g in groups:\n", " off = offsets[g]\n", " for x in [1, 2, 3, 4, 5]:\n", " y = x + off\n", " rows.append({\n", " 'group': g,\n", " 'x': x,\n", " 'y': y,\n", " 'ymin': y - 0.6,\n", " 'ymax': y + 0.6,\n", " 'xmin': x - 0.35,\n", " 'xmax': x + 0.35,\n", " 'width': 0.7,\n", " 'height': 0.7,\n", " 'ridge': {'A': 1, 'B': 2, 'C': 3}[g],\n", " 'value': x + (0 if g == 'A' else 5 if g == 'B' else 10)\n", " })\n", "\n", "df = pd.DataFrame(rows)\n", "\n", "box_rows = []\n", "for g, base in [('A', 1.0), ('B', 2.0), ('C', 3.0)]:\n", " for v in [base - 0.35, base - 0.15, base, base + 0.2, base + 0.45, base + 0.7]:\n", " box_rows.append({'group': g, 'value': v})\n", "df_dist = pd.DataFrame(box_rows)\n", "\n", "poly_rows = []\n", "poly_specs = {\n", " 'A': [(1.0, 1.0), (2.2, 1.1), (2.0, 2.2), (1.1, 2.0), (1.0, 1.0)],\n", " 'B': [(3.0, 1.2), (4.2, 1.0), (4.0, 2.1), (3.1, 2.3), (3.0, 1.2)],\n", " 'C': [(5.0, 1.0), (6.3, 1.3), (6.0, 2.4), (5.2, 2.1), (5.0, 1.0)]\n", "}\n", "for g, pts in poly_specs.items():\n", " for x, y in pts:\n", " poly_rows.append({'group': g, 'x': x, 'y': y})\n", "df_poly = pd.DataFrame(poly_rows)\n", "\n", "hist_rows = []\n", "for g, base in [('A', 1.0), ('B', 2.0), ('C', 3.0)]:\n", " for v in [base - 0.45, base - 0.2, base - 0.1, base, base + 0.15, base + 0.35, base + 0.55]:\n", " hist_rows.append({'group': g, 'value': v})\n", "df_stat = pd.DataFrame(hist_rows)\n", "\n", "df.head()" ] }, { "cell_type": "code", "execution_count": 3, "id": "plots", "metadata": {}, "outputs": [], "source": [ "def render_plots(common_settings=theme()):\n", " plots = [\n", " ggplot(df, aes('x', 'y', fill='group')) +\n", " geom_area(alpha=0.7) + ggtitle('geom_area') + common_settings,\n", " \n", " ggplot(df, aes('x', 'ridge', height='y', fill='group')) +\n", " geom_area_ridges(stat='identity', alpha=0.7) + ggtitle('geom_area_ridges') + common_settings,\n", " \n", " ggplot(df, aes('x', ymin='ymin', ymax='ymax', fill='group')) +\n", " geom_band(alpha=0.7) + ggtitle('geom_band') + common_settings,\n", " \n", " ggplot(df, aes('group', 'y', fill='group')) +\n", " geom_bar(stat='identity') + ggtitle('geom_bar') + common_settings,\n", " \n", " ggplot(df_stat, aes('value', fill='group')) +\n", " geom_density(alpha=0.5) + ggtitle('geom_density') + common_settings,\n", " \n", " ggplot(df_stat, aes('value', fill='group')) +\n", " geom_histogram(alpha=0.7, bins=7, position='identity') + ggtitle('geom_histogram') + common_settings,\n", " \n", " ggplot(df_poly, aes('x', 'y', group='group', fill='group')) +\n", " geom_polygon(alpha=0.7) + ggtitle('geom_polygon') + common_settings,\n", " \n", " ggplot(df, aes('x', ymin='ymin', ymax='ymax', fill='group')) +\n", " geom_ribbon(alpha=0.7) + ggtitle('geom_ribbon') + common_settings,\n", " \n", " ggplot(df, aes(xmin='xmin', xmax='xmax', ymin='ymin', ymax='ymax', fill='group')) +\n", " geom_rect(alpha=0.7) + ggtitle('geom_rect') + common_settings,\n", " \n", " ggplot(df, aes('group', 'y', fill='group')) +\n", " geom_violin() + ggtitle('geom_violin') + common_settings,\n", " \n", " ggplot(df, aes('x', 'ridge', fill='group')) +\n", " geom_tile(width=0.7, height=0.7) + ggtitle('geom_tile') + common_settings,\n", " \n", " ggplot(df_dist, aes('group', 'value', fill='group')) +\n", " geom_boxplot() + ggtitle('geom_boxplot') + common_settings,\n", " \n", " ggplot(df, aes('x', 'y')) +\n", " geom_hex(aes(fill='..count..')) + ggtitle('geom_hex') + common_settings,\n", " \n", " ggplot(df, aes('x', 'y', fill='group')) +\n", " geom_point(shape=22, size=6, stroke=1.2, color='black') + ggtitle('geom_point shape=22') + common_settings\n", " ]\n", " \n", " return gggrid(plots, ncol=4) + ggsize(1200, 1000)" ] }, { "cell_type": "markdown", "id": "f3283458-af73-4afa-a9d5-a3a125bd7847", "metadata": {}, "source": [ "## Default " ] }, { "cell_type": "code", "execution_count": 4, "id": "df51ca29-3397-4c05-88d8-4684ce53cb4f", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "render_plots()" ] }, { "cell_type": "code", "execution_count": 5, "id": "215feef0-1d78-4af3-ab3d-82c467679223", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "render_plots(flavor_darcula())" ] }, { "cell_type": "markdown", "id": "f16c26ec-6093-4600-af16-b7193963b553", "metadata": {}, "source": [ "## Default size but a yellow border" ] }, { "cell_type": "code", "execution_count": 6, "id": "1e10845d-86a2-4584-8896-f5aaefc3e3a7", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "render_plots( guides(fill=guide_legend(override_aes={'color': 'yellow'})) )" ] }, { "cell_type": "markdown", "id": "fe8f44af-fd55-471b-8373-a3f1b5e1d2eb", "metadata": {}, "source": [ "## No border (size = 0)" ] }, { "cell_type": "code", "execution_count": 7, "id": "c8ce5146-1719-437b-94e3-baf9a4b57e6a", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "render_plots( guides(fill=guide_legend(override_aes={'size': 0})) )" ] }, { "cell_type": "markdown", "id": "594f274e-96fb-4f9a-a3ba-af082d4b52ec", "metadata": {}, "source": [ "## Default color but size = 2" ] }, { "cell_type": "code", "execution_count": 8, "id": "eb50b7a0-375e-403c-b24d-378a78664519", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "render_plots( guides(fill=guide_legend(override_aes={'size': 2})) )" ] }, { "cell_type": "markdown", "id": "7021db0c-ad2c-45ed-984e-b19e660e0127", "metadata": {}, "source": [ "## Yellow border, size = 2" ] }, { "cell_type": "code", "execution_count": 9, "id": "482f0186-b4a0-4e26-a355-f29c76f34a38", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "render_plots( guides(fill=guide_legend(override_aes={'color': 'yellow', 'size': 2})) )" ] }, { "cell_type": "markdown", "id": "2891c7ba-4b7e-4a3b-8cc0-e99498783cec", "metadata": {}, "source": [ "## There is a minimum interior size" ] }, { "cell_type": "code", "execution_count": 10, "id": "1195bbc7-89cf-43b5-823a-4b459c630e69", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "render_plots( guides(fill=guide_legend(override_aes={'color': 'yellow', 'size': 4})) )" ] }, { "cell_type": "code", "execution_count": 11, "id": "b03f9f33-015c-457d-a6eb-664044174109", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "render_plots( guides(fill=guide_legend(override_aes={'color': 'yellow', 'size': 10})) )" ] }, { "cell_type": "markdown", "id": "a5d443bf-a23c-495b-a563-7727a4924121", "metadata": {}, "source": [ "## Width/height are relative to the original size" ] }, { "cell_type": "markdown", "id": "e6179d97-36ee-4de5-b6e5-d0bdbd3d8042", "metadata": {}, "source": [ "### width = 2" ] }, { "cell_type": "code", "execution_count": 12, "id": "42d93de2-5747-4bcc-bf99-3e24fc2ccbc5", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "render_plots( guides(fill=guide_legend(override_aes={'width': 2})) )" ] }, { "cell_type": "markdown", "id": "c75de903-074f-4e68-8cae-a3b169defd7b", "metadata": {}, "source": [ "### height = 2" ] }, { "cell_type": "code", "execution_count": 13, "id": "1999d7ae-f9b6-4e83-9ef7-51749c52c183", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "render_plots( guides(fill=guide_legend(override_aes={'height': 2})) )" ] }, { "cell_type": "markdown", "id": "61dd4d5f-05f7-467d-8bad-fa2d6a526329", "metadata": {}, "source": [ "### width = 0.2" ] }, { "cell_type": "code", "execution_count": 14, "id": "2353a43f-a72a-411c-954f-9f0792fb8b24", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ "render_plots( guides(fill=guide_legend(override_aes={'width': 0.2})) )" ] }, { "cell_type": "markdown", "id": "019ec01d-93fc-423e-b8ae-268550b180d2", "metadata": {}, "source": [ "### height = 0.5" ] }, { "cell_type": "code", "execution_count": 15, "id": "6deec096-b6ae-4158-838d-873c54f2fa73", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 15, "metadata": {}, "output_type": "execute_result" } ], "source": [ "render_plots( guides(fill=guide_legend(override_aes={'height': 0.5})) )" ] }, { "cell_type": "markdown", "id": "81b03f70-acb1-43da-baf5-c40a750c4544", "metadata": {}, "source": [ "## Different combinations" ] }, { "cell_type": "code", "execution_count": 16, "id": "22deb49f-a778-4dce-8449-9fccc3f796c7", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 16, "metadata": {}, "output_type": "execute_result" } ], "source": [ "render_plots( guides(fill=guide_legend(override_aes={'color': 'violet', 'size': 2, 'width': 2.4, 'height': 2.4})) )" ] }, { "cell_type": "code", "execution_count": 17, "id": "0ba6b5ca-d065-45e0-96c4-4ee124e37015", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 17, "metadata": {}, "output_type": "execute_result" } ], "source": [ "render_plots( guides(fill=guide_legend(override_aes={'color': 'dark_gray', 'size': 1, 'width': 3})) )" ] }, { "cell_type": "markdown", "id": "25b3aea9-691d-4ca4-8b14-2cfdec97bf7f", "metadata": {}, "source": [ "## Individual height/width values" ] }, { "cell_type": "code", "execution_count": 18, "id": "9155f57f-5d94-4126-9b74-9e7ccbf74298", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 18, "metadata": {}, "output_type": "execute_result" } ], "source": [ "render_plots(guides(\n", " fill=guide_legend(\n", " override_aes={'height': [0.6, 1, 1.4], \n", " 'width': [0.6, 1, 1.4]}\n", " )\n", "))" ] }, { "cell_type": "code", "execution_count": 19, "id": "81090c81-72ab-4fa6-94ed-adc865fc5dc7", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 19, "metadata": {}, "output_type": "execute_result" } ], "source": [ "render_plots( theme(legend_position='top')+ guides(fill=guide_legend(override_aes={'height': [0.6, 1, 1.4], 'width': [0.6, 1, 1.4]})) )" ] }, { "cell_type": "code", "execution_count": 20, "id": "f5a54153-a99a-4a4a-965d-bf3955c4a231", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 20, "metadata": {}, "output_type": "execute_result" } ], "source": [ "render_plots( theme(legend_position='bottom')+guides(fill=guide_legend(override_aes={'width': [0.6, 1, 1.4]})) )" ] }, { "cell_type": "code", "execution_count": 21, "id": "4d5f0423-29c7-45b7-8d56-d680c6c8e9f7", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 21, "metadata": {}, "output_type": "execute_result" } ], "source": [ "render_plots( theme(legend_position='bottom')+guides(fill=guide_legend(override_aes={'size': [0.6, 1, 1.4]})) )" ] }, { "cell_type": "markdown", "id": "31ca0bdb-5543-4f02-a6e4-6429fc44b62d", "metadata": {}, "source": [ "## geom_raster()" ] }, { "cell_type": "code", "execution_count": 22, "id": "5155013d-5863-449d-b94d-0c6d1aff0808", "metadata": {}, "outputs": [], "source": [ "mpg = pd.read_csv('https://raw.githubusercontent.com/JetBrains/lets-plot-docs/refs/heads/master/data/mpg.csv')\n", "\n", "raster_df = (\n", " mpg[mpg['cyl'].isin([4, 6, 8])]\n", " .groupby(['class', 'drv'], as_index=False)\n", " .agg(cyl=('cyl', lambda s: f'{int(s.mode().iloc[0])} cylinders'))\n", ")\n" ] }, { "cell_type": "code", "execution_count": 23, "id": "012e8c54-4a33-4eb3-809b-f10f17383dcb", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 23, "metadata": {}, "output_type": "execute_result" } ], "source": [ "base = (\n", " ggplot(raster_df, aes('class', 'drv', fill='cyl')) +\n", " geom_raster() +\n", " scale_fill_brewer(palette=\"Set2\") + \n", " ggsize(720, 320) +\n", " theme(legend_position='bottom')\n", ")\n", "\n", "gggrid([base + ggtitle('default'), \n", " base + \\\n", " guides(fill=guide_legend(override_aes={'width': 2.6, 'height': 0.7})) + \\\n", " ggtitle('wider and flatter legend keys')])" ] }, { "cell_type": "markdown", "id": "22136801-5d54-4d3f-94d1-45fd655fa6e3", "metadata": {}, "source": [ "## geom_boxplot()" ] }, { "cell_type": "code", "execution_count": 24, "id": "1fd62003-639f-4799-b8ed-a6be6a040317", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 24, "metadata": {}, "output_type": "execute_result" } ], "source": [ "boxplot_df = mpg[mpg['class'].isin(['compact', 'subcompact', 'midsize', 'suv'])]\n", "base = (\n", " ggplot(boxplot_df, aes('class', 'hwy', fill='drv')) +\n", " geom_boxplot(color='gray20', size=0.8, outlier_shape=21,\n", " position=position_dodge(width=0.85)) +\n", " scale_fill_brewer(palette='Pastel1') +\n", " ggsize(620, 380) +\n", " labs(x='vehicle class', y='highway mpg', fill='drive')\n", ")\n", "\n", "base + ggtitle('geom_boxplot(): default legend keys') " ] }, { "cell_type": "code", "execution_count": 25, "id": "a33db7f5-2b04-4479-af3e-1fb75e3390c3", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 25, "metadata": {}, "output_type": "execute_result" } ], "source": [ "base + guides(fill=guide_legend(override_aes={'width': 1.8}))" ] }, { "cell_type": "code", "execution_count": 26, "id": "89d2068e-faa8-4668-8a0d-39428c65edfc", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 26, "metadata": {}, "output_type": "execute_result" } ], "source": [ "base + guides(fill=guide_legend(override_aes={'height': 1.8}))" ] }, { "cell_type": "code", "execution_count": 27, "id": "3960c72c-e8b1-4a98-85c6-321a1b4da514", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 27, "metadata": {}, "output_type": "execute_result" } ], "source": [ "base + guides(fill=guide_legend(override_aes={'height': 1.4, 'width': 1.7}))" ] }, { "cell_type": "code", "execution_count": 28, "id": "cede8e61-1b42-41d3-b213-41c1e08b0c88", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 28, "metadata": {}, "output_type": "execute_result" } ], "source": [ "base + guides(fill=guide_legend(override_aes={'height': 0.7, 'width': 0.7}))" ] }, { "cell_type": "code", "execution_count": 31, "id": "c3821a46-2ee4-4d8c-9e5a-564a3787a511", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 31, "metadata": {}, "output_type": "execute_result" } ], "source": [ "(\n", " ggplot(boxplot_df, aes('class', 'hwy', fill='drv')) +\n", " geom_boxplot(color='gray20', size=2.0, outlier_shape=21,\n", " position=position_dodge(width=0.85)) +\n", " scale_fill_brewer(palette='Pastel1') +\n", " ggsize(620, 380) +\n", " labs(x='vehicle class', y='highway mpg', fill='drive') +\n", " ggtitle('size=2')\n", ")" ] }, { "cell_type": "code", "execution_count": null, "id": "4aa7bc11-2d25-4811-828c-016a62b267e9", "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.13.5" } }, "nbformat": 4, "nbformat_minor": 5 }