import os
import sys
import matplotlib
import matplotlib.pyplot as plt
import io
import urllib.request, urllib.parse, urllib.error, base64
plt.plot(list(range(10, 20)))
fig = plt.gcf()
imgdata = io.StringIO()
fig.savefig(imgdata, format='png')
imgdata.seek(0)
print('Content-type: image/png\n')
uri = 'data:image/png;base64,' + urllib.parse.quote(base64.b64encode(imgdata.buf))
print('<img src = "%s"/>' % uri)
matplotlib - store image in variable