Rnotebook中用python画图

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

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

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

def m_show(p):
    import io
    img = io.StringIO()
    p.savefig(img, format='svg')
    img.seek(0)
    print("<div style='width: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)

```

posted @ 2016-11-11 11:03  bregman  阅读(559)  评论(0编辑  收藏  举报