octave: Printing and Saving Plots

 
 15.2.9 Printing and Saving Plots
 --------------------------------
 
 The ‘print’ command allows you to send plots to you printer and to save
 plots in a variety of formats.  For example,
 
      print -dpsc
 
 prints the current figure to a color PostScript printer.  And,
 
      print -deps foo.eps
 
 saves the current figure to an encapsulated PostScript file called
 ‘foo.eps’.
 
    The current graphic toolkits produce very similar graphic displays
 but differ in their capability to display text and in print
 capabilities.  In particular, the OpenGL based toolkits such as ‘fltk’
 and ‘qt’ do not support the "interpreter" property of text objects.
 This means that when using OpenGL toolkits special symbols drawn with
 the "tex" interpreter will appear correctly on-screen but will be
 rendered with interpreter "none" when printing unless one of the
 ‘standalone’ (see below) modes is used.  These modes provide access to
 the pdflatex processor and therefore allow full use of LaTeX commands.
 
    A complete example showing the capabilities of text printing using
 the ‘-dpdflatexstandalone’ option is:
 
      x = 0:0.01:3;
      hf = figure ();
      plot (x, erf (x));
      hold on;
      plot (x, x, "r");
      axis ([0, 3, 0, 1]);
      text (0.65, 0.6175, ['$\displaystyle\leftarrow x = {2\over\sqrt{\pi}}'...
                           '\int_{0}^{x}e^{-t^2} dt = 0.6175$']);
      xlabel ("x");
      ylabel ("erf (x)");
      title ("erf (x) with text annotation");
      set (hf, "visible", "off");
      print (hf, "plot15_7.pdf", "-dpdflatexstandalone");
      set (hf, "visible", "on");
      system ("pdflatex plot15_7");
      open ("plot15_7.pdf");
 
  -- : print ()
  -- : print (OPTIONS)
  -- : print (FILENAME, OPTIONS)
  -- : print (H, FILENAME, OPTIONS)
      Print a plot, or save it to a file.
 
      Both output formatted for printing (PDF and PostScript), and many
      bitmapped and vector image formats are supported.
 
      FILENAME defines the name of the output file.  If the filename has
      no suffix, one is inferred from the specified device and appended
      to the filename.  If no filename is specified, the output is sent
      to the printer.
 
      H specifies the handle of the figure to print.  If no handle is
      specified the current figure is used.
 
      For output to a printer, PostScript file, or PDF file, the paper
      size is specified by the figure’s ‘papersize’ property.  The
      location and size of the image on the page are specified by the
      figure’s ‘paperposition’ property.  The orientation of the page is
      specified by the figure’s ‘paperorientation’ property.
 
      The width and height of images are specified by the figure’s
      ‘paperposition(3:4)’ property values.
 
      The ‘print’ command supports many OPTIONS:
 
      ‘-fH’
           Specify the handle, H, of the figure to be printed.  The
           default is the current figure.
 
      ‘-PPRINTER’
           Set the PRINTER name to which the plot is sent if no FILENAME
           is specified.
 
      ‘-GGHOSTSCRIPT_COMMAND’
           Specify the command for calling Ghostscript.  For Unix and
           Windows the defaults are "gs" and "gswin32c", respectively.
 
      ‘-color’
      ‘-mono’
           Color or monochrome output.
 
      ‘-solid’
      ‘-dashed’
           Force all lines to be solid or dashed, respectively.
 
      ‘-portrait’
      ‘-landscape’
           Specify the orientation of the plot for printed output.  For
           non-printed output the aspect ratio of the output corresponds
           to the plot area defined by the "paperposition" property in
           the orientation specified.  This option is equivalent to
           changing the figure’s "paperorientation" property.
 
      ‘-TextAlphaBits=N’
      ‘-GraphicsAlphaBits=N’
           Octave is able to produce output for various printers,
           bitmaps, and vector formats by using Ghostscript.  For bitmap
           and printer output anti-aliasing is applied using
           Ghostscript’s TextAlphaBits and GraphicsAlphaBits options.
           The default number of bits are 4 and 1 respectively.  Allowed
           values for N are 1, 2, or 4.
 
      ‘-dDEVICE’
           The available output format is specified by the option DEVICE,
           and is one of:
 
           ‘ps’
           ‘ps2’
           ‘psc’
           ‘psc2’
                PostScript (level 1 and 2, mono and color).  The
                OpenGL-based toolkits always generate PostScript level
                3.0.
 
           ‘eps’
           ‘eps2’
           ‘epsc’
           ‘epsc2’
                Encapsulated PostScript (level 1 and 2, mono and color).
                The OpenGL-based toolkits always generate PostScript
                level 3.0.
 
           ‘pslatex’
           ‘epslatex’
           ‘pdflatex’
           ‘pslatexstandalone’
           ‘epslatexstandalone’
           ‘pdflatexstandalone’
                Generate a LaTeX file ‘FILENAME.tex’ for the text
                portions of a plot and a file ‘FILENAME.(ps|eps|pdf)’ for
                the remaining graphics.  The graphics file suffix
                .ps|eps|pdf is determined by the specified device type.
                The LaTeX file produced by the ‘standalone’ option can be
                processed directly by LaTeX.  The file generated without
                the ‘standalone’ option is intended to be included from
                another LaTeX document.  In either case, the LaTeX file
                contains an ‘\includegraphics’ command so that the
                generated graphics file is automatically included when
                the LaTeX file is processed.  The text that is written to
                the LaTeX file contains the strings *exactly* as they
                were specified in the plot.  If any special characters of
                the TeX mode interpreter were used, the file must be
                edited before LaTeX processing.  Specifically, the
                special characters must be enclosed with dollar signs (‘$
                ... $’), and other characters that are recognized by
                LaTeX may also need editing (.e.g., braces).  The
                ‘pdflatex’ device, and any of the ‘standalone’ formats,
                are not available with the Gnuplot toolkit.
 
           ‘epscairo’
           ‘pdfcairo’
           ‘epscairolatex’
           ‘pdfcairolatex’
           ‘epscairolatexstandalone’
           ‘pdfcairolatexstandalone’
                Generate Cairo based output when using the Gnuplot
                graphics toolkit.  The ‘epscairo’ and ‘pdfcairo’ devices
                are synonymous with the ‘epsc’ device.  The LaTeX
                variants generate a LaTeX file, ‘FILENAME.tex’, for the
                text portions of a plot, and an image file,
                ‘FILENAME.(eps|pdf)’, for the graph portion of the plot.
                The ‘standalone’ variants behave as described for
                ‘epslatexstandalone’ above.
 
           ‘ill’
           ‘aifm’
                Adobe Illustrator (Obsolete for Gnuplot versions > 4.2)
 
           ‘canvas’
                Javascript-based drawing on HTML5 canvas viewable in a
                web browser (only available for the Gnuplot graphics
                toolkit).
 
           ‘cdr’
           ‘corel’
                CorelDraw
 
           ‘dxf’
                AutoCAD
 
           ‘emf’
           ‘meta’
                Microsoft Enhanced Metafile
 
           ‘fig’
                XFig.  For the Gnuplot graphics toolkit, the additional
                options ‘-textspecial’ or ‘-textnormal’ can be used to
                control whether the special flag should be set for the
                text in the figure.  (default is ‘-textnormal’)
 
           ‘gif’
                GIF image (only available for the Gnuplot graphics
                toolkit)
 
           ‘hpgl’
                HP plotter language
 
           ‘jpg’
           ‘jpeg’
                JPEG image
 
           ‘latex’
                LaTeX picture environment (only available for the Gnuplot
                graphics toolkit).
 
           ‘mf’
                Metafont
 
           ‘png’
                Portable network graphics
 
           ‘pbm’
                PBMplus
 
           ‘pdf’
                Portable document format
 
      ‘svg’
           Scalable vector graphics
 
      ‘tikz’
      ‘tikzstandalone’
           Generate a LaTeX file using PGF/TikZ format.  The OpenGL-based
           toolkits create a PGF file while Gnuplot creates a TikZ file.
           The ‘tikzstandalone’ device produces a LaTeX document which
           includes the TikZ file (‘tikzstandalone’ and is only available
           for the Gnuplot graphics toolkit).
 
           If the device is omitted, it is inferred from the file
           extension, or if there is no filename it is sent to the
           printer as PostScript.
 
      ‘-dGHOSTSCRIPT_DEVICE’
           Additional devices are supported by Ghostscript.  Some
           examples are;
 
           ‘pdfwrite’
                Produces pdf output from eps
 
           ‘ljet2p’
                HP LaserJet IIP
 
           ‘pcx24b’
                24-bit color PCX file format
 
           ‘ppm’
                Portable Pixel Map file format
 
           For a complete list, type ‘system ("gs -h")’ to see what
           formats and devices are available.
 
           When Ghostscript output is sent to a printer the size is
           determined by the figure’s "papersize" property.  When the
           output is sent to a file the size is determined by the plot
           box defined by the figure’s "paperposition" property.
 
      ‘-append’
           Append PostScript or PDF output to a pre-existing file of the
           same type.
 
      ‘-rNUM’
           Resolution of bitmaps in pixels per inch.  For both metafiles
           and SVG the default is the screen resolution; for other
           formats it is 150 dpi.  To specify screen resolution, use
           "-r0".
 
      ‘-loose’
      ‘-tight’
           Force a tight or loose bounding box for eps files.  The
           default is loose.
 
      ‘-PREVIEW’
           Add a preview to eps files.  Supported formats are:
 
           ‘-interchange’
                Provide an interchange preview.
 
           ‘-metafile’
                Provide a metafile preview.
 
           ‘-pict’
                Provide pict preview.
 
           ‘-tiff’
                Provide a tiff preview.
 
      ‘-SXSIZE,YSIZE’
           Plot size in pixels for EMF, GIF, JPEG, PBM, PNG, and SVG.
           For PS, EPS, PDF, and other vector formats the plot size is in
           points.  This option is equivalent to changing the size of the
           plot box associated with the "paperposition" property.  When
           using the command form of the print function you must quote
           the XSIZE,YSIZE option.  For example, by writing "-S640,480".
 
      ‘-FFONTNAME’
      ‘-FFONTNAME:SIZE’
      ‘-F:SIZE’
           Use FONTNAME and/or FONTSIZE for all text.  FONTNAME is
           ignored for some devices: dxf, fig, hpgl, etc.
 
      The filename and options can be given in any order.
 
      Example: Print to a file using the pdf device.
 
           figure (1);
           clf ();
           surf (peaks);
           print figure1.pdf
 
      Example: Print to a file using jpg device.
 
           clf ();
           surf (peaks);
           print -djpg figure2.jpg
 
      Example: Print to printer named PS_printer using ps format.
 
           clf ();
           surf (peaks);
           print -dpswrite -PPS_printer
 
