Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

how to set width and height of matplotlib images in python Rmd chunks? #1140

Closed
tdhock opened this issue Jan 20, 2022 · 6 comments
Closed

how to set width and height of matplotlib images in python Rmd chunks? #1140

tdhock opened this issue Jan 20, 2022 · 6 comments

Comments

@tdhock
Copy link

tdhock commented Jan 20, 2022

hi thanks very much for this package which is very useful for including python chunks in Rmd documents.
Is there a way to control the height and width of the images produced in the chunks? I would like to make Rmd beamer slides with images from matplotlib which have reduced height, but full slide width (so you can write a few lines of commentary above/below the figure).
The fig.width and fig.height have no effect on the image size, and that is documented as R-specific: https://bookdown.org/yihui/rmarkdown-cookbook/figure-size.html says The size of plots made in R can be controlled by the chunk option fig.width and fig.height (in inches).
Is there another way to control matplotlib figure height and width?
The out.height='25%' chunk option works for proportionally reducing the image size (both height and width), but I would like to reduce the height but maintain full width. is that possible?
Thanks and here is a zip file with MRE as Rmd source code with rendered PDF.
slides.zip

@tdhock
Copy link
Author

tdhock commented Jan 21, 2022

Also would it be possible to output PNG instead of PDF? Slides with PDF/vector graphics take up a lot of disk space (>10MB) so it would be nice to be able to switch to PNG graphics to reduce slide file size.
Looking at the source code I see an option (reticulate.engine.matplotlib.show) which defaults to a function (eng_python_matplotlib_show) that has plt$savefig() so should I write a custom function here? Is this hook documented anywhere? I did not find any documentation from a web search for "r option reticulate.engine.matplotlib.show"

    hook <- getOption("reticulate.engine.matplotlib.show", eng_python_matplotlib_show)

@tdhock tdhock changed the title how to set width and height of matplotlib images? how to set width and height of matplotlib images in python Rmd chunks? Jan 21, 2022
@jwalton3141
Copy link

jwalton3141 commented Jan 21, 2022

Is there another way to control matplotlib figure height and width?

You could use the figsize argument of matplotlib's plt.figure() command.

So where you have:

```{python}
plt.plot([1, 2, 3, 4]) 
plt.ylabel('some numbers')
plt.show()
```

replace with, eg.

```{python}
plt.figure(figsize=(6, 2))
plt.plot([1, 2, 3, 4])
plt.ylabel('some numbers')
plt.show()
```

@jwalton3141
Copy link

jwalton3141 commented Jan 21, 2022

would it be possible to output PNG instead of PDF?

Yes. You can do so using the dev figure option of Rmarkdown, as such:

title: "matplotlib test"
author: "Toby Dylan Hocking"
output: 
  beamer_presentation:
    dev: png 

@tdhock
Copy link
Author

tdhock commented Jan 21, 2022

Great, thanks for the advice. Going forward for other people who have these kinds of questions, can these tips please be documented in some obvious place? For example,

For the record I was trying to get this to work with plotnine so the final code I used looks like this,

  • dev: png in header
  • knitr::opts_chunk$set(dpi=500) in R chunk
  • plotnine.options.figure_size = (5, 3) in python chunk to set default fig size, can be overridden for specific figures via p9.theme(figure_size=(4.5,2.5))
  • p9.theme(subplots_adjust={"left":0.15, "right":0.75, "bottom":0.2, "top":1}) to adjust borders so axis label and legend do not get cut off.

@t-kalinowski
Copy link
Member

t-kalinowski commented Jul 7, 2023

The standard knitr chunk options fig.width and fig.height should now work:

```{python}
#| fig.width: 8
#| fig.height: 2
import matplotlib.pyplot as plt
plt.plot([1,2,3,4], [1,4,9,16])
```

@tdhock
Copy link
Author

tdhock commented Oct 24, 2023

Thanks! I confirm this works with these recent R/package versions

R Under development (unstable) (2023-08-23 r85001 ucrt)
Platform: x86_64-w64-mingw32/x64
Running under: Windows 10 x64 (build 19045)

Matrix products: default


locale:
[1] LC_COLLATE=English_United States.utf8 
[2] LC_CTYPE=English_United States.utf8   
[3] LC_MONETARY=English_United States.utf8
[4] LC_NUMERIC=C                          
[5] LC_TIME=English_United States.utf8    

time zone: America/Phoenix
tzcode source: internal

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

loaded via a namespace (and not attached):
 [1] digest_0.6.33     fastmap_1.1.1     xfun_0.40         Matrix_1.6-1     
 [5] lattice_0.21-8    reticulate_1.34.0 knitr_1.43        htmltools_0.5.6  
 [9] png_0.1-8         rmarkdown_2.24    tinytex_0.46      cli_3.6.1        
[13] grid_4.4.0        withr_2.5.0       compiler_4.4.0    highr_0.10       
[17] rstudioapi_0.15.0 tools_4.4.0       evaluate_0.21     Rcpp_1.0.11      
[21] yaml_2.3.7        jsonlite_1.8.7    rlang_1.1.1      

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants