On this page

Clifford Interact#

Download this notebook from GitHub (right-click to download).


import numpy as np
import pandas as pd
import datashader as ds
import panel as pn

from numba import jit
from datashader import transfer_functions as tf
from colorcet import palette_n

pn.extension(sizing_mode="stretch_width")

This example demonstrates how to use the pn.interact function to trigger updates in an image of a Clifford attractor generated using the Datashader library.

@jit(nopython=True)
def clifford_trajectory(a, b, c, d, x0, y0, n):
    xs, ys = np.zeros(n), np.zeros(n)
    xs[0], ys[0] = x0, y0
    for i in np.arange(n-1):
        xs[i+1] = np.sin(a * ys[i]) + c * np.cos(a * xs[i])
        ys[i+1] = np.sin(b * xs[i]) + d * np.cos(b * ys[i])
    return xs, ys

ps = {k:p[::-1] for k,p in palette_n.items()}

def clifford_plot(a=1.9, b=1.9, c=1.9, d=0.8, n=1000000, cmap=ps['kbc']):
    cvs = ds.Canvas(plot_width=600, plot_height=600)
    xs, ys = clifford_trajectory(a, b, c, d, 0, 0, n)
    df = pd.DataFrame(dict(x=xs,y=ys))
    agg = cvs.points(df, 'x', 'y')
    return tf.shade(agg, cmap=cmap)

pane = pn.interact(clifford_plot, a=(0,2), b=(0,2), c=(0,2), d=(0,2), n=(int(1),int(2e7)), cmap=ps)

logo = "https://tinyurl.com/y9c2zn65/logo_stacked_s.png"
text = pn.pane.Markdown("""
This example demonstrates **how to use the ``pn.interact`` function** to trigger updates in an image of a Clifford [attractor](https://en.wikipedia.org/wiki/Attractor) generated using the [Datashader](https://datashader.org) library. We speed up things using the **[numba](http://numba.pydata.org/) `jit` decorator**.

You can **use the widgets** to vary the parameters of this [Clifford attractor](https://anaconda.org/jbednar/clifford_attractor).

**Many values result in nearly blank plots** that contain only a few scattered points.""")

pn.Row(pn.Column(logo, text, pane[0], width=400, sizing_mode="fixed"), pn.layout.VSpacer(width=50), pane[1])

App#

Lets wrap it into nice template that can be served via panel serve clifford_interact.ipynb

pn.template.FastListTemplate(
    site="Panel", 
    title="Clifford Attractor using Panel Interact, Numba and Datashader", 
    sidebar=[logo, pane[0]], 
    main=[text, pane[1]],
    main_max_width="650px"
).servable();
This web page was generated from a Jupyter notebook and not all interactivity will work on this website. Right click to download and run locally for full Python-backed interactivity.

Download this notebook from GitHub (right-click to download).