{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "## Using parameters `norm, vmain, vmax` and `cmap` in `geom_imshow()`\n", "\n", "You can use parameters `norm, vmain, vmax` and `cmap` when rendering grayscale images.\n", "\n", "A grayscale image is specified by Numpy's 2D array where each element's value \n", "represents the luminance of corresponding pixel in the image." ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "execution": { "iopub.execute_input": "2024-04-26T11:53:16.306556Z", "iopub.status.busy": "2024-04-26T11:53:16.306556Z", "iopub.status.idle": "2024-04-26T11:53:17.209606Z", "shell.execute_reply": "2024-04-26T11:53:17.209606Z" } }, "outputs": [], "source": [ "import numpy as np\n", "\n", "from lets_plot import *" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "execution": { "iopub.execute_input": "2024-04-26T11:53:17.209606Z", "iopub.status.busy": "2024-04-26T11:53:17.209606Z", "iopub.status.idle": "2024-04-26T11:53:17.225253Z", "shell.execute_reply": "2024-04-26T11:53:17.225253Z" } }, "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": "2024-04-26T11:53:17.241009Z", "iopub.status.busy": "2024-04-26T11:53:17.241009Z", "iopub.status.idle": "2024-04-26T11:53:17.257911Z", "shell.execute_reply": "2024-04-26T11:53:17.256812Z" } }, "outputs": [], "source": [ "# Generate 2D Numpy arrays. \n", "# First two arrays both contain float numbers but have different range of values.\n", "# The 3rd array contains int-s.\n", "arr_f0 = np.linspace(.3, .7, 30).reshape(1, -1)\n", "arr_f1 = np.linspace(30., 170., 30).reshape(1, -1)\n", "arr_i = np.linspace(30, 170, 30, dtype=int).reshape(1, -1)" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "execution": { "iopub.execute_input": "2024-04-26T11:53:17.257911Z", "iopub.status.busy": "2024-04-26T11:53:17.257911Z", "iopub.status.idle": "2024-04-26T11:53:17.272694Z", "shell.execute_reply": "2024-04-26T11:53:17.272694Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "arr_f0: shape (1, 30), data range [0.3-0.7] float64\n", "arr_f1: shape (1, 30), data range [30.0-170.0] float64\n", "arr_i : shape (1, 30), data range [30-170] int32\n" ] } ], "source": [ "print(\"arr_f0: shape {}, data range [{}-{}] {}\".format(arr_f0.shape, arr_f0.min(), arr_f0.max(), arr_f0.dtype))\n", "print(\"arr_f1: shape {}, data range [{}-{}] {}\".format(arr_f1.shape, arr_f1.min(), arr_f1.max(), arr_f1.dtype))\n", "print(\"arr_i : shape {}, data range [{}-{}] {}\".format(arr_i.shape, arr_i.min(), arr_i.max(), arr_i.dtype))\n" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "execution": { "iopub.execute_input": "2024-04-26T11:53:17.272694Z", "iopub.status.busy": "2024-04-26T11:53:17.272694Z", "iopub.status.idle": "2024-04-26T11:53:17.385130Z", "shell.execute_reply": "2024-04-26T11:53:17.383826Z" } }, "outputs": [], "source": [ "# Setup suitable plot options for the demo.\n", "p = (ggplot() + ggsize(450, 60) + geom_rect(xmin=-0.5, ymin=-0.5, xmax=29.5, ymax=0.5, color=\"black\", alpha=0) + \n", "coord_cartesian() + theme_void())" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Image normalization\n", "\n", "By default, `geom_imshow()` applies a linear scaling to transform data values to range [0-255]." ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "execution": { "iopub.execute_input": "2024-04-26T11:53:17.385130Z", "iopub.status.busy": "2024-04-26T11:53:17.385130Z", "iopub.status.idle": "2024-04-26T11:53:17.431085Z", "shell.execute_reply": "2024-04-26T11:53:17.431085Z" } }, "outputs": [ { "data": { "text/html": [ "
\n", " " ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "
\n", " " ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "
\n", " " ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "(p + geom_imshow(arr_f0)).show()\n", "(p + geom_imshow(arr_f1)).show()\n", "(p + geom_imshow(arr_i)).show()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## \n", "\n", "You can disable image normalization using the `norm` parameter." ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "execution": { "iopub.execute_input": "2024-04-26T11:53:17.431085Z", "iopub.status.busy": "2024-04-26T11:53:17.431085Z", "iopub.status.idle": "2024-04-26T11:53:17.463475Z", "shell.execute_reply": "2024-04-26T11:53:17.462250Z" } }, "outputs": [ { "data": { "text/html": [ "
\n", " " ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "
\n", " " ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "
\n", " " ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "(p + geom_imshow(arr_f0, norm=False)).show()\n", "(p + geom_imshow(arr_f1, norm=False)).show()\n", "(p + geom_imshow(arr_i, norm=False)).show()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Define normalization range using `vmin, vmax` parameters" ] }, { "cell_type": "code", "execution_count": 8, "metadata": { "execution": { "iopub.execute_input": "2024-04-26T11:53:17.464601Z", "iopub.status.busy": "2024-04-26T11:53:17.464601Z", "iopub.status.idle": "2024-04-26T11:53:17.494580Z", "shell.execute_reply": "2024-04-26T11:53:17.494580Z" } }, "outputs": [ { "data": { "text/html": [ "
\n", " " ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "
\n", " " ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "
\n", " " ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "# Set upper limit less than the data max value.\n", "(p + geom_imshow(arr_f0, vmax=.5)).show()\n", "(p + geom_imshow(arr_f1, vmax=100)).show()\n", "(p + geom_imshow(arr_i, vmax=100)).show()" ] }, { "cell_type": "code", "execution_count": 9, "metadata": { "execution": { "iopub.execute_input": "2024-04-26T11:53:17.494580Z", "iopub.status.busy": "2024-04-26T11:53:17.494580Z", "iopub.status.idle": "2024-04-26T11:53:17.526092Z", "shell.execute_reply": "2024-04-26T11:53:17.526092Z" } }, "outputs": [ { "data": { "text/html": [ "
\n", " " ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "
\n", " " ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "
\n", " " ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "# Set limits wider than the data entire range.\n", "(p + geom_imshow(arr_f0, vmin=.4, vmax=.6)).show()\n", "(p + geom_imshow(arr_f1, vmin=70, vmax=120)).show()\n", "(p + geom_imshow(arr_i, vmin=50, vmax=150)).show()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Colormaps" ] }, { "cell_type": "code", "execution_count": 10, "metadata": { "execution": { "iopub.execute_input": "2024-04-26T11:53:17.526092Z", "iopub.status.busy": "2024-04-26T11:53:17.526092Z", "iopub.status.idle": "2024-04-26T11:53:17.573695Z", "shell.execute_reply": "2024-04-26T11:53:17.572926Z" } }, "outputs": [ { "data": { "text/html": [ "
\n", " " ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "
\n", " " ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "
\n", " " ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "(p + geom_imshow(arr_f0, cmap=\"magma\")).show()\n", "(p + geom_imshow(arr_f1, cmap=\"viridis\")).show()\n", "(p + geom_imshow(arr_i, cmap=\"plasma\")).show()" ] } ], "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 }