JuliaInterop / JuliaCall

Embed Julia in R
https://non-contradiction.github.io/JuliaCall/index.html
Other
267 stars 36 forks source link

Saving PyPlot figure in julia code chunk in rBookdown #121

Open likefrankie opened 5 years ago

likefrankie commented 5 years ago

I can run the following code in Julia 1.2.0 console and it works fine - that is, it saves the figure as expected.

using PyPlot clf(); ioff(); x1 = collect(0:0.1:2) x2a = 1 .- 0.5x1 x2b = ifelse.(2 .- 2x1.>=0, 2 .- 2*x1,0)

fig, ax = subplots();

ax.fillbetween(x1,x2a,color="red",linewidth=2,label=L"x{2} \leq 1 - \frac{1}{2}x_{1}",alpha=0.3); ax.legend(loc="upper right"); ax.fillbetween(x1,x2b,color="blue",linewidth=2,label=L"x{2} \leq 2 - 2x_{1}",alpha=0.3); ax.legend(loc="upper right"); ax.setxlabel(L"x{1}") ax.setylabel(L"x{2}") savefig("example-01a.png");

But when I run the same code inside a code chunk in rBookdown with Julia as the engine, the saved chart is blank, only showing the x-axis and y-axis.

using PyPlot
clf();
ioff();
x1 = collect(0:0.1:2)
x2a = 1 .- 0.5*x1
x2b = ifelse.(2 .- 2*x1.>=0, 2 .- 2*x1,0)

fig, ax = subplots();

ax.fill_between(x1,x2a,color="red",linewidth=2,label=L"x_{2} \leq 1 - \frac{1}{2}x_{1}",alpha=0.3);
ax.legend(loc="upper right");
ax.fill_between(x1,x2b,color="blue",linewidth=2,label=L"x_{2} \leq 2 - 2x_{1}",alpha=0.3);
ax.legend(loc="upper right");
ax.set_xlabel(L"x_{1}")
ax.set_ylabel(L"x_{2}")
savefig("example-01a.png");

My apologies if this is a trivial issue, and would appreciate any suggestions to address it.

Non-Contradiction commented 5 years ago

Hi, thanks for the feedback! The code works fine for me in both rmarkdown and rbookdown chunks. I highly suspect the issue is involved with the setting of pyplot (matplotlib). What is your sessionInfo() from R? Can you run the code directly in Julia in batch mode (not interactively) and see if the plot is normal?

likefrankie commented 5 years ago

Thank you for your response. I ran in linux (instead of in windows) and was able to get the charts to work. Please find below my sessionInfo(). I also ran in batch mode and the plots were normal.

I am also using Python 3.6.8 and matlibplot 3.1.1

sessionInfo() R version 3.6.1 (2019-07-05) Platform: x86_64-pc-linux-gnu (64-bit) Running under: Ubuntu 18.04.3 LTS

Matrix products: default BLAS: /usr/lib/x86_64-linux-gnu/blas/libblas.so.3.7.1 LAPACK: /usr/lib/x86_64-linux-gnu/lapack/liblapack.so.3.7.1

locale: [1] LC_CTYPE=en_US.UTF-8 LC_NUMERIC=C
[3] LC_TIME=en_US.UTF-8 LC_COLLATE=en_US.UTF-8
[5] LC_MONETARY=en_US.UTF-8 LC_MESSAGES=en_US.UTF-8
[7] LC_PAPER=en_US.UTF-8 LC_NAME=en_US.UTF-8
[9] LC_ADDRESS=en_US.UTF-8 LC_TELEPHONE=en_US.UTF-8
[11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=en_US.UTF-8

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

other attached packages: [1] latex2exp_0.4.0 diagram_1.6.4 shape_1.4.4 JuliaCall_0.17.0 [5] kableExtra_1.1.0 xtable_1.8-4 bookdown_0.14 knitr_1.25

loaded via a namespace (and not attached): [1] Rcpp_1.0.2 highr_0.8 pillar_1.4.2 compiler_3.6.1
[5] tools_3.6.1 zeallot_0.1.0 digest_0.6.22 evaluate_0.14
[9] tibble_2.1.3 viridisLite_0.3.0 pkgconfig_2.0.3 rlang_0.4.1
[13] rstudioapi_0.10 yaml_2.2.0 xfun_0.10 httr_1.4.1
[17] stringr_1.4.0 xml2_1.2.2 vctrs_0.2.0 hms_0.5.2
[21] webshot_0.5.1 glue_1.3.1 R6_2.4.0 rmarkdown_1.16
[25] readr_1.3.1 magrittr_1.5 backports_1.1.5 scales_1.0.0
[29] htmltools_0.4.0 rvest_0.3.4 colorspace_1.4-1 stringi_1.4.3
[33] munsell_0.5.0 crayon_1.3.4