{ "cells": [ { "cell_type": "code", "execution_count": 1, "id": "regulated-engineering", "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", " \n", " \n", " " ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "import numpy as np\n", "import pandas as pd\n", "\n", "from lets_plot import *\n", "LetsPlot.setup_html()" ] }, { "cell_type": "code", "execution_count": 2, "id": "right-pathology", "metadata": {}, "outputs": [], "source": [ "w, h = 600, 400" ] }, { "cell_type": "code", "execution_count": 3, "id": "parliamentary-landscape", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# default theme\n", "p = (\n", " ggplot({'x': list(range(5)), 'y': list(range(5))}, aes('x', 'y')) +\n", " geom_point(aes(size='y', fill=\"y\"),tooltips=layer_tooltips().title(\"TITLE\").line('label|value')) +\n", " labs(title='The plot title', \n", " subtitle=\"The plot subtitle\",\n", " caption=\"The plot caption\",\n", " x='X axis label', \n", " y='Y axis label', \n", " fill='Fill legend title',\n", " size='Size legend title'\n", " )\n", ")\n", "p" ] }, { "cell_type": "code", "execution_count": 4, "id": "coupled-extension", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "# theme functions\n", "bunch = GGBunch()\n", "bunch.add_plot(p + ggtitle(\"theme_grey()\") + theme_grey(), 0, 0, w, h)\n", "bunch.add_plot(p + ggtitle(\"theme_none()\") + theme_none(), w, 0, w, h)\n", "bunch.show()" ] }, { "cell_type": "code", "execution_count": 5, "id": "applicable-seating", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "e_line = element_line(color='light_green', size=3)\n", "e_rect = element_rect(color='#2c7fb8', fill='#edf8b1', size=2)\n", "e_text = element_text(color='#f1a354', face='italic')\n", "\n", "# tooltips\n", "\n", "bunch = GGBunch()\n", "\n", "bunch.add_plot(p + theme(axis_tooltip=e_rect) +\n", " ggtitle(\"axis_tooltip = e_rect\", subtitle=\"axis tooltip -> fill + text/border color + stroke width\"),\n", " 0, 0)\n", "\n", "bunch.add_plot(p + theme(axis_tooltip=e_line) +\n", " ggtitle(\"axis_tooltip = e_line\", subtitle=\"axis tooltip -> text/border color + stroke width\"),\n", " w, 0)\n", "\n", "bunch.add_plot(p + theme(axis_tooltip=e_text) +\n", " ggtitle(\"axis_tooltip = e_text\", subtitle=\"axis tooltip -> text/border color\"),\n", " 0, h*1.1)\n", "\n", "bunch.add_plot(p + theme(axis_tooltip_text=e_text) +\n", " ggtitle(\"axis_tooltip_text = e_text\", subtitle=\"axis tooltip -> text color + font face\"),\n", " w, h*1.1)\n", "\n", "bunch.add_plot(p + theme(tooltip_text=e_text) +\n", " ggtitle(\"tooltip_text = e_text\", subtitle=\"all tooltip -> text color + font face\"),\n", " 0, h*2.2)\n", "\n", "bunch.add_plot(p + theme(tooltip=e_rect, tooltip_text=e_text) +\n", " ggtitle(\"+ tooltip=e_rect\", subtitle=\"-> tooltip rect\"),\n", " w, h*2.2)\n", "\n", "bunch.show()" ] }, { "cell_type": "code", "execution_count": 6, "id": "fossil-monday", "metadata": {}, "outputs": [], "source": [ "# Theme options \n", "theme_opts = theme(\n", " plot_title=element_text('red', face='bold_italic'),\n", " plot_subtitle=element_text('pink', face='normal'),\n", " plot_caption=element_text('gray', face='italic'),\n", " \n", " legend_title=element_text('#cc8f00'),\n", " legend_text=element_text('orange', face='italic'),\n", " \n", " axis_title_x=element_text('dark_green'),\n", " axis_text_x=element_text('#00b300'),\n", " axis_tooltip_text_x=element_text(color='light_green'),\n", "\n", " axis_title_y=element_text('dark_blue'),\n", " axis_text_y=element_text(color='#9e9eff'),\n", " axis_tooltip_text_y=element_text(color='light_blue'),\n", "\n", " tooltip=element_rect(color='#54278f', fill='#fcfbfd'),\n", " tooltip_text=element_text(color='#54278f', face='italic') \n", ")\n" ] }, { "cell_type": "code", "execution_count": 7, "id": "exempt-diagnosis", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "p2 = p + theme_opts\n", "p2" ] }, { "cell_type": "code", "execution_count": 8, "id": "norman-bunch", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "p2 + coord_flip() " ] }, { "cell_type": "code", "execution_count": null, "id": "packed-trainer", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": 9, "id": "express-highland", "metadata": {}, "outputs": [], "source": [ "data = dict(\n", " supp = ['OJ', 'OJ', 'OJ', 'VC', 'VC', 'VC'],\n", " dose = [0.5, 1.0, 2.0, 0.5, 1.0, 2.0],\n", " length = [13.23, 22.70, 26.06, 7.98, 16.77, 26.14],\n", " len_min = [11.83, 21.2, 24.50, 4.24, 15.26, 23.35],\n", " len_max = [15.63, 24.9, 27.11, 10.72, 19.28, 28.93]\n", ")\n" ] }, { "cell_type": "code", "execution_count": 10, "id": "varying-biology", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# bars\n", "bar_labels=labs(\n", " title=\"Tooth length per dose\",\n", " subtitle=\"geom_bar\", \n", " caption=\"The ToothGrowth dataset\",\n", " x = \"Dosage levels (mg)\",\n", " y =\"Tooth length\", \n", " color=\"Supplements\",\n", " fill=\"Supplements\"\n", ")\n", "bar_theme = theme(\n", " title=element_text('#3f007d'),\n", "\n", " plot_subtitle=element_text(face='bold_italic'),\n", " plot_caption=element_text(face='italic'),\n", "\n", " legend_title=element_text('#d95f0e'),\n", " legend_text=element_text('#fe9929', face='italic'),\n", "\n", " axis_title=element_text(face='bold'),\n", " axis_tooltip_text=element_text(color='light_pink'),\n", " \n", " axis_title_x=element_text('#a50f15'),\n", " axis_text_x=element_text('pink'),\n", "\n", " axis_title_y=element_text('dark_green'),\n", " axis_text_y=element_text('#a1d99b'),\n", "\n", " tooltip_text=element_text(color='#54278f', face='italic')\n", ")\n", "\n", "\n", "bar = (\n", " ggplot(data) + \n", " geom_bar(aes(x='dose', color='supp', y='length', fill='supp'), stat='identity', position='dodge') + \n", " bar_labels +\n", " bar_theme\n", ")\n", "bar" ] }, { "cell_type": "code", "execution_count": 11, "id": "frozen-spencer", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# coord flip\n", "bar + coord_flip()" ] }, { "cell_type": "code", "execution_count": 12, "id": "accredited-reducing", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# orientation \"y\"\n", "(\n", " ggplot(data) +\n", " geom_bar(aes(x='length', color='supp', y='dose', fill='supp'), \n", " stat='identity', position='dodge', orientation=\"y\") +\n", " bar_labels +\n", " bar_theme\n", ")" ] }, { "cell_type": "code", "execution_count": 13, "id": "established-watch", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Outlier Tooltips\n", "\n", "crossbar = (\n", " ggplot(data, aes(x='dose', color='supp')) +\n", " geom_crossbar(aes(ymin='len_min', ymax='len_max', middle='length'), fatten=5) +\n", " labs(title='geom_crossbar') +\n", " theme(\n", " plot_title=element_text('#990000', face='bold_italic'),\n", "\n", " legend_title=element_text('#cc8f00'),\n", " legend_text=element_text('orange', face='italic'),\n", " \n", " axis_title=element_text('#9b2d30'),\n", " axis_text=element_text('pink'),\n", " axis_tooltip=element_rect(color='pink', fill='#6c4675', size = 2.0),\n", " axis_tooltip_text=element_text(color='pink'),\n", "\n", " tooltip=element_rect(color='dark_blue', fill='#fff5ee'),\n", " tooltip_text=element_text(color='dark_blue', face='italic')\n", " )\n", ")\n", "\n", "crossbar " ] }, { "cell_type": "code", "execution_count": 14, "id": "satellite-tomato", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# facets\n", "\n", "crossbar + facet_grid('supp') + theme(strip_text=element_text(color='#990000', face='bold'))" ] }, { "cell_type": "code", "execution_count": 15, "id": "daily-coordination", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 15, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# GGbunch with different theme options\n", "\n", "bars_bunch = GGBunch()\n", "bars_bunch.add_plot(bar, 0, 0)\n", "bars_bunch.add_plot(crossbar, 0, h*1.1)\n", "bars_bunch" ] }, { "cell_type": "code", "execution_count": 16, "id": "quantitative-earth", "metadata": {}, "outputs": [], "source": [ "# Save to SVG\n", "#ggsave(bars_bunch, \"bars_bunch.svg\")\n", "\n", "\n", "#from IPython.display import SVG\n", "#display(SVG(filename=\"lets-plot-images/bars_bunch.svg\"))" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "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 }