2017-06-24 9 views
0

セル{0}がmatplotlibプロットでセル{1}が1X 2 HTMLテーブルを作成する方法に関するアイデアは、Python 3.Xのテキスト記述ですか?HTMLテーブルでは、pythonを使ってjupyterノートブックでplotの横にテキストを追加する方法は?

テストケースの設定
import matplotlib.pyplot as plt 
from io import BytesIO 
%matplotlib inline 

def add_split_screen(fig, text, iwidth=None): 

    figdata = BytesIO() 
    fig.savefig(figdata, format='png') 
    figdata.seek(0) 
    figdata 
    figdata.close() 
    iwidth = ' width={0} '.format(iwidth) if iwidth is not None else '' 
    datatable = '<table><tr><td>{0}</td><td>{1}</td></tr></table>'.format(figdata, text) 
    display(HTML(datatable)) 

:次にJupyterノートブック内の関数を実行している

fig, ax = plt.subplots(1,1, figsize=(6,4)) 
ax.plot([1,2,3]) 
text = '<h4>Special Chart:</h4><BR>Description of chart will go here.' 

を:

add_split_screen(fig, text, iwidth='500px') 

次のように私の出力は次のようになります。 enter image description here

しかし、私は実際にJupyterノートブックをインラインで見ることに興味があります。

答えて

1

あなたはdocumentを読んでおり、正しいトラックにBytesIOを使用しているようです。それには2つの以上のステップは、単に以下のとおりです。

  1. あなたは
  2. のBase64を使用してfigdataをエンコードをプロットのための適切なタグを使用してください。そして、ここでstr

にそれらをデコード(Jupyterノートで)完全かつ検証可能な例では、あなたのコードから変更されます。

from base64 import b64encode 
from io import BytesIO 

from IPython.display import display, HTML 
import matplotlib.pyplot as plt 

def add_split_screen(fig, text, iwidth=None): 
    figdata = BytesIO() 
    fig.savefig(figdata, format='png') 
    iwidth = ' width={0} '.format(iwidth) if iwidth is not None else '' 
    datatable = '<table><tr><td><img src="data:image/png;base64,{0}"/></td><td>{1}</td></tr></table>'.format(b64encode(figdata.getvalue()).decode(), text) 
    display(HTML(datatable)) 

fig, ax = plt.subplots(1,1, figsize=(6,4)) 
ax.plot([1,2,3]) 
text = '<h4>Special Chart:</h4><BR>Description of chart will go here.' 
add_split_screen(fig, text, iwidth='500px') 

enter image description here

関連する問題