{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Simple Way to Draw a Curve" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "execution": { "iopub.execute_input": "2024-04-17T07:35:03.384879Z", "iopub.status.busy": "2024-04-17T07:35:03.384795Z", "iopub.status.idle": "2024-04-17T07:35:03.709017Z", "shell.execute_reply": "2024-04-17T07:35:03.708527Z" } }, "outputs": [ { "data": { "text/html": [ "\n", "
\n", " \n", " " ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "import numpy as np\n", "import pandas as pd\n", "\n", "from lets_plot import *\n", "LetsPlot.setup_html()" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "execution": { "iopub.execute_input": "2024-04-17T07:35:03.722327Z", "iopub.status.busy": "2024-04-17T07:35:03.722169Z", "iopub.status.idle": "2024-04-17T07:35:03.723670Z", "shell.execute_reply": "2024-04-17T07:35:03.723496Z" } }, "outputs": [], "source": [ "BG_CLR = '#253494'\n", "EC_CLR = '#ffffcc'" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "execution": { "iopub.execute_input": "2024-04-17T07:35:03.724860Z", "iopub.status.busy": "2024-04-17T07:35:03.724709Z", "iopub.status.idle": "2024-04-17T07:35:03.726177Z", "shell.execute_reply": "2024-04-17T07:35:03.725993Z" } }, "outputs": [], "source": [ "# Curve parametrized equation\n", "def ec_eq(*, a=0, b=0):\n", " return lambda x, y: y ** 2 - x ** 3 - a * x - b" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "execution": { "iopub.execute_input": "2024-04-17T07:35:03.727343Z", "iopub.status.busy": "2024-04-17T07:35:03.727227Z", "iopub.status.idle": "2024-04-17T07:35:03.728785Z", "shell.execute_reply": "2024-04-17T07:35:03.728605Z" } }, "outputs": [], "source": [ "def level(X, Y, *, eq, c=1):\n", " level_map = lambda z: np.exp(-c * np.abs(z))\n", " return level_map(eq(X, Y))" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "execution": { "iopub.execute_input": "2024-04-17T07:35:03.729910Z", "iopub.status.busy": "2024-04-17T07:35:03.729800Z", "iopub.status.idle": "2024-04-17T07:35:03.732091Z", "shell.execute_reply": "2024-04-17T07:35:03.731908Z" } }, "outputs": [], "source": [ "dx, dy = 1.0 / 30.0, 1.0 / 10.0\n", "\n", "x = np.arange(-3, 3, dx)\n", "y = np.arange(-3, 3, dy)\n", "X, Y = np.meshgrid(x, y)\n", "curve_eq = ec_eq(a=-1, b=0)\n", "Z = level(X, Y, eq=curve_eq, c=10)\n", "\n", "df = pd.DataFrame(dict(x=X.reshape(-1), y=Y.reshape(-1), z=Z.reshape(-1)))" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "execution": { "iopub.execute_input": "2024-04-17T07:35:03.733183Z", "iopub.status.busy": "2024-04-17T07:35:03.733071Z", "iopub.status.idle": "2024-04-17T07:35:03.735021Z", "shell.execute_reply": "2024-04-17T07:35:03.734839Z" } }, "outputs": [], "source": [ "theme_layer = scale_color_gradient(low=BG_CLR, high=EC_CLR) + scale_fill_gradient(low=BG_CLR, high=EC_CLR) + \\\n", " xlim(df.x.min(), df.x.max()) + ylim(df.y.min(), df.y.max()) + ggsize(600, 450) + \\\n", " theme_void() + theme(panel_background=element_rect(color=BG_CLR, fill=BG_CLR))" ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "execution": { "iopub.execute_input": "2024-04-17T07:35:03.735958Z", "iopub.status.busy": "2024-04-17T07:35:03.735845Z", "iopub.status.idle": "2024-04-17T07:35:03.769452Z", "shell.execute_reply": "2024-04-17T07:35:03.769215Z" } }, "outputs": [ { "data": { "text/html": [ " \n", " " ], "text/plain": [ "