#!/usr/bin/env python
# coding: utf-8
# # Convenience Methods for Exporting Plot to a File or File-Like Object
#
# You can export figure created by `ggplot()` or `gggrid()` functions
# to a vector or raster format using the following methods:
# - `to_svg(path)`
# - `to_html(path, iframe)`
# - `to_png(path, scale)`
# - `to_pdf(path, scale)`
#
# To save plot to a file on disc, specify the file' pathname in `path`.
# To stream plot image to a file-like object, supply such object in the `path` parameter instead of a pathname.
#
# In[1]:
import numpy as np
import io
from IPython import display
from lets_plot import *
# In[2]:
LetsPlot.setup_html()
# In[3]:
data = {'x': np.random.normal(size=100)}
p = ggplot(data, aes(x='x')) + geom_histogram()
# #### 1. Saving to a File
#
# In[4]:
path = p.to_svg('lets-plot-images/hist.svg')
# In[5]:
display.SVG(path)
# #### 2. Wrighting to a Stream of In-Memory Bytes
#
# In[6]:
stream = io.BytesIO()
p.to_svg(stream)
# In[7]:
display.SVG(stream.getvalue())