{ "cells": [ { "cell_type": "markdown", "id": "d0845d27", "metadata": {}, "source": [ "# Multiple Color Scales\n", "\n", "Use `color_by`/`fill_by` parameters and `paint_a`/`paint_b`/`paint_c` aesthetics if you need to display two different layers with the same color aesthetic but different color scales.\n", "Use [new scale functions](https://nbviewer.org/github/JetBrains/lets-plot/blob/master/docs/f-23a/scale_functions.ipynb) that allows to specify an aesthetic." ] }, { "cell_type": "code", "execution_count": 1, "id": "3b22b7e0", "metadata": { "execution": { "iopub.execute_input": "2024-04-17T07:29:50.596771Z", "iopub.status.busy": "2024-04-17T07:29:50.596381Z", "iopub.status.idle": "2024-04-17T07:29:50.913383Z", "shell.execute_reply": "2024-04-17T07:29:50.913150Z" } }, "outputs": [], "source": [ "from lets_plot import *" ] }, { "cell_type": "code", "execution_count": 2, "id": "0f2d11ac", "metadata": { "execution": { "iopub.execute_input": "2024-04-17T07:29:50.914817Z", "iopub.status.busy": "2024-04-17T07:29:50.914694Z", "iopub.status.idle": "2024-04-17T07:29:50.917087Z", "shell.execute_reply": "2024-04-17T07:29:50.916898Z" } }, "outputs": [ { "data": { "text/html": [ "\n", "
\n", " \n", " " ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "LetsPlot.setup_html()" ] }, { "cell_type": "code", "execution_count": 3, "id": "718fdb86", "metadata": { "execution": { "iopub.execute_input": "2024-04-17T07:29:50.918244Z", "iopub.status.busy": "2024-04-17T07:29:50.918085Z", "iopub.status.idle": "2024-04-17T07:29:50.920343Z", "shell.execute_reply": "2024-04-17T07:29:50.920149Z" } }, "outputs": [], "source": [ "def generate_data(p1, p2, p3, p4, p5, x_max=400, y_max=300):\n", " import math\n", "\n", " def calculate_z(x, y):\n", " z = math.sin(x * p1 * math.pi / x_max)\n", " z += math.sin(y * p2 * math.pi / y_max)\n", " z += x * p3 / x_max\n", " z += y * p4 / y_max\n", " return z * p5\n", "\n", " x = []\n", " y = []\n", " z = []\n", " for row in range(y_max - 1):\n", " for col in range(x_max - 1):\n", " x.append(col)\n", " y.append(row)\n", " z.append(calculate_z(col, row))\n", "\n", " return dict(x=x, y=y, z=z)" ] }, { "cell_type": "code", "execution_count": 4, "id": "59e32893", "metadata": { "execution": { "iopub.execute_input": "2024-04-17T07:29:50.921414Z", "iopub.status.busy": "2024-04-17T07:29:50.921289Z", "iopub.status.idle": "2024-04-17T07:29:50.983595Z", "shell.execute_reply": "2024-04-17T07:29:50.983402Z" } }, "outputs": [], "source": [ "height_data = generate_data(3, 3, 3, 5, 11)\n", "temperature_data = generate_data(1, 2, 5, 4, -.5)" ] }, { "cell_type": "code", "execution_count": 5, "id": "25d9ebc7", "metadata": { "execution": { "iopub.execute_input": "2024-04-17T07:29:50.984731Z", "iopub.status.busy": "2024-04-17T07:29:50.984648Z", "iopub.status.idle": "2024-04-17T07:29:51.830351Z", "shell.execute_reply": "2024-04-17T07:29:51.830113Z" } }, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ggplot() + \\\n", " geom_contour(aes('x', 'y', z='z', color='..level..'), \\\n", " data=height_data, bins=15, size=1) + \\\n", " geom_contour(aes('x', 'y', z='z', paint_a='..level..'), \\\n", " data=temperature_data, color_by='paint_a', \\\n", " bins=8, size=1) + \\\n", " scale_color_gradient(name=\"height\", low=\"#993404\", high=\"#ffffd4\") + \\\n", " scale_gradient('paint_a', name=\"temperature\", low=\"#0571b0\", high=\"#ca0020\") + \\\n", " ggsize(800, 600)" ] } ], "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": 5 }