{ "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": [ "" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ggplot() + \\\n", " geom_point(aes(x='x', y='y', color='z'), data=df[df.z > 0.5].sort_values(by='z')) + \\\n", " coord_fixed() + theme_layer + \\\n", " ggtitle('Elliptic Curve by Points')" ] }, { "cell_type": "code", "execution_count": 8, "metadata": { "execution": { "iopub.execute_input": "2024-04-17T07:35:03.770502Z", "iopub.status.busy": "2024-04-17T07:35:03.770409Z", "iopub.status.idle": "2024-04-17T07:35:03.847672Z", "shell.execute_reply": "2024-04-17T07:35:03.847461Z" } }, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ggplot() + \\\n", " geom_bin2d(aes(x='x', y='y', fill='z'), data=df, stat='identity', color=BG_CLR) + \\\n", " theme_layer + \\\n", " ggtitle('Elliptic Curve by 2D-Bins')" ] }, { "cell_type": "code", "execution_count": 9, "metadata": { "execution": { "iopub.execute_input": "2024-04-17T07:35:03.850727Z", "iopub.status.busy": "2024-04-17T07:35:03.850619Z", "iopub.status.idle": "2024-04-17T07:35:03.927553Z", "shell.execute_reply": "2024-04-17T07:35:03.927339Z" } }, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ggplot() + \\\n", " geom_tile(aes(x='x', y='y', fill='z'), data=df, color=BG_CLR) + \\\n", " theme_layer + \\\n", " ggtitle('Elliptic Curve by Tiles')" ] }, { "cell_type": "code", "execution_count": 10, "metadata": { "execution": { "iopub.execute_input": "2024-04-17T07:35:03.930422Z", "iopub.status.busy": "2024-04-17T07:35:03.930247Z", "iopub.status.idle": "2024-04-17T07:35:04.006380Z", "shell.execute_reply": "2024-04-17T07:35:04.006160Z" } }, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ggplot() + \\\n", " geom_raster(aes(x='x', y='y', fill='z'), data=df) + \\\n", " theme_layer + \\\n", " ggtitle('Elliptic Curve by Raster')" ] }, { "cell_type": "code", "execution_count": 11, "metadata": { "execution": { "iopub.execute_input": "2024-04-17T07:35:04.009311Z", "iopub.status.busy": "2024-04-17T07:35:04.009206Z", "iopub.status.idle": "2024-04-17T07:35:04.054848Z", "shell.execute_reply": "2024-04-17T07:35:04.054647Z" } }, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ggplot() + \\\n", " geom_contour(aes(x='x', y='y', z='z', color='..level..'), data=df, bins=4) + \\\n", " theme_layer + \\\n", " ggtitle('Elliptic Curve by Contours')" ] }, { "cell_type": "code", "execution_count": 12, "metadata": { "execution": { "iopub.execute_input": "2024-04-17T07:35:04.055985Z", "iopub.status.busy": "2024-04-17T07:35:04.055909Z", "iopub.status.idle": "2024-04-17T07:35:04.108422Z", "shell.execute_reply": "2024-04-17T07:35:04.108218Z" } }, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ggplot() + \\\n", " geom_contourf(aes(x='x', y='y', z='z', fill='..level..'), data=df, binwidth=.3) + \\\n", " theme_layer + \\\n", " ggtitle('Elliptic Curve by Filled Contours')" ] } ], "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": 4 }