{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "### The `alpha` parameter in `geom_imshow()`" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "execution": { "iopub.execute_input": "2024-04-17T07:34:25.493451Z", "iopub.status.busy": "2024-04-17T07:34:25.493376Z", "iopub.status.idle": "2024-04-17T07:34:25.805846Z", "shell.execute_reply": "2024-04-17T07:34:25.805557Z" } }, "outputs": [ { "data": { "text/html": [ "\n", "
\n", " \n", " " ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "import numpy as np\n", "from lets_plot import *\n", "\n", "LetsPlot.setup_html()" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "execution": { "iopub.execute_input": "2024-04-17T07:34:25.818925Z", "iopub.status.busy": "2024-04-17T07:34:25.818793Z", "iopub.status.idle": "2024-04-17T07:34:26.178574Z", "shell.execute_reply": "2024-04-17T07:34:26.178182Z" } }, "outputs": [ { "data": { "text/plain": [ "(225, 225, 3)" ] }, "execution_count": 2, "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\n" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "execution": { "iopub.execute_input": "2024-04-17T07:34:26.179777Z", "iopub.status.busy": "2024-04-17T07:34:26.179698Z", "iopub.status.idle": "2024-04-17T07:34:26.210560Z", "shell.execute_reply": "2024-04-17T07:34:26.210335Z" } }, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 3, "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": 4, "metadata": { "execution": { "iopub.execute_input": "2024-04-17T07:34:26.211822Z", "iopub.status.busy": "2024-04-17T07:34:26.211721Z", "iopub.status.idle": "2024-04-17T07:34:26.238300Z", "shell.execute_reply": "2024-04-17T07:34:26.238018Z" } }, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "pair1 = GGBunch()\n", "pair1.add_plot(p + geom_imshow(im_arr) + ggtitle(\"no alpha (opaque image)\"), 0, 0)\n", "pair1.add_plot(p + geom_imshow(im_arr, alpha=0.7) + ggtitle(\"alpha=0.7\"), 500, 0)\n", "pair1" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### 2. `alpha` parameter and grayscale image" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "execution": { "iopub.execute_input": "2024-04-17T07:34:26.239777Z", "iopub.status.busy": "2024-04-17T07:34:26.239662Z", "iopub.status.idle": "2024-04-17T07:34:26.241529Z", "shell.execute_reply": "2024-04-17T07:34:26.241354Z" } }, "outputs": [ { "data": { "text/plain": [ "(225, 225)" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "im_arr_gs = im_arr[:,:,0]\n", "im_arr_gs.shape" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "execution": { "iopub.execute_input": "2024-04-17T07:34:26.242715Z", "iopub.status.busy": "2024-04-17T07:34:26.242576Z", "iopub.status.idle": "2024-04-17T07:34:26.256685Z", "shell.execute_reply": "2024-04-17T07:34:26.256500Z" } }, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "pair2 = GGBunch()\n", "pair2.add_plot(p + geom_imshow(im_arr_gs) + ggtitle(\"no alpha (opaque image)\"), 0, 0)\n", "pair2.add_plot(p + geom_imshow(im_arr_gs, alpha=0.7) + ggtitle(\"alpha=0.7\"), 500, 0)\n", "pair2" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### 3. `alpha` parameter and pseudo-color image" ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "execution": { "iopub.execute_input": "2024-04-17T07:34:26.257904Z", "iopub.status.busy": "2024-04-17T07:34:26.257782Z", "iopub.status.idle": "2024-04-17T07:34:26.272713Z", "shell.execute_reply": "2024-04-17T07:34:26.272510Z" } }, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "pair3 = GGBunch()\n", "pair3.add_plot(p + geom_imshow(im_arr_gs, \"magma\") + ggtitle(\"no alpha (opaque image)\"), 0, 0)\n", "pair3.add_plot(p + geom_imshow(im_arr_gs, \"magma\", alpha=0.7) + ggtitle(\"alpha=0.7\"), 500, 0)\n", "pair3" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### 4. `alpha` parameter and grayscale image with transparent pixels (`NaN` values)" ] }, { "cell_type": "code", "execution_count": 8, "metadata": { "execution": { "iopub.execute_input": "2024-04-17T07:34:26.273818Z", "iopub.status.busy": "2024-04-17T07:34:26.273670Z", "iopub.status.idle": "2024-04-17T07:34:26.275202Z", "shell.execute_reply": "2024-04-17T07:34:26.275026Z" } }, "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": 9, "metadata": { "execution": { "iopub.execute_input": "2024-04-17T07:34:26.276177Z", "iopub.status.busy": "2024-04-17T07:34:26.276104Z", "iopub.status.idle": "2024-04-17T07:34:26.290496Z", "shell.execute_reply": "2024-04-17T07:34:26.290321Z" } }, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "pair4 = GGBunch()\n", "pair4.add_plot(p + geom_imshow(im_arr_gs_nan, \"magma\", norm=False) + ggtitle(\"no alpha (opaque image)\"), 0, 0)\n", "pair4.add_plot(p + geom_imshow(im_arr_gs_nan, \"magma\", norm=False, alpha=0.7) + ggtitle(\"alpha=0.7\"), 500, 0)\n", "pair4" ] } ], "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 }