import numpy as np
import pandas as pd
from lets_plot import *
LetsPlot.setup_html()
BG_CLR = '#253494'
EC_CLR = '#ffffcc'
# Curve parametrized equation
def ec_eq(*, a=0, b=0):
return lambda x, y: y ** 2 - x ** 3 - a * x - b
def level(X, Y, *, eq, c=1):
level_map = lambda z: np.exp(-c * np.abs(z))
return level_map(eq(X, Y))
dx, dy = 1.0 / 30.0, 1.0 / 10.0
x = np.arange(-3, 3, dx)
y = np.arange(-3, 3, dy)
X, Y = np.meshgrid(x, y)
curve_eq = ec_eq(a=-1, b=0)
Z = level(X, Y, eq=curve_eq, c=10)
df = pd.DataFrame(dict(x=X.reshape(-1), y=Y.reshape(-1), z=Z.reshape(-1)))
theme_layer = scale_color_gradient(low=BG_CLR, high=EC_CLR) + scale_fill_gradient(low=BG_CLR, high=EC_CLR) + \
xlim(df.x.min(), df.x.max()) + ylim(df.y.min(), df.y.max()) + ggsize(600, 450) + \
theme_classic() + theme(axis='blank', panel_background=element_rect(color=BG_CLR, fill=BG_CLR))
ggplot() + \
geom_point(aes(x='x', y='y', color='z'), data=df[df.z > 0.5].sort_values(by='z')) + \
coord_fixed() + theme_layer + \
ggtitle('Elliptic Curve by Points')
ggplot() + \
geom_bin2d(aes(x='x', y='y', fill='z'), data=df, stat='identity', color=BG_CLR) + \
theme_layer + \
ggtitle('Elliptic Curve by 2D-Bins')