Rnotebook中用python画图

  • 如果notebook需要转化为pdf, 能想到办法是保存图片文件,嵌入mardown语法中。
  • 但是如果在html中显示, 可以考虑下面思虑, 比较取巧。

``` {python, engine.path="/opt/anaconda3/bin/python", results='asis'}

import matplotlib
matplotlib.use('Agg')
import io

def m_show(p):
    img = io.StringIO()
    p.savefig(img, format='svg')
    img.seek(0)
    print("<div style='60px'>" + img.getvalue() + "</div>")
    
""" Prepare your plot here ... """
from pylab import *
plt.plot(np.arange(10))
# Use the custom show function instead of plt.show()
m_show(plt)

```

原文地址:https://www.cnblogs.com/bregman/p/6053506.html