Charts¶
Plotly¶
A Plotly figure is passed as it is. It stays interactive: hover labels, zoom, legend clicks. Its width follows the column, and its height comes from the figure.
import plotly.express as px
from respysive import Presentation, Slide
data = px.data.gapminder().query("country in ['Brazil', 'China', 'Japan', 'Nigeria']")
fig = px.line(data, x="year", y="lifeExp", color="country", markers=True, template="plotly_white",
labels={"year": "", "lifeExp": "Life expectancy (years)", "country": ""})
fig.update_layout(height=520, margin=dict(l=10, r=10, t=10, b=10), legend=dict(orientation="h", y=1.1, title=None))
slide = Slide()
slide.add_title("China gained 29 years in half a century")
slide.add_content([fig])
deck = Presentation()
deck.add_slide([slide])
deck.save_html("plotly.html", theme="sober")
The figure’s dictionary and its JSON string (fig.to_json()) work too.
Plotly is loaded from a CDN once for the whole deck, whatever the number
of charts.
Altair and Matplotlib¶
An Altair chart is passed as it is, or as a Vega-Lite specification
(chart.to_dict() or chart.to_json()). Its width and height
properties set its size in pixels, and tooltips work as in a notebook.
A Matplotlib Figure is converted to SVG, so it stays sharp when the
slide is scaled. figsize sets its proportions, and the figure then
takes the width of its column.
import altair as alt
import matplotlib
matplotlib.use("Agg")
import matplotlib.pyplot as plt
import plotly.express as px
from respysive import Presentation, Slide
latest = px.data.gapminder().query("year == 2007")
chart = (
alt.Chart(latest)
.mark_circle(size=90, opacity=0.8)
.encode(x=alt.X("gdpPercap:Q", scale=alt.Scale(type="log"), title="GDP per capita (USD)"),
y=alt.Y("lifeExp:Q", scale=alt.Scale(zero=False), title="Life expectancy (years)"),
color=alt.Color("continent:N", title=None), tooltip=["country", "lifeExp"])
.properties(width=460, height=380)
)
fig, ax = plt.subplots(figsize=(6, 4.6))
ax.hist(latest["lifeExp"], bins=15, color="#457b9d", edgecolor="white")
ax.set_xlabel("Life expectancy (years)")
ax.set_ylabel("Countries")
ax.spines[["top", "right"]].set_visible(False)
slide = Slide()
slide.add_title("Richer countries live longer, and most live past 70")
slide.add_content([chart, fig], columns=[6, 6])
plt.close(fig)
deck = Presentation()
deck.add_slide([slide])
deck.save_html("altair_matplotlib.html", theme="sober")
Charts in cards¶
A card’s graph key takes any of these chart types, see
Appearance and Cards and title pages.
Other JavaScript libraries¶
add_custom_content draws a chart with a library respysive does not
know. It takes a name for the library (letters, digits, and dashes), the
configuration object, the URLs to load, and the script that draws the
chart. The script receives config, whose config.container holds the
id of the div to draw in.
from respysive import Presentation, Slide
slide = Slide()
slide.add_title("Oceania lives 26 years longer than Africa")
slide.add_custom_content(
library="chartjs",
config={
"type": "bar",
"data": {
"labels": ["Africa", "Americas", "Asia", "Europe", "Oceania"],
"datasets": [{"label": "Life expectancy, 2007", "data": [54.8, 73.6, 70.7, 77.6, 80.7],
"backgroundColor": "#457b9d"}],
},
"options": {"maintainAspectRatio": False},
},
cdn={"js": ["https://cdn.jsdelivr.net/npm/chart.js@4.4.1/dist/chart.umd.min.js"]},
init_script=(
"new Chart(document.getElementById(config.container)"
".appendChild(document.createElement('canvas')), config)"
),
div_style={"height": "480px", "width": "100%"},
)
deck = Presentation()
deck.add_slide([slide])
deck.save_html("chartjs.html", theme="sober")