Generate one plot per revealjs slide in python for loop using Quarto
Question:
I’m trying to generate a slide deck consisting of several plots using Quarto with revealjs output format. I need to generate these plots with plotly through a loop, but that is messing up my output. I’m getting the plots lined up vertically, which doesn’t fit the slide size. What I want to achieve is one plot per slide. Is that possible?
I havn’t been able to find any solution so far. Here’s some code to replicate the problem. Note that I only used Iris to get an example similar to mine, I know the for loop doesn’t make much sense in this case.
---
title: "Foor Loops in Quarto"
format:
revealjs:
theme: default
jupyter: ds
code-fold: true
execute:
echo: false
freeze: auto
---
```{python}
#| include: false
# Imports
from sklearn import datasets
import pandas as pd
import plotly.express as px
```
```{python}
#| include: false
# Get data
iris = datasets.load_iris()
df = pd.DataFrame(iris['data'], columns=iris['feature_names'])
df['target'] = iris['target']
```
## Scatter plot per species
```{python}
for target in df['target'].unique():
fig = px.scatter(df, x='petal length (cm)', y='sepal length (cm)')
fig.show()
```
Answers:
You can do this very easily just by generating markdown slide header dynamically in each iteration of loop using display(Markdown("Slide header"))
along with chunk option output: asis
.
---
title: "For Loops in Quarto"
format:
revealjs:
theme: default
code-fold: true
execute:
echo: false
---
```{python}
#| include: false
from IPython.display import display, Markdown
from sklearn import datasets
import pandas as pd
import plotly.express as px
```
```{python}
#| include: false
# Get data
iris = datasets.load_iris()
df = pd.DataFrame(iris['data'], columns=iris['feature_names'])
df['target'] = iris['target']
```
```{python}
#| output: asis
for target in df['target'].unique():
display(Markdown("## Scatter plot per species"))
fig = px.scatter(df, x='petal length (cm)', y='sepal length (cm)')
fig.show()
```
I’m trying to generate a slide deck consisting of several plots using Quarto with revealjs output format. I need to generate these plots with plotly through a loop, but that is messing up my output. I’m getting the plots lined up vertically, which doesn’t fit the slide size. What I want to achieve is one plot per slide. Is that possible?
I havn’t been able to find any solution so far. Here’s some code to replicate the problem. Note that I only used Iris to get an example similar to mine, I know the for loop doesn’t make much sense in this case.
---
title: "Foor Loops in Quarto"
format:
revealjs:
theme: default
jupyter: ds
code-fold: true
execute:
echo: false
freeze: auto
---
```{python}
#| include: false
# Imports
from sklearn import datasets
import pandas as pd
import plotly.express as px
```
```{python}
#| include: false
# Get data
iris = datasets.load_iris()
df = pd.DataFrame(iris['data'], columns=iris['feature_names'])
df['target'] = iris['target']
```
## Scatter plot per species
```{python}
for target in df['target'].unique():
fig = px.scatter(df, x='petal length (cm)', y='sepal length (cm)')
fig.show()
```
You can do this very easily just by generating markdown slide header dynamically in each iteration of loop using display(Markdown("Slide header"))
along with chunk option output: asis
.
---
title: "For Loops in Quarto"
format:
revealjs:
theme: default
code-fold: true
execute:
echo: false
---
```{python}
#| include: false
from IPython.display import display, Markdown
from sklearn import datasets
import pandas as pd
import plotly.express as px
```
```{python}
#| include: false
# Get data
iris = datasets.load_iris()
df = pd.DataFrame(iris['data'], columns=iris['feature_names'])
df['target'] = iris['target']
```
```{python}
#| output: asis
for target in df['target'].unique():
display(Markdown("## Scatter plot per species"))
fig = px.scatter(df, x='petal length (cm)', y='sepal length (cm)')
fig.show()
```