{ "cells": [ { "cell_type": "code", "execution_count": 1, "id": "f2a9d691", "metadata": {}, "outputs": [], "source": [ "import pandas as pd\n", "from lets_plot import *\n", "\n", "def dump_plot(plot, display=None):\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", " if display:\n", " print(plot_json)\n", "\n", " return plot\n", " " ] }, { "cell_type": "code", "execution_count": 2, "id": "632b5308", "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", " \n", " \n", " " ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "LetsPlot.setup_html()" ] }, { "cell_type": "code", "execution_count": 3, "id": "57d71b80", "metadata": {}, "outputs": [], "source": [ "plot_data = pd.DataFrame.from_records([\n", " (\"pet\", \"cat\", 5, \"carnivore\"),\n", " (\"pet\", \"dog\", 10, \"carnivore\"),\n", " (\"pet\", \"rabbit\", 2, \"herbivore\"),\n", " (\"pet\", \"hamster\", 1, \"herbivore\"),\n", "\n", " (\"farm_animal\", \"cow\", 500, \"herbivore\"),\n", " (\"farm_animal\", \"pig\", 100, \"carnivore\"),\n", " (\"farm_animal\", \"horse\", 700, \"herbivore\"),\n", "])\n", "plot_data.columns = (\"animal_type\", \"animal\", \"weight\", \"diet\")\n" ] }, { "cell_type": "code", "execution_count": 4, "id": "c2b38e6c", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "plot = (\n", " ggplot(plot_data, aes(x=\"animal\", y=\"weight\"))\n", " + geom_bar(stat=\"identity\")\n", " + theme_bw()\n", " + theme(\n", " panel_grid_minor=element_blank()\n", " )\n", ")\n", "dump_plot(plot)" ] }, { "cell_type": "markdown", "id": "e6b3addb", "metadata": {}, "source": [ "#### Two facets, grid, free scales" ] }, { "cell_type": "code", "execution_count": 5, "id": "d49c97ea", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "plot + facet_grid(x = \"animal_type\", scales=\"free\")" ] }, { "cell_type": "markdown", "id": "009732d4", "metadata": {}, "source": [ "#### Two facets, wrap, free scales" ] }, { "cell_type": "code", "execution_count": 6, "id": "cc578600", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "plot + facet_wrap(facets=\"animal_type\", ncol=2, scales=\"free\")" ] }, { "cell_type": "markdown", "id": "be5fb006", "metadata": {}, "source": [ "#### Four facets, grid, free x" ] }, { "cell_type": "code", "execution_count": 7, "id": "dfcffe78", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "plot + facet_grid(x=\"animal_type\", y=\"diet\", scales=\"free_x\")" ] }, { "cell_type": "markdown", "id": "ac86f5cb", "metadata": {}, "source": [ "#### Four facets, wrap, free x" ] }, { "cell_type": "code", "execution_count": 8, "id": "c44f0d76", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "plot + facet_wrap(facets=[\"animal_type\", \"diet\"], ncol=2, scales=\"free_x\")" ] }, { "cell_type": "markdown", "id": "763efd82", "metadata": {}, "source": [ "#### Four facets, grid, free y" ] }, { "cell_type": "code", "execution_count": 9, "id": "a0fda8bf", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "plot + facet_grid(x=\"animal_type\", y=\"diet\", scales=\"free_y\")" ] }, { "cell_type": "markdown", "id": "447c81a8", "metadata": {}, "source": [ "#### Four facets, wrap, free y" ] }, { "cell_type": "code", "execution_count": 10, "id": "279c2c26", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "plot + facet_wrap(facets=[\"animal_type\", \"diet\"], ncol=2, scales=\"free_y\")" ] }, { "cell_type": "markdown", "id": "ed178b59", "metadata": {}, "source": [ "#### Four facets, grid, free scales" ] }, { "cell_type": "code", "execution_count": 11, "id": "54415628", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "plot + facet_grid(x=\"animal_type\", y=\"diet\", scales=\"free\")" ] }, { "cell_type": "markdown", "id": "f01f9d1a", "metadata": {}, "source": [ "#### Four facets, wrap, free scales" ] }, { "cell_type": "code", "execution_count": 12, "id": "375864f5", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "plot + facet_wrap(facets=[\"animal_type\", \"diet\"], ncol=2, scales=\"free\")" ] }, { "cell_type": "markdown", "id": "648003c3", "metadata": {}, "source": [ "## Y-orientation" ] }, { "cell_type": "code", "execution_count": 13, "id": "3dc987b2", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "plot_y = (\n", " ggplot(plot_data, aes(x=\"weight\", y=\"animal\"))\n", " + geom_bar(stat=\"identity\", orientation=\"y\")\n", " + theme_bw()\n", " + theme(\n", " panel_grid_minor=element_blank()\n", " )\n", ")\n", "plot_y" ] }, { "cell_type": "markdown", "id": "488b572a", "metadata": {}, "source": [ "#### Two facets, grid, free scales" ] }, { "cell_type": "code", "execution_count": 14, "id": "0dd614c1", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ "plot_y + facet_grid(y = \"animal_type\", scales=\"free_y\")" ] }, { "cell_type": "markdown", "id": "cc13ed2f", "metadata": {}, "source": [ "#### Two facets, wrap, free scales" ] }, { "cell_type": "code", "execution_count": 15, "id": "310a5a2b", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 15, "metadata": {}, "output_type": "execute_result" } ], "source": [ "plot_y + facet_wrap(facets=\"animal_type\", ncol=2, scales=\"free\")" ] }, { "cell_type": "markdown", "id": "f84e759c", "metadata": {}, "source": [ "#### Four facets, grid, free x" ] }, { "cell_type": "code", "execution_count": 16, "id": "15a97b25", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 16, "metadata": {}, "output_type": "execute_result" } ], "source": [ "plot_y + facet_grid(y=\"animal_type\", x=\"diet\", scales=\"free_x\")" ] }, { "cell_type": "markdown", "id": "50f107a8", "metadata": {}, "source": [ "#### Four facets, wrap, free x" ] }, { "cell_type": "code", "execution_count": 17, "id": "0d1138c1", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 17, "metadata": {}, "output_type": "execute_result" } ], "source": [ "plot_y + facet_wrap(facets=[\"animal_type\", \"diet\"], ncol=2, scales=\"free_x\")" ] }, { "cell_type": "markdown", "id": "1b178076", "metadata": {}, "source": [ "#### Four facets, grid, free y" ] }, { "cell_type": "code", "execution_count": 18, "id": "33355fcf", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 18, "metadata": {}, "output_type": "execute_result" } ], "source": [ "plot_y + facet_grid(y=\"animal_type\", x=\"diet\", scales=\"free_y\")" ] }, { "cell_type": "markdown", "id": "a6f9c778", "metadata": {}, "source": [ "#### Four facets, wrap, free y" ] }, { "cell_type": "code", "execution_count": 19, "id": "20018426", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 19, "metadata": {}, "output_type": "execute_result" } ], "source": [ "plot_y + facet_wrap(facets=[\"animal_type\", \"diet\"], ncol=2, scales=\"free_y\")" ] }, { "cell_type": "markdown", "id": "b7411929", "metadata": {}, "source": [ "#### Four facets, grid, free scales" ] }, { "cell_type": "code", "execution_count": 20, "id": "8d51722a", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 20, "metadata": {}, "output_type": "execute_result" } ], "source": [ "plot_y + facet_grid(y=\"animal_type\", x=\"diet\", scales=\"free\") + theme(panel_border=element_rect())" ] }, { "cell_type": "markdown", "id": "1366bce4", "metadata": {}, "source": [ "#### Four facets, wrap, free scales" ] }, { "cell_type": "code", "execution_count": 21, "id": "e22440f7", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 21, "metadata": {}, "output_type": "execute_result" } ], "source": [ "\n", "dump_plot(plot_y + facet_wrap(facets=[\"animal_type\", \"diet\"], ncol=2, scales=\"free\") + theme(panel_border=element_rect()))" ] }, { "cell_type": "code", "execution_count": null, "id": "7ac474df-973f-4312-aaac-a9420eb03cec", "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.15" } }, "nbformat": 4, "nbformat_minor": 5 }