Save/Export Canvas¶
Toytree drawings can be saved to disk using the render
functions of toyplot. You can save toyplot figures in a variety of formats, including HTML, SVG, PDF, and PNG.
import toytree
import toyplot
To save a tree drawing you must store the Canvas
object to a named variable.
# Draw an example tree and save the Canvas, axes, and mark
rtre = toytree.rtree.unittree(ntips = 8)
canvas, axes, mark = rtre.draw();
toytree.save¶
The method toytree.save
provides the easiest way to save a canvas. Just provide the Canvas and output path name, and it will save it in the requested format based on the suffix at the end of the output path.
toytree.save(canvas, "/tmp/drawing.html")
toytree.save(canvas, "/tmp/drawing.svg")
toytree.save(canvas, "/tmp/drawing.pdf")
toytree.save(canvas, "/tmp/drawing.png")
toyplot.render¶
Under the hood, the toytree.save
function calls the toyplot.render
function. If you want a more verbose option you can use this approach intead, which requires importing the render function.
# the default option
toyplot.html.render(canvas, "tmp/tree-plot.html")
# to save to SVG
import toyplot.svg
toyplot.svg.render(canvas, "tmp/tree-plot.svg")
# to save to PDF
import toyplot.pdf
toyplot.pdf.render(canvas, "tmp/tree-plot.pdf")
# to save to PNG
import toyplot.png
toyplot.png.render(canvas, "tmp/tree-plot.png")
Formats¶
HTML¶
HTML rendering is the default format. This will save the figure as a vector graphic (SVG) wrapped in HTML with additional optional javascript wrapping for interactive features. This is the only format that will retain the interactive hover features. It can be opened in a browser, and can be convenient for viewing very large trees.
SVG¶
SVG is a vector format that is best for saving and creating publication quality figures. You can further edit the figure in Illustrator or InkScape (recommended).
PDF¶
The SVG figure can be embedded in a PDF for easier sharing with people who do not know how to view an SVG file.
PNG¶
PNG is a raster format. Instead of saving the instructions for the drawing it reduces it to pixels. This can be convenient for reducing the size of a drawing, especially if it contains tons of data (millions of points).