Skip to content

Instantly share code, notes, and snippets.

@Kornel
Created April 10, 2017 17:26
Show Gist options
  • Save Kornel/780e2ead4bc8674e7f21fb5c6af5f329 to your computer and use it in GitHub Desktop.
Save Kornel/780e2ead4bc8674e7f21fb5c6af5f329 to your computer and use it in GitHub Desktop.
Inline barplot for jupyter
from tempfile import NamedTemporaryFile
import base64
def inline_hist(data, figsize=(4, 4), **kwags):
data = list(data)
fig, ax = plt.subplots(1, 1, figsize=figsize, **kwags)
for k,v in ax.spines.items():
v.set_visible(False)
ax.set_xticks([])
ax.set_yticks([])
plt.bar(np.arange(len(data)), data)
tmp = NamedTemporaryFile()
plt.savefig(tmp)
tmp.seek(0)
plt.close()
base64 = base64.b64encode(tmp.read()).decode()
return '<img src="data:image/png;base64,{}"/>'.format(base64)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment