Search code examples
pythonhtmljupyterquarto

how to control size of images in quarto html report


I have tried to set image size using the chunk options fig.height and fig-height but the image sizes are unaffected.

---
title: 'image size'
author: 'Joseph Powers'
date: 2024-05-07
format:
    html:
        toc: true
        toc-depth: 3
        html-math-method: webtex
editor: source
fig.align: 'center'
dpi: 300
include: TRUE
echo: FALSE
message: FALSE
warning: FALSE
error: FALSE
cache: FALSE
code-fold: true
df-print: kable
---

```{python}
import pandas as pd
import numpy as np
from plotnine import *
from mizani.formatters import percent_format
from scipy import stats

my_blue = '#0177c9'
my_red = '#bd0707'

x = np.random.normal(.05, .025, 4000)

# Estimate pdf using kde
pdf = stats.gaussian_kde(x)

# Evaluate pdf at a set of points
points = np.linspace(min(x), max(x), 1000)
estimated_density = pdf.evaluate(points)

df_dens = pd.DataFrame({
    'x': points,
    'y': estimated_density
})

p = (ggplot(df_dens, aes('x', 'y')) +
 geom_line() +
 geom_area(data = df_dens[df_dens['x'] >= 0], fill = my_blue) +
 geom_area(data = df_dens[df_dens['x'] <= 0], fill = my_red) +
 theme_bw() +
 theme(panel_grid=element_blank(), axis_text_y=element_blank(), axis_ticks_y=element_blank()) +
 labs(y='', x="\nPlausible Treatment Effects") +
 scale_x_continuous(
    breaks=np.arange(-1, 1.01, 0.01),
    labels=percent_format()
    ) 
)

p
```

```{python}
#| out-width: '20%'
#| out-height: '20%'
p
```

```{python}
#| fig-height: 3
#| fig-width: 10
p
```

```{python, fig.height=1, fig.width=1}
#| fig-height: 1
#| fig-width: 1
p
```

enter image description here


Solution

  • If we look at the figure chunk option list for python cells from Quarto doc, there are no such fig-height, fig-width options listed. And also from previous encounter, I can assume that fig-height, fig-width options do not work as intended for python cells.

    Instead, I would suggest using either plotnine theme option theme(figure_size = (a, b)) for a specific plot or the default option plotnine.options.figure_size = (a, b) for all plots.

    ---
    title: 'image size'
    author: 'Joseph Powers'
    date: 2024-05-07
    format:
        html:
            toc: true
            toc-depth: 3
            html-math-method: webtex
    editor: source
    fig.align: 'center'
    dpi: 300
    include: TRUE
    echo: FALSE
    message: FALSE
    warning: FALSE
    error: FALSE
    cache: FALSE
    code-fold: true
    df-print: kable
    ---
    
    ```{python}
    import pandas as pd
    import numpy as np
    from plotnine import *
    from scipy import stats
    
    my_blue = '#0177c9'
    my_red = '#bd0707'
    
    x = np.random.normal(.05, .025, 4000)
    
    # Estimate pdf using kde
    pdf = stats.gaussian_kde(x)
    
    # Evaluate pdf at a set of points
    points = np.linspace(min(x), max(x), 1000)
    estimated_density = pdf.evaluate(points)
    
    df_dens = pd.DataFrame({
        'x': points,
        'y': estimated_density
    })
    
    p = (ggplot(df_dens, aes('x', 'y')) +
     geom_line() +
     geom_area(data = df_dens[df_dens['x'] >= 0], fill = my_blue) +
     geom_area(data = df_dens[df_dens['x'] <= 0], fill = my_red) +
     theme_bw()
    )
    
    p + theme(figure_size=(2, 2)) # in inches
    ```
    

    plotnine graph with specified height or width