{ "cells": [ { "cell_type": "markdown", "id": "configured-sherman", "metadata": {}, "source": [ "# Linking Multiple Scatter Plots\n", "\n", "In this example we're going to link multiple scatter plots and later compose them intro a grid." ] }, { "cell_type": "code", "execution_count": 1, "id": "combined-affairs", "metadata": {}, "outputs": [], "source": [ "import numpy as np\n", "import pandas as pd\n", "import jscatter" ] }, { "cell_type": "markdown", "id": "7453a4c8-712a-44e5-aece-3d155716b85e", "metadata": {}, "source": [ "### Scatter Plots with 1-to-1 Correspondences\n", "\n", "The the straight-forward case, we have two scatter plots with a one-to-one correspondence of the data points. (E.g., think of comparing two embedding spaces of the same data that were generated with two different dimensionality reduction methods)" ] }, { "cell_type": "code", "execution_count": 2, "id": "383de8ff-08c8-450a-be86-c1c09acb799d", "metadata": {}, "outputs": [ { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "e55a37b33a36495380fb82d2bb3c9063", "version_major": 2, "version_minor": 0 }, "text/plain": [ "GridBox(children=(HBox(children=(VBox(children=(Button(button_style='primary', icon='arrows', layout=Layout(wi…" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import jscatter\n", "import numpy as np\n", "\n", "x = np.random.rand(500)\n", "y = np.random.rand(500)\n", "\n", "scatterA = jscatter.Scatter(x=x, y=y)\n", "scatterB = jscatter.Scatter(x=x, y=y)\n", "\n", "jscatter.link([scatterA, scatterB])" ] }, { "cell_type": "markdown", "id": "17f3e32d-529d-468a-adc3-16358f517613", "metadata": {}, "source": [ "In the following we'll look at a real-world use-case for the linking feature. In the example we're going to explore four different embeddings of the [Fashion MNIST dataset](Fashion MNIST).\n", "\n", "We'll start by loading the data..." ] }, { "cell_type": "code", "execution_count": 3, "id": "195bbcf0-80cd-4070-9082-434a35a95770", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", "\u001b[1m[\u001b[0m\u001b[34;49mnotice\u001b[0m\u001b[1;39;49m]\u001b[0m\u001b[39;49m A new release of pip is available: \u001b[0m\u001b[31;49m24.0\u001b[0m\u001b[39;49m -> \u001b[0m\u001b[32;49m24.2\u001b[0m\n", "\u001b[1m[\u001b[0m\u001b[34;49mnotice\u001b[0m\u001b[1;39;49m]\u001b[0m\u001b[39;49m To update, run: \u001b[0m\u001b[32;49mpip install --upgrade pip\u001b[0m\n", "Note: you may need to restart the kernel to use updated packages.\n" ] } ], "source": [ "%pip install pyarrow --quiet" ] }, { "cell_type": "code", "execution_count": 4, "id": "39e401a5-1346-4da5-b8df-4131cdf8836e", "metadata": {}, "outputs": [], "source": [ "import io\n", "import pyarrow as pa\n", "import requests\n", "\n", "r = requests.get(\n", " 'https://storage.googleapis.com/flekschas/regl-scatterplot/fashion-mnist-embeddings.arrow'\n", ")\n", "\n", "with pa.ipc.open_file(io.BytesIO(r.content)) as reader:\n", " embeddings = reader.read_pandas()\n", " embeddings['class'] = embeddings['class'].astype('category')" ] }, { "cell_type": "markdown", "id": "6ef585c7-7744-4c56-a53f-1a90ee14d4f6", "metadata": {}, "source": [ "...and then instantiate four scatters with a popping color map..." ] }, { "cell_type": "code", "execution_count": 5, "id": "9138e2aa-8276-4e94-a1b2-51de0da01d41", "metadata": {}, "outputs": [ { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "b91f28727d164fb6bf7fd3790040fa84", "version_major": 2, "version_minor": 0 }, "text/plain": [ "GridBox(children=(HBox(children=(VBox(children=(Button(button_style='primary', icon='arrows', layout=Layout(wi…" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "config = dict(\n", " background_color='#111111',\n", " color_by='class',\n", " color_map=[\n", " '#FFFF00',\n", " '#1CE6FF',\n", " '#FF34FF',\n", " '#FF4A46',\n", " '#008941',\n", " '#006FA6',\n", " '#A30059',\n", " '#FFDBE5',\n", " '#7A4900',\n", " '#0000A6',\n", " ],\n", ")\n", "\n", "scatter_pca = jscatter.Scatter(data=embeddings, x='pcaX', y='pcaY', **config)\n", "scatter_tsne = jscatter.Scatter(data=embeddings, x='tsneX', y='tsneY', **config)\n", "scatter_umap = jscatter.Scatter(data=embeddings, x='umapX', y='umapY', **config)\n", "scatter_cae = jscatter.Scatter(data=embeddings, x='caeX', y='caeY', **config)\n", "\n", "jscatter.link(\n", " [scatter_pca, scatter_tsne, scatter_umap, scatter_cae], rows=2, row_height=240\n", ")" ] }, { "cell_type": "markdown", "id": "9a2d124d-ab3d-4846-af05-bce410054f83", "metadata": {}, "source": [ "In clockwise order, the embeddings are: PCA, t-SNE, convolutional autoencoder, and UMAP." ] }, { "cell_type": "markdown", "id": "excessive-venture", "metadata": {}, "source": [ "### Scatter Plots with N-to-M Correspondences\n", "\n", "Now to the more complex case where we have an N-by-M correspondence between the data points.\n", "\n", "To simulate this case, we first generate some dummy grid data where the first dataset has `4` points, the second has `16`, and the third has `64` points.\n", "\n", "Hence, a point in the first scatter plot corresponds to 4 points in the second scatter plot and 16 points in the third scatter plot.\n", "\n", "To be able to determine the correspondences between points, we need to have a column that specifies the membership of points to a common set of groups. In this examply we have four groups (`0` to `3`) and all points in the three datasets assigned to one of these three groups." ] }, { "cell_type": "code", "execution_count": 6, "id": "automated-producer", "metadata": {}, "outputs": [], "source": [ "X1, Y1 = np.mgrid[0:2:1, 0:2:1]\n", "X2, Y2 = np.mgrid[0:4:1, 0:4:1]\n", "X3, Y3 = np.mgrid[0:8:1, 0:8:1]\n", "\n", "df1 = pd.DataFrame(\n", " np.concatenate(\n", " (\n", " np.expand_dims(X1.flatten(), axis=1),\n", " np.expand_dims(Y1.flatten(), axis=1),\n", " np.expand_dims(np.repeat(np.arange(4), 1), axis=1),\n", " ),\n", " axis=1,\n", " ),\n", " columns=['x', 'y', 'group'],\n", ")\n", "df1.group = df1.group.astype('category')\n", "\n", "df2 = pd.DataFrame(\n", " np.concatenate(\n", " (\n", " np.expand_dims(X2.flatten(), axis=1),\n", " np.expand_dims(Y2.flatten(), axis=1),\n", " np.expand_dims(np.repeat(np.arange(4), 4), axis=1),\n", " ),\n", " axis=1,\n", " ),\n", " columns=['x', 'y', 'group'],\n", ")\n", "df2.group = df2.group.astype('category')\n", "\n", "df3 = pd.DataFrame(\n", " np.concatenate(\n", " (\n", " np.expand_dims(X3.flatten(), axis=1),\n", " np.expand_dims(Y3.flatten(), axis=1),\n", " np.expand_dims(np.repeat(np.arange(4), 16), axis=1),\n", " ),\n", " axis=1,\n", " ),\n", " columns=['x', 'y', 'group'],\n", ")\n", "df3.group = df3.group.astype('category')" ] }, { "cell_type": "markdown", "id": "e114b8bf-92a2-4005-bc98-9cbd8042b2d0", "metadata": {}, "source": [ "For instance, in the second dataset (`df2`) we see that the first four points are assigned to group `0`. So either one of these four points from dataset two correspond to the first point of the first dataset (`df1`) as that point is also assigned to group `0`." ] }, { "cell_type": "code", "execution_count": 7, "id": "ffbb9853-d792-4537-8e97-102b7a8be0e4", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
xygroup
0000
1010
2020
3030
4101
5111
\n", "
" ], "text/plain": [ " x y group\n", "0 0 0 0\n", "1 0 1 0\n", "2 0 2 0\n", "3 0 3 0\n", "4 1 0 1\n", "5 1 1 1" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df2.head(6)" ] }, { "cell_type": "markdown", "id": "b65f029c-5761-44f3-944e-4d666d8a2e78", "metadata": {}, "source": [ "Next, we create three scatter plot instances as usual." ] }, { "cell_type": "code", "execution_count": 8, "id": "signed-hello", "metadata": {}, "outputs": [], "source": [ "sc1 = jscatter.Scatter(data=df1, x='x', y='y', color_by='group', size=24)\n", "sc2 = jscatter.Scatter(data=df2, x='x', y='y', color_by='group', size=24)\n", "sc3 = jscatter.Scatter(data=df3, x='x', y='y', color_by='group', size=24)" ] }, { "cell_type": "markdown", "id": "sticky-proposition", "metadata": {}, "source": [ "Finally, to tell jscatter about the point correspondences, all we have to do is to specify which column contains the group information using the `match_by` argument." ] }, { "cell_type": "code", "execution_count": 9, "id": "9211d947-93fd-4ce0-ad1b-6f1461a34101", "metadata": {}, "outputs": [ { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "2c5f9bd424bf4138be9eb0a7e7595032", "version_major": 2, "version_minor": 0 }, "text/plain": [ "GridBox(children=(HBox(children=(VBox(children=(Button(button_style='primary', icon='arrows', layout=Layout(wi…" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "jscatter.link([sc1, sc2, sc3], match_by='group')" ] }, { "cell_type": "markdown", "id": "1a8f7886-0d42-4227-b0f4-ff6780103d6b", "metadata": {}, "source": [ "Try selecting a point in the left scatter plot to see how the corresponding points in the other two scatter plots are selected.\n", "\n", "Note that the hover state is limited to a single point, which means when you mouse over the top-left point in the left-most scatter plot, only one out of the `4` and `16` corresponding points in the other two scatter plots is highlighted. Unfortunately, there is no way around this limitation at the moment." ] }, { "cell_type": "markdown", "id": "1e4ded38-fadf-46d9-998f-8a667dd5a183", "metadata": {}, "source": [ "### Control Linking\n", "\n", "If you need control over which interaction (view, selection, or hover) is linked, use `compose()` instead of `link()`. The APIs are very similar. In fact, `link()` is just a shorthand for `compose(sync_view=True, sync_selection=True, sync_hover=True)`.\n", "\n", "For example, the example below we show the same four scatter plots from above but this time the views are _not_ linked." ] }, { "cell_type": "code", "execution_count": 10, "id": "christian-factor", "metadata": {}, "outputs": [ { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "b129dde5017b4c21b82d01099b695c61", "version_major": 2, "version_minor": 0 }, "text/plain": [ "GridBox(children=(HBox(children=(VBox(children=(Button(button_style='primary', icon='arrows', layout=Layout(wi…" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "jscatter.compose(\n", " [jscatter.Scatter(x=x, y=y) for i in range(4)],\n", " sync_selection=True,\n", " sync_hover=True,\n", " rows=2,\n", ")" ] }, { "cell_type": "code", "execution_count": null, "id": "104e242b-4585-498f-84ae-7073ea98dfb5", "metadata": {}, "outputs": [], "source": [] } ], "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.12.3" } }, "nbformat": 4, "nbformat_minor": 5 }