如何在jupyter/ipython笔记本中的图旁边显示文本段落
问题描述:
我正在寻找一种(可能有创意的)方式将文本放置在jupyter笔记本中的图形旁边。这个想法是对图表进行详细描述,而不是笔记本的通常垂直流动。如何在jupyter/ipython笔记本中的图旁边显示文本段落
任何想法?
答
一个颇具创意的方式是模仿内联后端,但添加一个基础表。为Python 2.7可能的解决方案可能看起来像
from io import BytesIO
import matplotlib.pyplot as plt
from IPython.display import display, Image, HTML
import base64
def plotdesc(fig, text, iwidth=None):
bio = BytesIO()
# save fig as png to bytes IO instead to disk
fig.savefig(bio, format='png')
plt.close(fig)
iwidth = ' width={0} '.format(iwidth) if iwidth is not None else ''
img_tag = "<img src='data:image/png;base64," + base64.b64encode(bio.getvalue()) + "'{0}/>".format(iwidth)
datatable = '<table><tr><td>{0}</td><td>{1}</td></tr></table>'.format(img_tag, text)
display(HTML(datatable))
的情况下使用,如:
fig, ax = plt.subplots(1,1, figsize=(6,4))
ax.plot([1,2,3])
text = '<h4>Description of the chart:</h4><BR>asdfsa fasdf qwer fsdaf er qw asdcdsafqwer dacfas dfqwetr cvxy fsa'
plotdesc(fig, text, iwidth='500px')
如果现在设置表的CSS例如删除边框
%%html
<style>
table,td,tr,th {border:none!important}
</style>
的曲线图。当然该溶液可进一步增强,可以使用固定的列宽度等 IIRC的base64编码用蟒3.x的略微不同的