在带有 matplotlib 的 rstudio 中使用带有网状包的针织机的问题

Problem using knitter with reticulate package in rstudio with matplotlib

要详细说明标题,

我正在使用 rstudio 制作一个包含 r 和 python 代码的 rmarkdown 文件。我的配置如下:

Running under: Windows 10 x64 (build 18363)

Matrix products: default

locale:
[1] LC_COLLATE=English_United Kingdom.1252  LC_CTYPE=English_United Kingdom.1252    LC_MONETARY=English_United Kingdom.1252
[4] LC_NUMERIC=C                            LC_TIME=English_United Kingdom.1252    

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

loaded via a namespace (and not attached):
 [1] Rcpp_1.0.4.6         rstudioapi_0.11      knitr_1.28           magrittr_1.5         rappdirs_0.3.1       tidyselect_1.0.0    
 [7] munsell_0.5.0        lattice_0.20-38      colorspace_1.4-1     R6_2.4.1             rlang_0.4.5          dplyr_0.8.5         
[13] tools_3.6.3          grid_3.6.3           gtable_0.3.0         xfun_0.12            htmltools_0.4.0      yaml_2.2.1          
[19] assertthat_0.2.1     digest_0.6.25        tibble_2.1.3         lifecycle_0.2.0      crayon_1.3.4         Matrix_1.2-18       
[25] purrr_0.3.3          ggplot2_3.3.0        rsconnect_0.8.16     glue_1.3.2           evaluate_0.14        rmarkdown_2.1       
[31] compiler_3.6.3       pillar_1.4.3         scales_1.1.0         jsonlite_1.6.1       reticulate_1.15-9000 pkgconfig_2.0.3 

我尝试 运行ning 的 python 块是:

import scipy as sp
import numpy as np
import pandas as pd


df = pd.read_csv("D:/03 PhD Edinburgh related/OneDrive/OneDrive - University of Edinburgh/00 PhD/000 PhD Data/01 Project I Chr Hansen/20200421_analysis_pp16013/20200420_pp16013_analysis.csv")

plt.scatter(df['OD'].values, df['osmolarity.mOSM'].values,
            c=df['hydrophobicity'].values,cmap='magma')
plt.xlabel('OD')
plt.ylabel('osmolarity')
plt.title('osmolarity as function of OD and Hydrophobicity')

cbar = plt.colorbar()
cbar.set_label('Hydrophobicity', rotation=270)

plt.show()

我可以在 rStudio 中 运行 python 块并按预期显示图形。

当我尝试使用 knitter 制作文档时,它到达 python 块并给出错误:

"This application failed to start because it could not find or load 
the Qt platform plugin "windows" in "", 
reinstalling the application may fix this problem."

这与使用matplotlib的部分具体相关。我可以创建并显示 Pandas 数据框。

我已尝试安装 github 的最新版 reticulate。

提前感谢您的帮助。

在我的rmarkdown文件中,设置好使用的conda环境的插件路径后就可以正常使用了。您可以在将 matplotlib 用于 python 代码块之前添加以下 r 代码块。

library(reticulate)
py_run_string("import os as os")
py_run_string("os.environ['QT_QPA_PLATFORM_PLUGIN_PATH'] = 'C:/Users/[UserID]/Anaconda3/envs/[EnvironmentName]/Library/plugins/platforms'")

我在另一个堆栈溢出答案中找到了这个问题,这是重复的。

答案请看这里

我基本上只是保存我的 python 图,然后在 r 中加载保存的 python 图。