DONTPRINTYET       See also: Seesaveas XREFsaveas, Seehgsave XREFhgsave, *noteDONTPRINTYET       See also: Seesaveas XREFsaveas, Seehgsave XREFhgsave, See
      orient XREForient, Seefigure XREFfigure.
 
  -- : saveas (H, FILENAME)
  -- : saveas (H, FILENAME, FMT)
      Save graphic object H to the file FILENAME in graphic format FMT.
 
      FMT should be one of the following formats:
 
      ‘ps’
           PostScript
 
      ‘eps’
           Encapsulated PostScript
 
      ‘jpg’
           JPEG Image
 
      ‘png’
           PNG Image
 
      ‘emf’
           Enhanced Meta File
 
      ‘pdf’
           Portable Document Format
 
      All device formats specified in ‘print’ may also be used.  If FMT
      is omitted it is extracted from the extension of FILENAME.  The
      default format is "pdf".
 
           clf ();
           surf (peaks);
           saveas (1, "figure1.png");
 
DONTPRINTYET       See also: Seeprint XREFprint, Seehgsave XREFhgsave, *noteDONTPRINTYET       See also: Seeprint XREFprint, Seehgsave XREFhgsave, See
      orient XREForient.
 
  -- : orient (ORIENTATION)
  -- : orient (HFIG, ORIENTATION)
  -- : ORIENTATION = orient ()
  -- : ORIENTATION = orient (HFIG)
      Query or set the print orientation for figure HFIG.
 
      Valid values for ORIENTATION are "portrait", "landscape", and
      "tall".
 
      The "landscape" option changes the orientation so the plot width is
      larger than the plot height.  The "paperposition" is also modified
      so that the plot fills the page, while leaving a 0.25 inch border.
 
      The "tall" option sets the orientation to "portrait" and fills the
      page with the plot, while leaving a 0.25 inch border.
 
      The "portrait" option (default) changes the orientation so the plot
      height is larger than the plot width.  It also restores the default
      "paperposition" property.
 
      When called with no arguments, return the current print
      orientation.
 
      If the argument HFIG is omitted, then operate on the current figure
      returned by ‘gcf’.
 
      See also: Seeprint XREFprint, Seesaveas XREFsaveas.
 
    ‘print’ and ‘saveas’ are used when work on a plot has finished and
 the output must be in a publication-ready format.  During intermediate
 stages it is often better to save the graphics object and all of its
 associated information so that changes—to colors, axis limits, marker
 styles, etc.—can be made easily from within Octave.  The
 ‘hgsave’/‘hgload’ commands can be used to save and re-create a graphics
 object.
 
  -- : hgsave (FILENAME)
  -- : hgsave (H, FILENAME)
  -- : hgsave (H, FILENAME, FMT)
      Save the graphics handle H to the file FILENAME in the format FMT.
 
      If unspecified, H is the current figure as returned by ‘gcf’.
 
      When FILENAME does not have an extension the default filename
      extension ‘.ofig’ will be appended.
 
      If present, FMT should be one of the following:
 
         • ‘-binary’, ‘-float-binary’
 
         • ‘-hdf5’, ‘-float-hdf5’
 
         • ‘-V7’, ‘-v7’, ‘-7’, ‘-mat7-binary’
 
         • ‘-V6’, ‘-v6’, ‘-6’, ‘-mat6-binary’
 
         • ‘-text’
 
         • ‘-zip’, ‘-z’
 
      When producing graphics for final publication use ‘print’ or
      ‘saveas’.  When it is important to be able to continue to edit a
      figure as an Octave object, use ‘hgsave’/‘hgload’.
 
DONTPRINTYET       See also: Seehgload XREFhgload, *notehdl2struct:
DONTPRINTYET       See also: Seehgload XREFhgload, Seehdl2struct

      XREFhdl2struct, Seesaveas XREFsaveas, Seeprint XREFprint.
 
  -- : H = hgload (FILENAME)
      Load the graphics object in FILENAME into the graphics handle H.
 
      If FILENAME has no extension, Octave will try to find the file with
      and without the standard extension of ‘.ofig’.
 
DONTPRINTYET       See also: Seehgsave XREFhgsave, *notestruct2hdl:
DONTPRINTYET       See also: Seehgsave XREFhgsave, Seestruct2hdl

      XREFstruct2hdl.