{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Simple Way to Draw a Curve" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "execution": { "iopub.execute_input": "2026-01-27T17:09:29.507346Z", "iopub.status.busy": "2026-01-27T17:09:29.507276Z", "iopub.status.idle": "2026-01-27T17:09:29.510334Z", "shell.execute_reply": "2026-01-27T17:09:29.510041Z" } }, "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": "2026-01-27T17:09:29.527426Z", "iopub.status.busy": "2026-01-27T17:09:29.527328Z", "iopub.status.idle": "2026-01-27T17:09:29.528916Z", "shell.execute_reply": "2026-01-27T17:09:29.528532Z" } }, "outputs": [], "source": [ "BG_CLR = '#253494'\n", "EC_CLR = '#ffffcc'" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "execution": { "iopub.execute_input": "2026-01-27T17:09:29.529690Z", "iopub.status.busy": "2026-01-27T17:09:29.529613Z", "iopub.status.idle": "2026-01-27T17:09:29.531108Z", "shell.execute_reply": "2026-01-27T17:09:29.530895Z" } }, "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": "2026-01-27T17:09:29.531803Z", "iopub.status.busy": "2026-01-27T17:09:29.531732Z", "iopub.status.idle": "2026-01-27T17:09:29.533206Z", "shell.execute_reply": "2026-01-27T17:09:29.532984Z" } }, "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": "2026-01-27T17:09:29.533797Z", "iopub.status.busy": "2026-01-27T17:09:29.533728Z", "iopub.status.idle": "2026-01-27T17:09:29.536012Z", "shell.execute_reply": "2026-01-27T17:09:29.535798Z" } }, "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": "2026-01-27T17:09:29.536686Z", "iopub.status.busy": "2026-01-27T17:09:29.536614Z", "iopub.status.idle": "2026-01-27T17:09:29.538599Z", "shell.execute_reply": "2026-01-27T17:09:29.538371Z" } }, "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": "2026-01-27T17:09:29.539353Z", "iopub.status.busy": "2026-01-27T17:09:29.539281Z", "iopub.status.idle": "2026-01-27T17:09:29.574646Z", "shell.execute_reply": "2026-01-27T17:09:29.574415Z" } }, "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": "2026-01-27T17:09:29.575651Z", "iopub.status.busy": "2026-01-27T17:09:29.575562Z", "iopub.status.idle": "2026-01-27T17:09:29.656660Z", "shell.execute_reply": "2026-01-27T17:09:29.656366Z" } }, "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": "2026-01-27T17:09:29.660192Z", "iopub.status.busy": "2026-01-27T17:09:29.660094Z", "iopub.status.idle": "2026-01-27T17:09:29.739319Z", "shell.execute_reply": "2026-01-27T17:09:29.738960Z" } }, "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": "2026-01-27T17:09:29.741643Z", "iopub.status.busy": "2026-01-27T17:09:29.741564Z", "iopub.status.idle": "2026-01-27T17:09:29.821079Z", "shell.execute_reply": "2026-01-27T17:09:29.820800Z" } }, "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": "2026-01-27T17:09:29.823614Z", "iopub.status.busy": "2026-01-27T17:09:29.823536Z", "iopub.status.idle": "2026-01-27T17:09:29.873537Z", "shell.execute_reply": "2026-01-27T17:09:29.872974Z" } }, "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": "2026-01-27T17:09:29.874541Z", "iopub.status.busy": "2026-01-27T17:09:29.874458Z", "iopub.status.idle": "2026-01-27T17:09:29.931387Z", "shell.execute_reply": "2026-01-27T17:09:29.931081Z" } }, "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 }