Skip to content

Plots

Datapane supports all major Python visualization libraries, allowing you to add interactive plots and visualizations to your report.

The dp.Plot block takes a plot object from one of the supported Python visualisation libraries and renders it in your report.

Info

Datapane will automatically wrap your visualization or plot in a dp.Plot block if you pass it into your report directly.

It takes the following parameters:

  • name: Sets the name of the chart within the caption
  • caption : Adds a caption beneath your plot
  • responsive: Boolean (True by default) which controls whether the plot fills the block. Set to False if you want to manually specify the height and width within the plot object.
dp.Plot(
    fig, name="fig1", caption="Chart showing average life expectancy", responsive=False
)

Datapane currently supports the following libraries:

Library Site / Docs
Altair https://altair-viz.github.io/
Matplotlib / Seaborn https://matplotlib.org/ / https://seaborn.pydata.org/
Bokeh https://bokeh.org/
Plotapi https://plotapi.com
Plotly https://plotly.com/python/

If you're using another visualization library e.g. Pyvis for networks, try saving your chart as a local HTML file and wrapping that in a dp.HTML block.

Altair

Altair is a declarative statistical visualization library for Python, based on Vega and Vega-Lite. Altair’s API is simple, friendly and consistent and built on top of the powerful Vega-Lite visualization grammar. This elegant simplicity produces beautiful and effective visualizations with a minimal amount of code.

To get started using Altair to make your visualizations, begin with Altair's Documentation

import altair as alt
import datapane as dp
import pandas as pd
from vega_datasets import data as vega_data

gap = pd.read_json(vega_data.gapminder.url)

select_year = alt.selection_single(
    name="select",
    fields=["year"],
    init={"year": 1955},
    bind=alt.binding_range(min=1955, max=2005, step=5),
)

alt_chart = (
    alt.Chart(gap)
    .mark_point(filled=True)
    .encode(
        alt.X("fertility", scale=alt.Scale(zero=False)),
        alt.Y("life_expect", scale=alt.Scale(zero=False)),
        alt.Size("pop:Q"),
        alt.Color("cluster:N"),
        alt.Order("pop:Q", sort="descending"),
    )
    .add_selection(select_year)
    .transform_filter(select_year)
)

dp.Report(dp.Plot(alt_chart)).save(path="altair-plot.html")

Bokeh

Bokeh is an interactive visualization library which provides elegant, concise construction of versatile graphics, and affords high-performance interactivity over large datasets.

To get started using Bokeh to make your visualizations, begin with Bokeh's User Guide.

from bokeh.plotting import figure, output_file, show
from bokeh.sampledata.iris import flowers
import datapane as dp

colormap = {"setosa": "red", "versicolor": "green", "virginica": "blue"}
colors = [colormap[x] for x in flowers["species"]]

bokeh_chart = figure(title="Iris Morphology")
bokeh_chart.xaxis.axis_label = "Petal Length"
bokeh_chart.yaxis.axis_label = "Petal Width"

bokeh_chart.circle(
    flowers["petal_length"],
    flowers["petal_width"],
    color=colors,
    fill_alpha=0.2,
    size=10,
)

dp.Report(dp.Plot(bokeh_chart)).save(path="bokeh-plot.html")

Matplotlib

Matplotlib is the original Python visualisation library, often supported and used with Jupyter Notebooks. Matplotlib plots are not interactive in Datapane Reports, but are saved as SVGs so can be viewed at high fidelity.

Higher-level matplotlib libraries such as Seaborn are also supported, and can be used in a similar way to the matplotlib example below,

import matplotlib.pyplot as plt
import pandas as pd
import datapane as dp
from vega_datasets import data as vega_data

gap = pd.read_json(vega_data.gapminder.url)
fig = gap.plot.scatter(x="life_expect", y="fertility")

dp.Report(dp.Plot(fig)).save(path="matplotlib-plot.html")

Info

You can pass either a matplotlib Figure or Axes object to dp.Plot, you can obtain the current global figure from matplotlib by running plt.gcf()

Plotapi

Plotapi (or PlotAPI) is a visualization package that's beautiful by default. It enables beautiful and interactive visualizations with just a single line of code.

Check out the gallery of Plotapi examples to get started.

import datapane as dp
from plotapi import Chord

Chord.set_license(plotapi_username, plotapi_license)

matrix = [
    [0, 5, 6, 4, 7, 4],
    [5, 0, 5, 4, 6, 5],
    [6, 5, 0, 4, 5, 5],
    [4, 4, 4, 0, 5, 5],
    [7, 6, 5, 5, 0, 4],
    [4, 5, 5, 5, 4, 0],
]

names = ["Action", "Adventure", "Comedy", "Drama", "Fantasy", "Thriller"]

fig = Chord(matrix, names)

dp.Report(dp.Plot(fig)).save(path="plotapi-plot.html")

Plotly

Plotly's Python graphing library makes interactive, publication-quality graphs.

import plotly.express as px
import datapane as dp

df = px.data.gapminder()

plotly_chart = px.scatter(
    df.query("year==2007"),
    x="gdpPercap",
    y="lifeExp",
    size="pop",
    color="continent",
    hover_name="country",
    log_x=True,
    size_max=60,
)

dp.Report(dp.Plot(plotly_chart)).save(path="plotly-plot.html")