cls*_*udt 7 html python matplotlib ipython ipython-notebook
以下代码在IPython命令的结果单元格中输出呈现的HTML:
from IPython.core.display import HTML
def putHTML():
source = """
<h1>Yah, rendered HTML</h1>
<h2>Here is an image</h2>
<img src="http://lbkbd.in/wp-content/uploads/2014/10/HTML-Logo.png"/>
"""
return HTML(source)
Run Code Online (Sandbox Code Playgroud)
如何使用此方法在HTML布局中包含matplotlib生成的图形?
Phi*_*lip 12
由于IPython已经有一个为图像生成HTML输出的后端,因此您可以使用它们的内联后端:
from matplotlib._pylab_helpers import Gcf
from IPython.core.pylabtools import print_figure
from base64 import b64encode
# Plot something
plot([1,2],[3,4])
# Get a handle for the plot that was just generated
fig = Gcf.get_all_fig_managers()[-1].canvas.figure
# Generate a data URL for the image
# Matplotlib's display() would output the plot, so I do this manually.
# There might be a wrapper for this somewhere in IPython, if you're
# unhappy with this line..
image_data = "data:image/png;base64,%s" % b64encode(print_figure(fig)).decode("utf-8")
# Remove the plot from the list of plots for the current cell
Gcf.destroy_fig(fig)
# Now you can use the data URL in your HTML output
HTML("Foo Bar <br> <img src='%s'> <br> Baz" % image_data)
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
4665 次 |
最近记录: |