{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "## geom_imshow()\n", "\n", "`geom_imshow()` displays an image specified by 2D or 3D Numpy array.\n", "\n", "Whether the image is grayscale or color depends on the shape of the image array:\n", "- (M, N) - grey-scale image\n", "- (M, N, 3) - color RGB image\n", "- (M, N, 4) - color RGB image with alpha channel\n", "\n", "The array's `dtype` can be int, uint or float.\n", "\n", "By default, all values in the image array will be transformed to the range [0-255] using a linear scaler." ] }, { "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", "\n", "LetsPlot.setup_html()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Grayscale image\n" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "A2x3 = np.array([\n", " [50, 150 ,200],\n", " [200,100,50]\n", " ])\n", "\n", "ggplot() + geom_imshow(A2x3)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Grayscale image without normalization" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ggplot() + geom_imshow(A2x3, norm=False)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Grayscale image in \"Viridis\" colors" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ggplot() + geom_imshow(A2x3, cmap=\"viridis\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### RGB image\n", "\n", "M x N x 3 array" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "A2x3x3 = np.array([\n", " [[255, 0, 0], [0, 255, 0], [0, 0, 255]],\n", " [[0, 255, 0], [0, 0, 255], [255, 0, 0]]\n", " ])\n", "ggplot() + geom_imshow(A2x3x3)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### RGB image with alpha channel\n", "\n", "M x N x 4 array" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "A2x3x4 = np.array([\n", " [[1, 0, 0, 1], [0, 1, 0, 1], [0, 0, 1, 1]],\n", " [[0, 1, 0, 0.3], [0, 0, 1, 0.3], [1, 0, 0, 0.3]]\n", " ])\n", "\n", "ggplot() + geom_imshow(A2x3x4)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Just a random image" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "image = np.random.choice([0.0, 1.0], [10, 100, 3])\n", "ggplot() + geom_imshow(image) + coord_cartesian()\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.7.13" } }, "nbformat": 4, "nbformat_minor": 1 }