'Save ggplot to a base64 encoded string.
Parameters
filename : string
filepath to save to
as_tag: bool
if true, returns an <img /> tag with the image uri as the src attribute
width: int, float
width of the plot in inches
height: int, float
height of the plot in inches'

def save_as_base64(self, as_tag=False, width=None, height=None, dpi=180):

    imgdata = six.StringIO()
    self.save(imgdata, width=width, height=height, dpi=dpi)
    imgdata.seek(0)
    uri = (u'data:image/png;base64,' + urllib.quote(base64.b64encode(imgdata.buf)))
    if (as_tag == True):
        return (u'<img    src    =    "%s"/>' % uri)
    else:
        return uri
