{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "### The `alpha` parameter in `geom_imshow()`" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "execution": { "iopub.execute_input": "2025-11-05T13:48:30.327226Z", "iopub.status.busy": "2025-11-05T13:48:30.327133Z", "iopub.status.idle": "2025-11-05T13:48:30.330425Z", "shell.execute_reply": "2025-11-05T13:48:30.330239Z" } }, "outputs": [], "source": [ "import numpy as np\n", "\n", "from lets_plot import *" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "execution": { "iopub.execute_input": "2025-11-05T13:48:30.331464Z", "iopub.status.busy": "2025-11-05T13:48:30.331396Z", "iopub.status.idle": "2025-11-05T13:48:30.333085Z", "shell.execute_reply": "2025-11-05T13:48:30.332914Z" } }, "outputs": [ { "data": { "text/html": [ "\n", "
\n", " \n", " " ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "LetsPlot.setup_html()" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "execution": { "iopub.execute_input": "2025-11-05T13:48:30.346409Z", "iopub.status.busy": "2025-11-05T13:48:30.346310Z", "iopub.status.idle": "2025-11-05T13:48:30.660077Z", "shell.execute_reply": "2025-11-05T13:48:30.659610Z" } }, "outputs": [ { "data": { "text/plain": [ "(225, 225, 3)" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Load an image\n", "\n", "from PIL import Image\n", "import requests\n", "from io import BytesIO\n", "\n", "response = requests.get('https://github.com/JetBrains/lets-plot-docs/raw/master/source/examples/cookbook/images/fisher_boat.png')\n", "\n", "image = Image.open(BytesIO(response.content))\n", "im_arr = np.asarray(image)\n", "im_arr.shape" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "execution": { "iopub.execute_input": "2025-11-05T13:48:30.661330Z", "iopub.status.busy": "2025-11-05T13:48:30.661208Z", "iopub.status.idle": "2025-11-05T13:48:30.717611Z", "shell.execute_reply": "2025-11-05T13:48:30.717379Z" } }, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "p = ggplot() + geom_point(x=112, y=112, color=\"dark-blue\", size=100, shape=15) + ggsize(450, 450)\n", "p + ggtitle(\"The background blue square\") + ggsize(400, 300)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### 1. `alpha` parameter and color image" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "execution": { "iopub.execute_input": "2025-11-05T13:48:30.718607Z", "iopub.status.busy": "2025-11-05T13:48:30.718528Z", "iopub.status.idle": "2025-11-05T13:48:30.747966Z", "shell.execute_reply": "2025-11-05T13:48:30.747773Z" } }, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "gggrid([\n", " p + geom_imshow(im_arr) + ggtitle(\"no alpha (opaque image)\"),\n", " p + geom_imshow(im_arr, alpha=0.7) + ggtitle(\"alpha=0.7\")\n", "]) + ggsize(1000, 500)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### 2. `alpha` parameter and grayscale image" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "execution": { "iopub.execute_input": "2025-11-05T13:48:30.749015Z", "iopub.status.busy": "2025-11-05T13:48:30.748903Z", "iopub.status.idle": "2025-11-05T13:48:30.750890Z", "shell.execute_reply": "2025-11-05T13:48:30.750691Z" } }, "outputs": [ { "data": { "text/plain": [ "(225, 225)" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "im_arr_gs = im_arr[:,:,0]\n", "im_arr_gs.shape" ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "execution": { "iopub.execute_input": "2025-11-05T13:48:30.752008Z", "iopub.status.busy": "2025-11-05T13:48:30.751937Z", "iopub.status.idle": "2025-11-05T13:48:30.770936Z", "shell.execute_reply": "2025-11-05T13:48:30.770736Z" } }, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "gggrid([\n", " p + geom_imshow(im_arr_gs) + ggtitle(\"no alpha (opaque image)\"),\n", " p + geom_imshow(im_arr_gs, alpha=0.7) + ggtitle(\"alpha=0.7\"),\n", "]) + ggsize(1000, 500)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### 3. `alpha` parameter and pseudo-color image" ] }, { "cell_type": "code", "execution_count": 8, "metadata": { "execution": { "iopub.execute_input": "2025-11-05T13:48:30.772172Z", "iopub.status.busy": "2025-11-05T13:48:30.772098Z", "iopub.status.idle": "2025-11-05T13:48:30.792966Z", "shell.execute_reply": "2025-11-05T13:48:30.792694Z" } }, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "gggrid([\n", " p + geom_imshow(im_arr_gs, \"magma\") + ggtitle(\"no alpha (opaque image)\"),\n", " p + geom_imshow(im_arr_gs, \"magma\", alpha=0.7) + ggtitle(\"alpha=0.7\"),\n", "]) + ggsize(1000, 500)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### 4. `alpha` parameter and grayscale image with transparent pixels (`NaN` values)" ] }, { "cell_type": "code", "execution_count": 9, "metadata": { "execution": { "iopub.execute_input": "2025-11-05T13:48:30.793959Z", "iopub.status.busy": "2025-11-05T13:48:30.793884Z", "iopub.status.idle": "2025-11-05T13:48:30.795388Z", "shell.execute_reply": "2025-11-05T13:48:30.795214Z" } }, "outputs": [], "source": [ "im_arr_gs_nan=im_arr_gs.copy()\n", "im_arr_gs_nan=im_arr_gs_nan.astype(np.float64)\n", "im_arr_gs_nan[im_arr_gs_nan < 80] = np.nan" ] }, { "cell_type": "code", "execution_count": 10, "metadata": { "execution": { "iopub.execute_input": "2025-11-05T13:48:30.795997Z", "iopub.status.busy": "2025-11-05T13:48:30.795923Z", "iopub.status.idle": "2025-11-05T13:48:30.812045Z", "shell.execute_reply": "2025-11-05T13:48:30.811857Z" } }, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "gggrid([\n", " p + geom_imshow(im_arr_gs_nan, \"magma\", norm=False) + ggtitle(\"no alpha (opaque image)\"),\n", " p + geom_imshow(im_arr_gs_nan, \"magma\", norm=False, alpha=0.7) + ggtitle(\"alpha=0.7\"),\n", "]) + ggsize(1000, 500)" ] } ], "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.10.13" } }, "nbformat": 4, "nbformat_minor": 4 }