from taipy.gui import Gui
from taipy.designer import Page

import matplotlib
matplotlib.use('Agg')  # Use a non-interactive backend

import matplotlib.pyplot as plt
import numpy as np
import matplotlib.colors as colors


def rbf(x, y):
    return 1.0 / (1 + 5 * ((x**2) + (y**2)))

N = 200
gain = 8
X, Y = np.mgrid[-3 : 3 : complex(0, N), -2 : 2 : complex(0, N)]
Z1 = rbf(X + 0.5, Y + 0.5)
Z2 = rbf(X - 0.5, Y - 0.5)
Z = gain * Z1 - Z2

shadeopts = {"cmap": "PRGn", "shading": "gouraud"}
colormap = "PRGn"
lnrwidth = 0.5

fig, ax = plt.subplots(2, 1, sharex=True, sharey=True)

pcm = ax[0].pcolormesh(
    X,
    Y,
    Z,
    norm=colors.SymLogNorm(
        linthresh=lnrwidth, linscale=1, vmin=-gain, vmax=gain, base=10
    ),
    **shadeopts
)
fig.colorbar(pcm, ax=ax[0], extend="both")
ax[0].text(-2.5, 1.5, "symlog")

pcm = ax[1].pcolormesh(X, Y, Z, vmin=-gain, vmax=gain, **shadeopts)
fig.colorbar(pcm, ax=ax[1], extend="both")
ax[1].text(-2.5, 1.5, "linear")


page = Page("j_matplotlib_figure.xprjson")
Gui(page).run(design=True)
