Skip to content

save

Mojo function 🡭

save

fn def save[C: ChartLike](plot: C, path: String, dpi: Float64 = 72, tight: Bool = False)

Render plot and write it to path in one call. The format comes from the path’s extension – .svg, .png, .bmp or .pdf – or from plot.chart_settings().theme.output_format when path has no extension at all; PNG/BMP both go through render() and differ only in the writer.

Any other extension raises and writes nothing (#755). A .jpg path used to fall back to the theme format like an absent one, so it got SVG markup in a file named .jpg and the call reported success.

plot is a plain borrow: save(scatter(x, y), path) compiles inline, with no need to bind the temporary to a variable first. Call render()/render_svg() directly to get the Canvas/SvgCanvas itself. save_layers()/save_facets() are the List[AnyChart] counterparts; the save(canvas: Canvas, path) overload below writes an already-rendered Canvas.

tight=True crops the output to the figure’s ink, trimming the whitespace a fixed size() reserves for a longer title or an absent legend (#372). It applies to every format, because the measurement is taken from the drawing rather than from pixels; see render_tight().

dpi applies to the raster formats and says how many pixels one inch of the figure gets. The figure’s own size is in points, 1/72 inch (Plot.size()), so the default of 72 is one pixel per point and every existing call renders exactly as before; 300 gives the same figure at print resolution, with the text and strokes the same fraction of the page rather than a third the size. The vector formats ignore it, having no pixels to count. A .pdf path writes a one-page document through render_pdf().

SVG output with a non-empty .labels(title=...) writes accessible markup automatically, via _svg_output_string()/ accessible_svg_string() with that title and _resolve_description()’s <desc> – the same markup write_accessible_svg() adds explicitly, for callers who don’t need a title that differs from the visible one. An untitled plot’s SVG is unaffected.

Parameters:

  • C (ChartLike)

Args:

  • plot (C)
  • path (String)
  • dpi (Float64)
  • tight (Bool)

Raises:

fn def save(figure: Figure, path: String, dpi: Float64 = 72, tight: Bool = False)

Render a composite Figure and write it to path; save(plot)’s counterpart (#697), with the same format rule and the same dpi and tight (see save_grid(), which this delegates to).

Args:

  • figure (Figure): The composite chart.
  • path (String): Where to write; the extension picks the format.
  • dpi (Float64): Pixels per inch for a raster export; ignored by vector.
  • tight (Bool): Crop every format to the figure’s ink.

Raises:

Error: Whatever save_grid() raises.

fn def save(svg: SvgCanvas, path: String)

Write an already-rendered SvgCanvas to path (#620).

The vector counterpart of the Canvas overload below, so a document already rendered through render_svg() – a Plot’s or a Figure’s – is saved the same way every other chart is rather than reaching for canvas’s own writer. A raster extension raises: this is markup, and turning it into pixels is render()’s job from the Plot it came from, at whatever size and resolution that caller wants.

Args:

  • svg (SvgCanvas): The rendered document.
  • path (String): Where to write it; the extension must be .svg, or absent.

Raises:

Error: A .png or .bmp path, or the write fails.

fn def save(canvas: Canvas, path: String)

Write an already-rendered Canvas to path: BMP for a .bmp extension, PNG otherwise. A .svg path raises, since raster pixels can’t become vector markup.

Args:

  • canvas (Canvas)
  • path (String)

Raises:

fn def save(var pdf: PdfCanvas, path: String)

Write an already-rendered PdfCanvas to path (#696).

The third rendered-canvas overload, completing the set: before it, a caller holding a PdfCanvas had no save() at all and had to reach for canvas.vector.pdf.write_pdf while every other canvas type saved through this function.

Takes the document by value rather than by mut, so save(render_pdf(plot), path) compiles inline the way save(scatter(x, y), path) does; write_pdf needs a mutable document because writing finalizes it, and an owned parameter is mutable without forcing the caller to bind a temporary first.

Args:

  • pdf (PdfCanvas): The rendered document.
  • path (String): Where to write it; the extension must be .pdf, or absent.

Raises:

Error: A .png, .bmp or .svg path, or the write fails.