Hvplot/holoviews/geoviews/panel/datashader

https://anaconda.org/pyviz/project/webinar/revisions 

Hvplot

Data

from hvplot.sample_data import airline_flights


flights = airline_flights.to_dask().persist()

Configure

pd.set_option("plotting.backend", "hvplot")

or 

df.hvplot(...)

Pandas

import hvplot.pandas

Scatter

df.hvplot.scatter(x="x", y="y", hover_cols=["col1", "col2"])

df.hvplot.scatter(x="x", y="y", hover_cols="all")

Overlay plots

df_line_space.hvplot.line(

    x="x",

    y="y",

    xlabel="xlabel",

    ylabel="ylabel",

) * df.hvplot.scatter(

    x="x2", y="y2", c="value"

)


(df.hvplot.line(label="df") * df2.hvplot.line(label="df2")).opts(legend_position='top_left')

Datashade

df.hvplot.line(y='COL', datashade=True, aggregator='any')

Geopandas

import hvplot.pandas

Contour plot

gdf.hvplot(c="COL")

gdf.hvplot(c="COL", geo=True, tiles="OSM", frame_width=600, frame_height=600, title="title")

Point plot

gdf.hvplot(geo=True, tiles="OSM", frame_width=600, frame_height=600, color="red", hover_cols=["timestamp"])

gdf.hvplot.points("lon", "lat", geo=True, tiles="OSM")


gdf.hvplot(

    c="red", geo=True, tiles="OSM", frame_width=600, frame_height=600, title="title"

) * gdf2.hvplot(

    c="blue", geo=True)

xarray

import hvplot.xarray

import matplotlib.pyplot as plt


ds.hvplot(cmap=plt.cm.viridis.name)


da.hvplot.quadmesh(x='longitude', y='latitude', rasterize=True, geo=True, tiles='OSM', cmap='turbo')


da.hvplot.image()

Holoviews

from holoviews.operation.datashader import rasterize

import holoviews as hv


hv.element.tiles.EsriImagery()

hv.element.tiles.EsriImagery().opts(width=600, height=550)


hv_pts = hv.Points(

    gdf[["latitude", "longitude"]], kdims=["longitude", "latitude"], vdims=[]

)


hv.Image(arr, kdims=["dim_1", "dim_0"])

Geoviews

import geoviews as gv

import geoviews.feature as gf

gv.extension("bokeh")

Background/tiles

tiles = gv.tile_sources.OSM

tiles = gv.tile_sources.Wikipedia

tiles = gv.tile_sources.CartoMidnight

tiles = gv.tile_sources.EsriImagery

tiles = gv.tile_sources.EsriNatGeo # good for ocean as shows bathymetry

Geopandas

gv.Points(gdf, vdims=[])

gv.Polygons(gdf)

Xarray

gds = gv.Dataset(ds[VAR])

gds.to(gv.Image).opts(tools=["hover"])


gv_img = gds.to(gv.Image, kdims=["lon", "lat"])

gv_raster = rasterize(gv_img)

gv_pts = gv.Points(gdf)


gf.coastline()

gf.borders()

gf.states(color=None)


gv_raster.opts(

    tools=["hover"],

    width=1000,

    height=600,

    cmap=plt.cm.viridis.name,

    colorbar=True,

    colorbar_position="right",

)


gv_pts.opts(

    tools=["hover"], color="black", size=2

)



Panel

Interactive plotting

import hvplot.xarray

import panel.widgets as pnw

ds["VAR"].interactive.sel(time=pnw.DiscreteSlider).plot()

dashboard

import panel as pn

import panel.widgets as pnw

from matplotlib.figure import Figure


pn.extension(sizing_mode="stretch_width")

pn.extension(sizing_mode="scale_both")

 Text

pn.panel("# Title").servable()

pn.panel("## subtitle").servable()

 Select box

value = pn.widgets.Select(name="value:", options=["a", "b"]).servable()

@pn.depends(value)

def plot_value(value: str) -> pn.Column:

    fig0 = Figure()

    ax0 = fig0.subplots()

    df[df["COL"] == value].plot(ax=ax0)

    return fig0

pn.panel(fig0).servable()

 Plots

plot = gdf.hvplot(...)

pn.panel(plot).servable()

 App layout

pn.Column(

    "# hello",

    "## yo",

    pn.Row(fig0, fig1),

    ).servable()

 HTML

pn.pane.HTML(html_report).servable()

 File uploader

file_input = pn.widgets.FileInput(accept=".json")

df = pn.widgets.DataFrame()

def load_json(data):

    if data is not None:

        df.value = pd.read_json(io.BytesIO(data), orient="index")

        return df.value

f = pn.bind(load_json, file_input.param.value)

pn.Column(file_input, f).servable()

 File downloader

file_download = pn.widgets.FileDownload(file='FileDownload.ipynb', filename='custom_filename.ipynb')

 Upload and download

def _callback():

    if file_input.value is not None:

        df = pd.read_csv(io.BytesIO(file_input.value))

        return io.BytesIO(df.iloc[0].to_csv(index=False).encode())

    else:

        return io.BytesIO()



def _disable_download(event):

    if file_input.value is None:

        file_download.disabled = True

    else:

        file_download.disabled = False



file_input = pn.widgets.FileInput(accept=".csv", mime_type="text/csv")

file_download = pn.widgets.FileDownload(

    filename="out.csv",

    callback=_callback,

    disabled=True,

)

pn.bind(_disable_download, file_input.param.value, watch=True)

pn.Column(file_input, file_download).servable()

$ panel serve app.py --show --autoreload --port 5007

http://localhost:5007/ 

Datashader

import datashader as ds

cvs = ds.Canvas(plot_width=850, plot_height=500)

agg = cvs.points(df, 'Longitude', 'Latitude')

img = ds.tf.shade(agg, cmap=colorcet.fire, how='log')