{ "cells": [ { "cell_type": "code", "execution_count": 1, "id": "adult-article", "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "
\n", " \n", " " ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "from lets_plot import *\n", "\n", "LetsPlot.setup_html()" ] }, { "cell_type": "code", "execution_count": 2, "id": "adjacent-suicide", "metadata": {}, "outputs": [], "source": [ "data = {\n", " 'x': [1, 1, 2, 2, 2],\n", " 'y' : [1, 3, 2, 1, -3],\n", " 'grp': [\"a\", \"b\", \"a\", \"b\", \"c\"]\n", "}\n", "\n", "p = ggplot(data, aes('x', 'y', group = 'grp')) + \\\n", " geom_bar(aes(fill = 'grp'), stat = 'identity', color = 'black') + \\\n", " scale_fill_brewer(palette = \"Pastel1\")" ] }, { "cell_type": "code", "execution_count": 3, "id": "attended-jacksonville", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "w, h = 600, 400\n", "\n", "bunch = GGBunch()\n", "bunch.add_plot(p + geom_label(aes(label = 'y'), position = 'stack') + ggtitle(\"Default\"), 0, 0)\n", "bunch.add_plot(p + geom_label(aes(label = 'y'), position = position_stack(0.0)) + ggtitle(\"vjust = 0.0\"), w, 0)\n", "bunch.add_plot(p + geom_label(aes(label = 'y'), position = position_stack(0.3)) + ggtitle(\"vjust = 0.3\"), 0, h)\n", "bunch.add_plot(p + geom_label(aes(label = 'y'), position = position_stack(0.5)) + ggtitle(\"vjust = 0.5\"), w, h)\n", "bunch.add_plot(p + geom_label(aes(label = 'y'), position = position_stack(0.7)) + ggtitle(\"vjust = 0.7\"), 0, 2*h)\n", "bunch.add_plot(p + geom_label(aes(label = 'y'), position = position_stack(1.0)) + ggtitle(\"vjust = 1.0\"), w, 2*h)\n", "bunch.show()" ] }, { "cell_type": "code", "execution_count": 4, "id": "decent-enterprise", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# position = 'fill' \n", "\n", "p2 = ggplot(data, aes('x', 'y', group = 'grp')) + \\\n", " geom_bar(aes(fill = 'grp'), stat = 'identity', position = 'fill', color = 'black') + \\\n", " scale_fill_brewer(palette = \"Pastel1\")\n", "\n", "p2 + geom_text(aes(label = 'y'), position = position_fill(0.5))" ] }, { "cell_type": "code", "execution_count": 5, "id": "atomic-philadelphia", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import numpy as np\n", "\n", "series = {\n", " 'x': [1]*4 + [2] * 4 + [3] * 4 + [4] * 4,\n", " 'type': ['a', 'b', 'c', 'd'] * 4,\n", " 'value': np.random.poisson(100, size=16)\n", "}\n", "p3 = ggplot(series, aes('x', 'value', group = 'type')) + geom_area(aes(fill = 'type'), color = 'white') \n", "p3" ] }, { "cell_type": "code", "execution_count": 6, "id": "pacific-insurance", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# vjust=0.5 will center the labels inside the corresponding area\n", "p3 + geom_label(aes(label = 'type'), position = position_stack(vjust = 0.5)) " ] } ], "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.13" } }, "nbformat": 4, "nbformat_minor": 5 }