{ "cells": [ { "cell_type": "markdown", "id": "a438d36c-99d6-4273-bce7-52f9830cd4b5", "metadata": {}, "source": [ "# Check alpha/opacity effects" ] }, { "cell_type": "code", "execution_count": 7, "id": "2f0fc161-5877-45e2-9832-272213c0cc77", "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", " \n", " \n", " " ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "from lets_plot import *\n", "import re\n", "\n", "LetsPlot.setup_html() " ] }, { "cell_type": "code", "execution_count": 8, "id": "5623144c-99c3-4bf3-8ef3-0ccb3f83a171", "metadata": {}, "outputs": [], "source": [ "def text_plot(color, alpha):\n", " rgba_alpha = None\n", " m = re.match(r'rgba\\(\\s*\\d+\\s*,\\s*\\d+\\s*,\\s*\\d+\\s*,\\s*([0-9.]+)\\s*\\)', color)\n", " if m:\n", " rgba_alpha = float(m.group(1))\n", " \n", " label = f\"{rgba_alpha}\\n{alpha}\"\n", " \n", " return (\n", " ggplot() +\n", " geom_text(\n", " x=0, y=0,\n", " label=label,\n", " size=24,\n", " color=color,\n", " alpha=alpha\n", " ) +\n", " theme_void() +\n", " theme(panel_grid_major=element_line(size=4)) +\n", " ggsize(300, 200)\n", " )" ] }, { "cell_type": "code", "execution_count": 9, "id": "f6e42fd5-f76e-4a7d-9aad-3332b0e91c3d", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "gggrid([\n", " text_plot(color='rgba(255,0,0,0)', alpha=None),\n", " text_plot(color='rgba(255,0,0,0)', alpha=0),\n", " text_plot(color='rgba(255,0,0,0)', alpha=0.3),\n", " text_plot(color='rgba(255,0,0,0)', alpha=1),\n", " #\n", " text_plot(color='rgba(255,0,0,0.3)', alpha=None),\n", " text_plot(color='rgba(255,0,0,0.3)', alpha=0),\n", " text_plot(color='rgba(255,0,0,0.3)', alpha=0.3),\n", " text_plot(color='rgba(255,0,0,0.3)', alpha=1),\n", " #\n", " text_plot(color='rgba(255,0,0,0.6)', alpha=None),\n", " text_plot(color='rgba(255,0,0,0.6)', alpha=0),\n", " text_plot(color='rgba(255,0,0,0.6)', alpha=0.3),\n", " text_plot(color='rgba(255,0,0,0.6)', alpha=1),\n", " #\n", " text_plot(color='rgba(255,0,0,1)', alpha=None),\n", " text_plot(color='rgba(255,0,0,1)', alpha=0),\n", " text_plot(color='rgba(255,0,0,1)', alpha=0.3),\n", " text_plot(color='rgba(255,0,0,1)', alpha=1)\n", "], ncol=4) + ggsize(600, 800) + ggtitle('If set, the alpha aesthetic overrides the alpha from the color')" ] }, { "cell_type": "code", "execution_count": 10, "id": "4c3df636-913c-4316-a4a3-1ae8ca3fd333", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# element_text\n", "(ggplot({'x': [1, 2, 3], 'y': [2, 4, 3]}, aes('x', 'y'))\n", " + geom_point(size=5)\n", " + geom_line()\n", " + labs(\n", " title='Plot Title',\n", " subtitle='Plot Subtitle',\n", " caption='Plot Caption',\n", " tag='A'\n", " )\n", " + theme(\n", " plot_title=element_text(color=\"rgba(255,0,0, 0.3)\", size=28),\n", " plot_subtitle=element_text(color=\"rgba(0,255,0, 0.3)\", size=24),\n", " plot_caption=element_text(color=\"rgba(0,255,255, 0.3)\", size=22),\n", " plot_tag=element_text(color=\"rgba(255,0,255, 0.3)\", size=30, face='bold')\n", " )\n", ")" ] }, { "cell_type": "code", "execution_count": 11, "id": "83b44f3e-eaef-4019-a09d-23669464066d", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# geom_tile\n", "(\n", " ggplot({'x': [1, 2], 'y': [1, 1]}) \n", " + geom_tile(\n", " aes(x='x', y='y'), \n", " color='rgba(0,0,0,0.2)', \n", " size=2, \n", " alpha = 0.3, \n", " linetype=4\n", " )\n", ")" ] }, { "cell_type": "code", "execution_count": 12, "id": "2b0c0af4-e63f-4771-b7af-484cde0b514d", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df = {\n", " 'x': [0, 1, 2, 3],\n", " 'y': [0, 0, 0, 0],\n", " 'label': ['0%', '25%', '50%', '100%'],\n", " 'color': ['#ff000000', '#ff000040', '#ff000080', '#ff0000ff'],\n", "}\n", "\n", "(\n", " ggplot(df, aes('x', 'y'))\n", " + geom_text(aes(label='label', color='color'), size=16)\n", " + scale_color_identity()\n", " + ggsize(500, 180)\n", ")\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.13.5" } }, "nbformat": 4, "nbformat_minor": 5 }