{ "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": {}, "outputs": [ { "data": { "text/html": [ "\n", "
\n", " \n", " " ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "import numpy as np\n", "from lets_plot import *\n", "LetsPlot.setup_html()" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "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": 3, "metadata": {}, "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] int64\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": 4, "metadata": {}, "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_classic() + theme(axis=\"blank\"))\n" ] }, { "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": 5, "metadata": {}, "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": 6, "metadata": {}, "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": 7, "metadata": {}, "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": 8, "metadata": {}, "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": 9, "metadata": {}, "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.7.13" } }, "nbformat": 4, "nbformat_minor": 4 }