On this page

Dynamic Plot Layout#

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


import panel as pn
import numpy as np
import holoviews as hv

pn.extension(sizing_mode="stretch_width")

This example demonstrates how to dynamically add and remove plots from a layout (in this case a Column) using the pop and append methods (requires a live Python server):

slider = pn.widgets.IntSlider(name='N', start=0, end=5)

column = pn.Column('This column will be populated with N Plots', sizing_mode="stretch_width")

def set_plots(event):
    nplots = event.new-event.old
    for i in range(abs(nplots)):
        if nplots < 0:
            column.pop(len(column)-1)
        elif nplots > 0:
            column.append(hv.Scatter(np.random.randn(1000, 2)).opts(responsive=True, height=300))
            
    column[0]=f'This column will be populated with {event.new} Plots'
    
slider.param.watch(set_plots, 'value')
slider.value = 1

pn.Row(pn.Column(slider, sizing_mode="fixed", width=300), column, sizing_mode="stretch_width")

App#

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

description = "This example demonstrates **how to dynamically add and remove plots** from a layout (in this case a Column) using the ``pop`` and ``append`` methods."
pn.template.FastListTemplate(site="Panel", title="Dynamic Plot Layout", sidebar=[slider], main=[description, column]).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).