save/export canvas
Saving Figures¶
Toytree drawings can be saved to disk using the render
functions of toyplot. This is where it is useful to store the Canvas object as a variable when it is returned during a toytree drawing. You can save toyplot figures in a variety of formats, including HTML (which is actually an SVG figures wrapped in HTML with addition javascript to provide interactivity); or SVG, PDF, and PNG.
import toytree
import toyplot
rtre = toytree.rtree.unittree(ntips = 8)
# draw a plot and store the Canvas object to a variable
canvas, axes, mark = rtre.draw(width=400, height=300);
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. You can share the file with others and anyone can open it in a browser. You can embed it on your website, or even display it in emails!
import os
os.getcwd()
'c:\\Users\\natet\\Desktop\\eatonlab\\toytree_NT\\docs'
# for sharing through web-links (or even email!) html is great!
toyplot.html.render(canvas, "tmp/tree-plot.html")
Optional formats: If you want to do additional styling of your figures in Illustrator or InkScape (recommended) then SVG is likely your best option. You can save figures in SVG by simply importing this as an additional option from toyplot.
# for creating scientific figures SVG is often the most useful format
import toyplot.svg
toyplot.svg.render(canvas, "tmp/tree-plot.svg")
Despite the advantages of working with the SVG or HTML formats (e.g., vector graphics and interactive pop-ups), if you're like me you still sometimes love to have an old-fashioned PDF. Again, you can import this from toyplot.
import toyplot.pdf
toyplot.pdf.render(canvas, "tmp/tree-plot.pdf")
Finally, PNG provides the most concise format for storing plots with many many many points in them, since it is a raster format, but has significant loss compared to the other formats above.
import toyplot.png
toyplot.png.render(canvas, "tmp/tree-plot.png")