{ "cells": [ { "cell_type": "code", "execution_count": 1, "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, "metadata": {}, "outputs": [], "source": [ "df = {\n", " 'x': [1],\n", " 'y': [1]\n", "}" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# No label format\n", "ggplot(df, aes('x','y')) + geom_text(aes(label='x'), size = 10)" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Use number format\n", "ggplot(df, aes('x','y')) + geom_text(aes(label='x'), size = 10, label_format='.2f')" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Use string pattern \n", "ggplot(df, aes('x','y')) + geom_text(aes(label='x'), size = 10, label_format='{.2f} Kg')" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Add tooltip\n", "ggplot(df, aes('x','y')) \\\n", " + geom_text(aes(label='x'), size = 10, label_format='{.2f} Kg', \\\n", " tooltips=layer_tooltips().line('position|(x=$x, y=$y)'))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Apply Alpha" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "##### `geom_text()`" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [], "source": [ "def text_plot(color, alpha):\n", " return ggplot(df, aes('x','y')) + geom_text(x=0, y=0, label='Lorem ipsum', size=14, color=color, alpha=alpha)" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "gggrid([\n", " text_plot(color='red', alpha=1),\n", " text_plot(color='red', alpha=0)\n", "]) + ggsize(800, 200)" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# defined aes alpha has priority over color's alpha\n", "\n", "gggrid([\n", " text_plot(color='rgba(255,0,0,0.5)', alpha=None),\n", " text_plot(color='rgba(255,0,0,0.5)', alpha=1)\n", "]) + ggsize(800, 200)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "##### `geom_label()`" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [], "source": [ "def label_plot(fill, color, alpha, alpha_stroke=None):\n", " return ggplot(df, aes('x','y')) + \\\n", " geom_label(label='Lorem ipsum', size=14, label_size=4, \n", " fill=fill, color=color, \n", " alpha=alpha, alpha_stroke=alpha_stroke)" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# alpha is applied to fill, not to color\n", "\n", "gggrid([\n", " label_plot(fill='light_blue', color='red', alpha=1),\n", " label_plot(fill='light_blue', color='red', alpha=0)\n", "]) + ggsize(800, 200)" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Activate the effect of alpha on color: \n", "# rgba or alpha_stroke=True\n", "\n", "gggrid([\n", " label_plot(fill='light_blue', color='rgba(255,0,0,0.5)', alpha=None),\n", " label_plot(fill='light_blue', color='red', alpha=0.1, alpha_stroke=True)\n", "]) + ggsize(800, 200)\n" ] } ], "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": 4 }