Python 从rmarkdown中的r块调用matplotlib时未显示绘图

Python 从rmarkdown中的r块调用matplotlib时未显示绘图,python,matplotlib,r-markdown,Python,Matplotlib,R Markdown,我使用以下函数使用python绘制一些矩阵: import os import matplotlib.pyplot as plt import numpy as np def plot_freq_bin_mats(subject, freq_bins): fig = plt.figure(figsize=(8,8)) for i, f in enumerate(freq_bins, start=1): subj_file = base_dir + subject

我使用以下函数使用python绘制一些矩阵:

import os
import matplotlib.pyplot as plt
import numpy as np

def plot_freq_bin_mats(subject, freq_bins):
    fig = plt.figure(figsize=(8,8))
    for i, f in enumerate(freq_bins, start=1):
        subj_file = base_dir + subject + '_' + f + '.txt'
        print(subj_file)
        if os.path.exists(subj_file):
            freq_mat = np.loadtxt(subj_file, delimiter=',')
        else:
            freq_mat = np.zeros((46,46))
        ax = fig.add_subplot(3,4,i)
        ax.set_title("{0}".format(f))
        ax.spy(freq_mat, markersize=1, color='black')
        ax.set_yticks([])
        ax.set_xticks([])
    fig.tight_layout()
    fig.suptitle("{0}".format(subject), fontsize=14, y=1)
    plt.show()
我试图从Rmarkdown文档中的R块中使用此代码,以便利用
par(mfrow)
从for循环创建网格布局。但是,当我从R块运行python源代码时,没有显示任何绘图。我没有收到任何错误消息。以下是我尝试使用的代码:

source_python('/path/to/script.py')
plot_freq_bin_mats(py$all_subjects[1], py$freq_bins)
这是我的Rmarkdown设置:

knitr::opts_chunk$set(echo = TRUE)
knitr::knit_engines$set(python=reticulate::eng_python)
library(reticulate)
use_python('/opt/anaconda3/bin/python')
matplotlib <- import("matplotlib")
matplotlib$use("Agg", force = TRUE)
knitr::opts_chunk$set(echo=TRUE)
knitr::knit_引擎$set(python=networkite::eng_python)
图书馆(网状)
使用python('/opt/anaconda3/bin/python')
matplotlib