{ "cells": [ { "cell_type": "markdown", "id": "d0c2599e", "metadata": {}, "source": [ "# Lets-Plot Themes" ] }, { "cell_type": "code", "execution_count": 1, "id": "076434ca", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "The geodata is provided by © OpenStreetMap contributors and is made available here under the Open Database License (ODbL).\n" ] }, { "data": { "text/html": [ "\n", "
\n", " \n", " " ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "import numpy as np\n", "import pandas as pd\n", "from PIL import Image\n", "\n", "from lets_plot import *\n", "from lets_plot.geo_data import *\n", "from lets_plot.bistro.corr import *\n", "LetsPlot.setup_html()" ] }, { "cell_type": "markdown", "id": "ac242d0f", "metadata": {}, "source": [ "### Preparation" ] }, { "cell_type": "code", "execution_count": 2, "id": "94b6d3ad", "metadata": {}, "outputs": [], "source": [ "w, h = 400, 300" ] }, { "cell_type": "code", "execution_count": 3, "id": "74457e08", "metadata": {}, "outputs": [], "source": [ "df = pd.read_csv(\"https://raw.githubusercontent.com/JetBrains/lets-plot-docs/master/data/mpg.csv\")\n", "gdf = geocode_countries().get_boundaries(3)\n", "img = Image.open(\"images/fisher_boat.png\")" ] }, { "cell_type": "code", "execution_count": 4, "id": "fcb086e9", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "p_plot = ggplot(df) + geom_point(aes('cty', 'hwy'))\n", "p_plot" ] }, { "cell_type": "code", "execution_count": 5, "id": "cff9d5a3", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "b_plot = ggplot(df) + geom_bar(aes('fl', color='fl', fill='fl'))\n", "b_plot" ] }, { "cell_type": "code", "execution_count": 6, "id": "3e6aacd3", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "f_plot = b_plot + facet_grid(x='year')\n", "f_plot" ] }, { "cell_type": "code", "execution_count": 7, "id": "03877c83", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "c_plot = corr_plot(df, flip=False).points().build()\n", "c_plot" ] }, { "cell_type": "code", "execution_count": 8, "id": "d2791f30", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "g_plot = ggplot() + geom_map(data=gdf, color='white', fill='black')\n", "g_plot" ] }, { "cell_type": "code", "execution_count": 9, "id": "f938f69f", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "i_plot = ggplot() + geom_image(image_data=np.asarray(img))\n", "i_plot" ] }, { "cell_type": "markdown", "id": "f43e57a3", "metadata": {}, "source": [ "### Elements" ] }, { "cell_type": "code", "execution_count": 10, "id": "89f612ad", "metadata": {}, "outputs": [], "source": [ "e_blank = element_blank()\n", "e_line = element_line(color='#de2d26', size=4)\n", "e_line2 = element_line(color='#fcae91', size=1)\n", "e_rect = element_rect(color='#2c7fb8', fill='#edf8b1', size=2)\n", "e_text = element_text(color='#31a354')" ] }, { "cell_type": "markdown", "id": "50e95997", "metadata": {}, "source": [ "### Themes" ] }, { "cell_type": "code", "execution_count": 11, "id": "f2c1c798", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "bunch = GGBunch()\n", "bunch.add_plot(p_plot + ggtitle(\"default (theme_minimal2())\"), \\\n", " 0, 0, w, h)\n", "bunch.add_plot(p_plot + ggtitle(\"theme_light()\") + theme_light(), \\\n", " w, 0, w, h)\n", "bunch.add_plot(p_plot + ggtitle(\"theme_classic()\") + theme_classic(), \\\n", " 0, h, w, h)\n", "bunch.add_plot(p_plot + ggtitle(\"theme_grey()\") + theme_grey(), \\\n", " w, h, w, h)\n", "bunch.add_plot(p_plot + ggtitle(\"theme_minimal()\") + theme_minimal(), \\\n", " 0, 2 * h, w, h)\n", "bunch.add_plot(p_plot + ggtitle(\"theme_none()\") + theme_none(), \\\n", " w, 2 * h, w, h)\n", "bunch.show()" ] }, { "cell_type": "markdown", "id": "753847a6", "metadata": {}, "source": [ "### Theme Parameters" ] }, { "cell_type": "code", "execution_count": 12, "id": "1519dfb3", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "bunch = GGBunch()\n", "for i, (param, element, element_name) in enumerate([('axis_title_x', e_text, 'e_text'), \\\n", " ('axis_text_x', e_text, 'e_text'), \\\n", " ('axis_ticks_x', e_line, 'e_line'), \\\n", " ('axis_line_x', e_line, 'e_line')]):\n", " bunch.add_plot(p_plot + ggtitle(\"{0}=element_blank\".format(param)) + theme(**{param: element_blank()}), \\\n", " 0, i * h, w, h)\n", " bunch.add_plot(p_plot + ggtitle(\"{0}={1}\".format(param, element_name)) + theme(**{param: element}), \\\n", " w, i * h, w, h)\n", "bunch.show()" ] }, { "cell_type": "code", "execution_count": 13, "id": "bbad9ac9", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "b_plot + \\\n", " ggtitle(\"Place legend\") + \\\n", " theme(legend_position=[1, 1], legend_justification=[1, 1], legend_direction='vertical')" ] }, { "cell_type": "code", "execution_count": 14, "id": "c748d3fa", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "bunch = GGBunch()\n", "bunch.add_plot(p_plot + ggtitle(\"axis_tooltip_x=e_blank\") + theme(axis_tooltip_x=e_blank), \\\n", " 0, 0, w, h)\n", "bunch.add_plot(p_plot + ggtitle(\"axis_tooltip_x=e_rect\") + theme(axis_tooltip_x=e_rect), \\\n", " w, 0, w, h)\n", "bunch.show()" ] }, { "cell_type": "code", "execution_count": 15, "id": "1ab0c989", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "bunch = GGBunch()\n", "bunch.add_plot(p_plot + ggtitle(\"default\"), \\\n", " 0, 0, w, h)\n", "bunch.add_plot(p_plot + ggtitle(\"line=e_line\") + theme(line=e_line), \\\n", " w, 0, w, h)\n", "bunch.show()" ] }, { "cell_type": "code", "execution_count": 16, "id": "f0dd2a43", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "bunch = GGBunch()\n", "bunch.add_plot(b_plot + ggtitle(\"theme_none()\") + theme_none(), \\\n", " 0, 0, w, h)\n", "bunch.add_plot(b_plot + ggtitle(\"theme_none + rect\") + theme_none() + theme(rect=e_rect), \\\n", " w, 0, w, h)\n", "bunch.show()" ] }, { "cell_type": "code", "execution_count": 17, "id": "9fe9fb9a", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "bunch = GGBunch()\n", "bunch.add_plot(f_plot + ggtitle(\"default\"), \\\n", " 0, 0, w, h)\n", "bunch.add_plot(f_plot + ggtitle(\"text=e_text\") + theme(text=e_text), \\\n", " w, 0, w, h)\n", "bunch.add_plot(f_plot + ggtitle(\"legend_text=e_text\") + theme(legend_text=e_text), \\\n", " 0, h, w, h)\n", "bunch.add_plot(f_plot + ggtitle(\"strip_text=e_text\") + theme(strip_text=e_text), \\\n", " w, h, w, h)\n", "bunch.show()" ] }, { "cell_type": "code", "execution_count": 18, "id": "060979fa", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "bunch = GGBunch()\n", "bunch.add_plot(b_plot + ggtitle(\"default\"), \\\n", " 0, 0, w, h)\n", "bunch.add_plot(b_plot + ggtitle(\"title=e_text\") + theme(title=e_text), \\\n", " w, 0, w, h)\n", "bunch.add_plot(b_plot + ggtitle(\"plot_title=e_text\") + theme(plot_title=e_text), \\\n", " 0, h, w, h)\n", "bunch.add_plot(b_plot + ggtitle(\"legend_title=e_text\") + theme(legend_title=e_text), \\\n", " w, h, w, h)\n", "bunch.show()" ] }, { "cell_type": "code", "execution_count": 19, "id": "4e2609ff", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "bunch = GGBunch()\n", "bunch.add_plot(p_plot + ggtitle(\"default\"), \\\n", " 0, 0, w, h)\n", "bunch.add_plot(p_plot + ggtitle(\"axis=e_blank\") + theme(axis=e_blank), \\\n", " w, 0, w, h)\n", "bunch.add_plot(p_plot + ggtitle(\"axis=e_line\") + theme(axis=e_line), \\\n", " 0, h, w, h)\n", "bunch.add_plot(p_plot + ggtitle(\"axis=e_text\") + theme(axis=e_text), \\\n", " w, h, w, h)\n", "bunch.show()" ] }, { "cell_type": "code", "execution_count": 20, "id": "a06e73d3", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "bunch = GGBunch()\n", "bunch.add_plot(p_plot + ggtitle(\"default\"), \\\n", " 0, 0, w, h)\n", "bunch.add_plot(p_plot + ggtitle(\"panel_background=e_rect\") + theme(panel_background=e_rect), \\\n", " w, 0, w, h)\n", "bunch.show()" ] }, { "cell_type": "code", "execution_count": 21, "id": "3ecf1753", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "bunch = GGBunch()\n", "bunch.add_plot(b_plot + ggtitle(\"panel_grid=e_blank\") + theme(panel_grid=e_blank), \\\n", " 0, 0, w, h)\n", "bunch.add_plot(p_plot + ggtitle(\"panel_grid=e_line\") + theme(panel_grid=e_line), \\\n", " w, 0, w, h)\n", "bunch.add_plot(p_plot + ggtitle(\"panel_grid_major=e_line, panel_grid_minor=element_line\") + \\\n", " theme(panel_grid_major=e_line, panel_grid_minor=e_line2), \\\n", " 0, h, w, h)\n", "bunch.add_plot(b_plot + ggtitle(\"panel_grid_major_x=e_blank, panel_grid_minor_x=e_blank\") + \\\n", " theme(panel_grid_major_x=e_blank, panel_grid_major_y=e_line, \\\n", " panel_grid_minor_x=e_blank, panel_grid_minor_y=e_line2), \\\n", " w, h, w, h)\n", "bunch.show()" ] }, { "cell_type": "code", "execution_count": 22, "id": "5990a939", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "bunch = GGBunch()\n", "bunch.add_plot(f_plot + ggtitle(\"default\") + theme_grey(), \\\n", " 0, 0, w, h)\n", "bunch.add_plot(f_plot + ggtitle(\"strip_background=e_rect\") + theme_grey() + theme(strip_background=e_rect), \\\n", " w, 0, w, h)\n", "bunch.add_plot(f_plot + ggtitle(\"strip_background=e_blank\") + theme_grey() + theme(strip_background=e_blank), \\\n", " 0, h, w, h)\n", "bunch.add_plot(f_plot + ggtitle(\"strip_text=e_blank\") + theme_grey() + theme(strip_text=e_blank), \\\n", " w, h, w, h)\n", "bunch.show()" ] }, { "cell_type": "markdown", "id": "4a8da0e8", "metadata": {}, "source": [ "### Extra" ] }, { "cell_type": "code", "execution_count": 23, "id": "7febdf46", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "bunch = GGBunch()\n", "bunch.add_plot(b_plot + coord_flip() + ggtitle(\"default\"), \\\n", " 0, 0, w, h)\n", "bunch.add_plot(b_plot + coord_flip() + ggtitle(\"plot 1\") + theme(line=e_line, text=e_text), \\\n", " w, 0, w, h)\n", "bunch.add_plot(b_plot + coord_flip() + ggtitle(\"plot 2\") + theme(axis=e_line, legend_title=e_text), \\\n", " 0, h, w, h)\n", "bunch.add_plot(b_plot + coord_flip() + ggtitle(\"plot 3\") + theme(plot_title=e_text, \\\n", " panel_background=e_rect, \\\n", " panel_grid_major_x=e_blank, \\\n", " panel_grid_major_y=e_line, \\\n", " panel_grid_minor_x=e_blank, \\\n", " panel_grid_minor_y=e_line2), \\\n", " w, h, w, h)\n", "bunch.add_plot(f_plot + coord_flip() + ggtitle(\"plot 4\") + \\\n", " theme(strip_background=e_rect, strip_text=e_text), \\\n", " 0, 2 * h, w, h)\n", "bunch.add_plot(b_plot + coord_flip() + ggtitle(\"plot 5\") + theme(axis_title_x=e_text, \\\n", " axis_text_x=e_text, \\\n", " axis_ticks_x=e_line, \\\n", " axis_line_x=e_line2, \\\n", " axis_tooltip_x=e_blank, \\\n", " legend_position='bottom', \\\n", " legend_direction='horizontal'), \\\n", " w, 2 * h, w, h)\n", "bunch.show()" ] }, { "cell_type": "code", "execution_count": 24, "id": "a4cc20c0", "metadata": { "scrolled": false }, "outputs": [ { "data": { "text/html": [ "
\n", " " ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "bunch = GGBunch()\n", "bunch.add_plot(c_plot + ggtitle(\"default\"), \\\n", " 0, 0, w, h)\n", "bunch.add_plot(c_plot + ggtitle(\"with theme\") + theme(text=e_text, \\\n", " axis=e_line, \\\n", " panel_background=e_rect, \\\n", " axis_title_x=e_text, \\\n", " axis_text_x=e_text, \\\n", " axis_ticks_x=e_line, \\\n", " axis_line_x=e_line, \\\n", " axis_tooltip_x=e_blank), \\\n", " w, 0, w, h)\n", "bunch.show()" ] }, { "cell_type": "code", "execution_count": 25, "id": "34caf987", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "bunch = GGBunch()\n", "bunch.add_plot(g_plot + ggtitle(\"default\"), \\\n", " 0, 0, w, h)\n", "bunch.add_plot(g_plot + coord_cartesian() + ggtitle(\"coord_cartesian()\"), \\\n", " w, 0, w, h)\n", "bunch.add_plot(g_plot + coord_fixed() + ggtitle(\"coord_fixed()\"), \\\n", " 0, h, w, h)\n", "bunch.add_plot(g_plot + ggtitle(\"theme_classic()\") + theme_classic(), \\\n", " w, h, w, h)\n", "bunch.show()" ] }, { "cell_type": "code", "execution_count": 26, "id": "41d6282c", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "bunch = GGBunch()\n", "bunch.add_plot(i_plot + ggtitle(\"default\"), \\\n", " 0, 0, w, h)\n", "bunch.add_plot(i_plot + ggtitle(\"classic theme\") + theme_classic(), \\\n", " w, 0, w, h)\n", "bunch.show()" ] }, { "cell_type": "code", "execution_count": 27, "id": "523e0cfb", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "pt_plot = ggplot(df, aes('cty', 'hwy')) + \\\n", " geom_point(aes(color='fl'), tooltips=layer_tooltips().anchor('top_center').min_width(50))\n", "\n", "bunch = GGBunch()\n", "bunch.add_plot(pt_plot + ggtitle(\"default\"), \\\n", " 0, 0, w, h)\n", "bunch.add_plot(pt_plot + ggtitle(\"gray background\") + \\\n", " theme(panel_background=element_rect(color='black', fill='gray')), \\\n", " w, 0, w, h)\n", "bunch.show()" ] } ], "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.7.10" } }, "nbformat": 4, "nbformat_minor": 5 }