Created
April 16, 2018 19:29
-
-
Save ruxi/0f414f229c0052522bd0fad0e95d0d49 to your computer and use it in GitHub Desktop.
source: https://stackoverflow.com/questions/21754976/ import numpy as np
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# source: https://stackoverflow.com/questions/21754976/ | |
import numpy as np | |
from IPython.display import HTML | |
import io | |
import base64 | |
class FlowLayout(object): | |
''' A class / object to display plots in a horizontal / flow layout below a cell ''' | |
def __init__(self): | |
# string buffer for the HTML: initially some CSS; images to be appended | |
self.sHtml = """ | |
<style> | |
.floating-box { | |
display: inline-block; | |
margin: 10px; | |
border: 3px solid #888888; | |
} | |
</style> | |
""" | |
def add_plot(self, oAxes): | |
''' Saves a PNG representation of a Matplotlib Axes object ''' | |
Bio=io.BytesIO() # bytes buffer for the plot | |
fig = oAxes.get_figure() | |
fig.canvas.print_png(Bio) # make a png of the plot in the buffer | |
# encode the bytes as string using base 64 | |
sB64Img = base64.b64encode(Bio.getvalue()).decode() | |
self.sHtml+= ( | |
'<div class="floating-box">'+ | |
'<img src="data:image/png;base64,{}\n">'.format(sB64Img)+ | |
'</div>') | |
def PassHtmlToCell(self): | |
''' Final step - display the accumulated HTML ''' | |
display(HTML(self.sHtml)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment