Created
April 10, 2017 17:26
-
-
Save Kornel/780e2ead4bc8674e7f21fb5c6af5f329 to your computer and use it in GitHub Desktop.
Inline barplot for jupyter
This file contains hidden or 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
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