{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# `geom_imshow()` and `geom_raster()`\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", "For example, you can create an RGB image array from a PNG file using `PIL` and `io` packages.\n", "\n", "`geom_raster()` on the other hand is a regular `ggplot` graphic layer wich takes a data-frame as the input \n", "and applies the aesthetic mappings as specified by the `aes()` function." ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "execution": { "iopub.execute_input": "2026-01-27T17:08:47.480017Z", "iopub.status.busy": "2026-01-27T17:08:47.479934Z", "iopub.status.idle": "2026-01-27T17:08:47.482916Z", "shell.execute_reply": "2026-01-27T17:08:47.482626Z" } }, "outputs": [], "source": [ "import numpy as np\n", "import pandas as pd\n", "\n", "from lets_plot import *" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "execution": { "iopub.execute_input": "2026-01-27T17:08:47.484051Z", "iopub.status.busy": "2026-01-27T17:08:47.483983Z", "iopub.status.idle": "2026-01-27T17:08:47.485843Z", "shell.execute_reply": "2026-01-27T17:08:47.485555Z" } }, "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": "2026-01-27T17:08:47.498907Z", "iopub.status.busy": "2026-01-27T17:08:47.498823Z", "iopub.status.idle": "2026-01-27T17:08:48.089539Z", "shell.execute_reply": "2026-01-27T17:08:48.089088Z" } }, "outputs": [ { "data": { "text/plain": [ "(225, 225, 3)" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "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", "img = np.asarray(image)\n", "img.shape" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### RGB image\n", "\n", "M x N x 3 `ndarray` specifies a color RGB image." ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "execution": { "iopub.execute_input": "2026-01-27T17:08:48.090834Z", "iopub.status.busy": "2026-01-27T17:08:48.090701Z", "iopub.status.idle": "2026-01-27T17:08:48.158214Z", "shell.execute_reply": "2026-01-27T17:08:48.157782Z" } }, "outputs": [ { "data": { "text/html": [ " \n", " " ], "text/plain": [ "