Download notebook (.ipynb)

Export Plot to SVG, PDF, HTML, PNG#

import io

import numpy as np
import pandas as pd

from lets_plot import *
LetsPlot.setup_html()
df = pd.read_csv("https://raw.githubusercontent.com/JetBrains/lets-plot-docs/master/data/iris.csv")
print(df.shape)
df.head()
(150, 5)
sepal_length sepal_width petal_length petal_width species
0 5.1 3.5 1.4 0.2 setosa
1 4.9 3.0 1.4 0.2 setosa
2 4.7 3.2 1.3 0.2 setosa
3 4.6 3.1 1.5 0.2 setosa
4 5.0 3.6 1.4 0.2 setosa
p = ggplot(df) + geom_point(aes("petal_length", "petal_width", color="species"), size=5) + ggsize(600, 400)
p

Export Using ggsave()#

The ggsave() function is an easy way to export plot to a file in SVG, PDF, HTML or PNG formats.

Export SVG to File#

fullpath_svg = ggsave(p, "plot.svg")

Load and display saved SVG.

from IPython.display import SVG

SVG(filename=fullpath_svg)
../../_images/6567e568fec1492d18286ef17d45bee8de6c696093357d6fbd10b1e5b3701c05.svg

Export PDF to File#

fullpath_pdf = ggsave(p, "plot.pdf")

This is what the PDF file looks like:

import base64
import urllib.request
from IPython.display import HTML

data = urllib.request.urlopen("https://github.com/JetBrains/lets-plot-docs/raw/master/source/examples/cookbook/lets-plot-images/plot.pdf").read()
b64 = base64.b64encode(data).decode("ascii")

HTML(f'<embed src="data:application/pdf;base64,{b64}" type="application/pdf" width="600" height="400">')

Export HTML to File#

By default, when exporting HTML, ggsave wraps the HTML of the plot in an iframe that matches the size of the plot.

fullpath_html = ggsave(p, "plot.html")

Load and display saved HTML.

HTML(filename=fullpath_html)

Use the iframe=False option to only export the HTML of the plot, without adding an iframe.

fullpath_no_iframe = ggsave(p, "no_iframe_plot.html", iframe=False)
HTML(filename=fullpath_no_iframe)

Export to PNG File#

To save plot as a PNG file use a filename that have “png” extension.

fullpath_png = ggsave(p, "plot.png")

Load and display saved PNG.

from IPython.display import Image

Image(filename=fullpath_png, width=600, height=400)

Export 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.

Saving to a File#

path = p.to_svg("lets-plot-images/to_svg_plot.svg")
SVG(path)
../../_images/8297c05b25457889918df098c3d283f1200a4d096c5f7948a7e63f9731ece039.svg

Wrighting to a Stream of In-Memory Bytes#

stream = io.BytesIO()

p.to_svg(stream)
SVG(stream.getvalue())
../../_images/138307c5ffef7392837b59c1f20620d6ae45d38bab6f864f89bb560685fca3b9.svg

Adjusting the Size and Aspect Ratio#

Parameter scale#

# default scale is 2.0
img_scale_default = ggsave(p, "scale_default_plot.png")
Image(filename=img_scale_default)
../../_images/2a040815eeb3cb20844f41813bafcc95d6cb7126549359df41607a792c04262c.png
# use custom scale
img_scale_1 = ggsave(p, "scale_1_plot.png", scale=1)
Image(filename=img_scale_1)
../../_images/1ba1dd8bf4d38639b3fe88f85695411b341b7e59cff5d5c028ed5683a2d25042.png

Parameters w, h, unit and dpi#

These parameters allow you to configure the export of your plot:

  • w and h: Define the width and height of the output image in the specified unit.

  • unit: Specifies the unit of measurement ('in', 'cm', 'mm' or 'px').

  • dpi: Sets the resolution in dots per inch.

This is how they work for PNG and PDF formats:

  • If w, h, unit and dpi are all specified:

    • The plot’s pixel size (default or set by ggsize()) is ignored.

    • The output size is calculated using the specified w, h, unit and dpi.

      • The plot is resized to fit the specified w x h area, which may affect the layout, tick labels and other elements.

  • If only dpi is specified:

    • The plot’s pixel size (default or set by ggsize()) is converted to inches using the standard display PPI of 96.

    • The output size is then calculated based on the specified DPI.

      • The plot maintains its aspect ratio, preserving layout, tick labels and other visual elements.

      • Useful for printing - the plot will appear nearly the same size as on screen.

  • If w, h are not specified:

    • The scale parameter is used to determine the output size.

      • The plot maintains its aspect ratio, preserving layout, tick labels and other visual elements.

      • Useful for generating high-resolution images suitable for publication.

For SVG format:

  • If w, h and unit are specified:

    • The plot’s pixel size (default or set by ggsize()) is ignored.

    • The output size is calculated using the specified w, h and unit.

Parameters w and h

The w and h parameters override plot size, allowing to specify the output image size independently of the plot size.

img_square = ggsave(p, "square_plot.png", w=4, h=4, unit='in', dpi=100)
Image(filename=img_square)
../../_images/dcb556e171b39c103cfd0fdf7e34a8985231ea48f31c11c370100b49baf921bd.png

72 dpi

img_72 = ggsave(p, "72dpi_plot.png", w=6, h=4, unit='in', dpi=72)
Image(filename=img_72, width=900, height=600)

300 dpi

img_300 = ggsave(p, "300dpi_plot.png", w=6, h=4, unit='in', dpi=300)
Image(filename=img_300, width=900, height=600)