[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: saving plots
From: |
Francesco Potorti` |
Subject: |
Re: saving plots |
Date: |
Tue, 10 Sep 2002 05:23:21 -0500 |
If you are looking at a plot you like and you want to save it to a file
named "myplot" then do the following
gset terminal png
gset output "myplot.png"
replot
This will produce a png file. Check out the "set terminal" command in
gnuplot to see all the possible output formats.
I append two print.m functions which are handy to print a plot on
printer or on a file. The first one is what I have used for some years,
and is an improvement over an old version by Daniel Heiserer. The
second one has the same origin, is part of the octave-forge
distribution, and looks more complete and generally better at first
sight. The reason why I include the first version at all is that I just
discovered the second, and do not know whether it works with older
versions of Octave.
===File /home/pot/math/octavelib/utils/print.m==============
## USAGE: print('filename','device')
## sends a copy of the current plot to a 'device' file
## named FILENAME.
## if no arguments at all are specified a postscript file
## is piped to "|lpr"
##
##
## SPECIFICATION OF THE OUTPUT FILE
## 'filename' specifies the name of the file
##
## DEVICE DRIVERS
## the following devices are supported according to gnuplot's great
support of
## "terminals":
## 'aed512' 'hpdj' 'pstricks'
## 'aed767' 'hpgl' 'qms'
## 'aifm' 'hpljii' 'regis'
## 'bitgraph' 'hppj' 'selanar'
## 'cgm' 'imagen' 'starc'
## 'corel' 'kc-tek40xx' 'table'
## 'dumb' 'km-tek40xx' 'tandy-60dpi'
## 'dxf' 'latex' 'tek40xx'
## 'eepic' 'mf' 'tek410x'
## 'emtex' 'mif' 'texdraw'
## 'epson-180dpi' 'nec-cp6' 'tgif'
## 'epson-60dpi' 'okidata' 'tkcanvas'
## 'epson-lx800' 'pbm' 'tpic'
## 'fig' 'png' 'vttek'
## 'gpic' 'pcl5' 'x11'
## 'hp2623a' 'postscript' 'xlib'
## 'hp2648' 'pslatex'
## 'hp500c' 'pstex'
## However some devices are also available under other names: (A "+"
indicates support).
## + '-dps' # same as -postscript
## + '-dpsc' # same as -postscript, but colored
## + '-deps' # Encapsulated PostScript
## + '-depsc' # colored Encapsulated PostScript
## '-dhpgl' # same as -hpgl
## '-djpeg' # jpeg file format, will be translated via pbm and convert
## '-dgif' # gif file format, will be translated via pbm and convert
## '-dtiff' # tiff file, will be translated via pbm and convert
## + '-dpng' # Portable Network Graphics.
##
##
## REQUIRES: gget
##
##
## REMARK: original terminal and output of gnuplot is restored.
## AUTHOR: Daniel Heiserer <address@hidden>
## Copyright (C) 1999 Daniel Heiserer
## Thanks to Mario Storti for his 1st version.
##
## This file is distributed in the hope that it will be useful, but
## WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
## General Public License for more details.
##
## You should have received a copy of the GNU General Public License
## along with this file; see the file COPYING. If not, write to the Free
## Software Foundation, 59 Temple Place - Suite 330, Boston, MA
## 02111-1307, USA.
function print(file,terminal);
% remember to reset!
OLD_automatic_replot=automatic_replot;
% change it to 0. automatic replot is cool because it's interactive, so many
people might use it.
% otherwise it has some bad features for printing (writing to a file):
% for me it created always to plots in a postscript file.
% for the png terminal the replot emptied the file, while switiching
the "terminal" back.
automatic_replot=0;
% take care of the settings we had before
origterm=sprintf('gset terminal %s',gget('terminal'));
origterm=origterm(1:length(origterm)-1);
origout=sprintf('gset output %s',gget('output'));
origout=origout(1:length(origout)-1);
% never used and never checked
if nargin==0
file="|lpr";
terminal='ps';
end
% us a default for the terminal. Postcript is cool. So take it.
if nargin==1
terminal='ps';
end
% lets cut down the first 2 characters, if we have a "-d" device specification:
END=length(terminal);
if terminal(1:2)=='-d'
terminal=terminal(3:END);
end
extension=terminal;
% make sure the string is long enough, otherwise octave has problems with
comparing them, unfortunately
% octave is far off beeing as cool as perl in handling strings :-((
terminal=[terminal,' '];
% lets create a filename, assuming you want to have the extension indicating
the terminal
% if you have already a '.' in the file, I assume you don't want it.
if (file(1) == '|' || length(find(file=='.'))>0)
filename=file;
else
filename=[file '.' extension];
end
# just to make sure, the stuff is not appended, happend for the png terminal
if (file(1) != '|')
unlink(filename);
endif
eval(['gset output "',filename,'"']);
disp(['gset output "',filename,'"']);
% let's have a look at the terminal now:
if terminal(1:2)=='ps' || terminal(1:3)=='psc'
gset term postscript landscape color
#disp("gset term postscript landscape color");
replot
#disp("replot");
elseif terminal(1:3)=='eps'
gset term postscript eps color
disp("gset term postscript eps color");
replot
disp("replot");
elseif terminal(1:3)=='pbm'
gset term pbm color
disp("gset term pbm color");
replot
disp("replot");
elseif terminal(1:3)=='png'
gset term png color medium
disp("gset term png color medium");
replot
disp("replot");
elseif terminal(1:3)=='gif'
[STDOUT,STDERR]=system('which convert');
if STDERR==0
gset term pbm color
replot
todo=sprintf('convert %s %s.%s\n',filename,file,'gif');
system(todo);
unlink(filename);
else
error("Sorry you don't have convert on your machine,
gif-device not supportet");
end
else
% ok, now lets look at the gnuplot devices, be blind and use
what we have
disp(['Warning: assuming raw gnuplot setting for terminal:
',terminal]);
todo=['gset term ',terminal];
eval(todo);
disp(todo);
replot
disp("replot");
end
% In fact we are nearly finished. Lets clean up the things we changed and
restore the settings:
%===================================
% restore our old settings:
%-----------------------------------
% 1) terminal
eval(origterm);
#disp(origterm)
%-----------------------------------
% 2) output
# gnuplot is a bit messy here, once we start with a x11 device, he says
output is "", should be STDOUT
origout=[origout,' '];
if origout(1:18)=="gset output output"
eval('gset output');
#disp('gset output');
else
disp(origout)
#eval(origout);
end
replot
%-----------------------------------
% 3) plotting options
automatic_replot=OLD_automatic_replot;
%-----------------------------------
endfunction
============================================================
===File /usr/share/octave/2.1.35/site/m/octave-forge/plot/print.m===
## Copyright (C) 1999 Daniel Heiserer, Copyright (C) 2001 Laurent Mazet
##
## This program is free software; it is distributed in the hope that it
## will be useful, but WITHOUT ANY WARRANTY; without even the implied
## warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
## the GNU General Public License for more details.
##
## You should have received a copy of the GNU General Public License
## along with this file; see the file COPYING. If not, write to the
## Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
## 02111-1307, USA.
## usage: print (filename, options)
##
## Print a graph, or save it to a file
##
## filename:
## File to receive output. If no filename is specified, output is
## sent to the printer.
##
## options:
## -Pprinter
## Printer to which the graph is sent if no filename is specified.
##
## -color | -mono
## Monochrome or colour lines.
##
## -solid | -dashed
## Solid or dashed lines.
##
## -portrait | -landscape
## Plot orientation, as returned by "orient".
##
## -dDEVICE
## Output device, where DEVICE is one of:
##
## ps,ps2,psc,psc2
## Postscript (level 1 and 2, mono and color)
## eps,eps2,epsc,epsc2
## Encapsulated postscript (level 1 and 2, mono and color)
## ill,aifm
## Adobe Illustrator
## cdr,corel
## CorelDraw
## hpgl HP plotter language
## fig XFig
## dxf AutoCAD
## mf Metafont
## png Portable network graphics
## pbm PBMplus
##
## Other devices are supported by "convert" from ImageMagick. Type
## system("convert") to see what formats are available.
##
## 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.
##
## -Ffontname, -Ffontname:size, -F:size
## Postscript font (for use with postscript, aifm, corel and fig)
## "Helvetica" by default for PS/Aifm, "SwitzerlandLight" for Corel
## Can also be "Times-Roman". The font size is given in points.
## The fontname is ignored for the fig device.
##
## The filename and options can be given in any order.
##
## If you are using Octave 2.1.x or above, command("print") will change
## print from a function to a command, so instead of typing
## print("-FTimes-Roman:14", "-dashed", "-depsc", "out.ps")
## you can type
## print -FTimes-Roman:14 -dashed -depsc out.ps
##
## See also: orient, command
## Author: Daniel Heiserer <address@hidden>
## 2001-03-23 Laurent Mazet <address@hidden>
## * simplified interface: guess the device from the extension
## * font support
## 2001-03-25 Paul Kienzle <address@hidden>
## * add unwind_protect
## * use tmpnam to generate temporary name
## * move "set term" before "set output" as required by gnuplot
## * more options, and flexible options
## 2001-03-29 Laurent Mazet <address@hidden>
## * add solid and dashed options
## * change PBMplus device
## * add Corel device
## * take care of the default terminal settings to restore them.
## * add color, mono, dashed and solid support for printing and convert.
## * add orientation for printing.
## * create a .ps for printing (avoid some filtering problems).
## * default printing is mono, default convert is color.
## * add font size support.
## 2001-03-30 Laurent Mazet <address@hidden>
## * correct correl into corel
## * delete a irrelevant test
## * check for convert before choosing the ouput device
## 2001-03-31 Paul Kienzle <address@hidden>
## * use -Ffontname:size instead of -F"fontname size"
## * add font size support to fig option
## * update documentation
function print(...)
## take care of the settings we had before
origterm = gget("terminal");
origout = gget("output");
_automatic_replot = automatic_replot;
## take care of the default terminal settings to restore them.
terminal_default = "";
orientation = orient;
use_color = 0; # 0=default, -1=mono, +1=color
force_solid = 0; # 0=default, -1=dashed, +1=solid
fontsize = font = name = devopt = printer = "";
va_start();
for i=1:nargin
arg = va_arg();
if isstr(arg)
if strcmp(arg, "-color")
use_color = 1;
elseif strcmp(arg, "-mono")
use_color = -1;
elseif strcmp(arg, "-solid")
force_solid = 1;
elseif strcmp(arg, "-dashed")
force_solid = -1;
elseif strcmp(arg, "-portrait")
orientation = "portrait";
elseif strcmp(arg, "-landscape")
orientation = "landscape";
elseif length(arg) > 2 && arg(1:2) == "-d"
devopt = arg(3:length(arg));
elseif length(arg) > 2 && arg(1:2) == "-P"
printer = arg;
elseif length(arg) > 2 && arg(1:2) == "-F"
idx = rindex(arg, ":");
if (idx)
font = arg(3:idx-1);
fontsize = arg(idx+1:length(arg));
else
font = arg(3:length(arg));
endif
elseif length(arg) >= 1 && arg(1) == "-"
error([ "print: unknown option ", arg ]);
elseif length(arg) > 0
name = arg;
endif
else
error("print: expects string options");
endif
endfor
doprint = isempty(name);
if doprint
if isempty(devopt)
printname = [ tmpnam, ".ps" ];
else
printname = [ tmpnam, ".", devopt ];
endif
name = printname;
endif
if isempty(devopt)
dot = rindex(name, ".");
if (dot == 0)
error ("print: no format specified");
else
dev = tolower(name(dot+1:length(name)));
endif
else
dev = devopt;
endif
if strcmp(dev, "ill")
dev = "aifm";
elseif strcmp(dev, "cdr")
dev = "corel";
endif
## check if we have to use convert
dev_list = [" aifm corel fig png pbm dxf mf hpgl", ...
" ps ps2 psc psc2 eps eps2 epsc epsc2 " ];
convertname = "";
if isempty(findstr(dev_list , [ " ", dev, " " ]))
if !isempty(devopt)
convertname = [ devopt ":" name ];
else
convertname = name;
endif
dev = "epsc";
name = [ tmpnam, ".eps" ];
endif
unwind_protect
automatic_replot = 0;
if strcmp(dev, "ps") || strcmp(dev, "ps2") ...
|| strcmp(dev, "psc") || strcmp(dev, "psc2") ...
|| strcmp(dev, "epsc") || strcmp(dev, "epsc2") ...
|| strcmp(dev, "eps") || strcmp(dev, "eps2")
## Various postscript options
gset term postscript;
terminal_default = gget ("terminal");
if dev(1) == "e"
options = "eps ";
else
options = [ orientation, " " ];
endif
options = [ options, "enhanced " ];
if any( dev == "c" ) || use_color > 0
if force_solid < 0
options = [ options, "color dashed " ];
else
options = [ options, "color solid " ];
endif
else
if force_solid > 0
options = [ options, "mono solid " ];
else
options = [ options, "mono dashed " ];
endif
endif
if !isempty(font)
options = [ options, "\"", font, "\" " ];
endif
if !isempty(fontsize)
options = [ options, " ", fontsize ];
endif
eval (sprintf ("gset term postscript %s;", options));
elseif strcmp(dev, "aifm") || strcmp(dev, "corel")
## Adobe Illustrator, CorelDraw
eval(sprintf ("gset term %s;", dev));
terminal_default = gget ("terminal");
if (use_color >= 0)
options = " color";
else
options = " mono";
endif
if !isempty(font)
options = [ options, " \"" , font, "\"" ];
endif
if !isempty(fontsize)
options = [ options, " ", fontsize ];
endif
eval (sprintf ("gset term %s %s;", dev, options));
elseif strcmp(dev, "fig")
## XFig
gset term fig;
terminal_default = gget ("terminal");
options = orientation;
if (use_color >= 0)
options = " color";
else
options = " mono";
endif
if !isempty(fontsize)
options = [ options, " fontsize ", fontsize ];
endif
eval (sprintf ("gset term fig %s;", option));
elseif strcmp(dev, "png") || strcmp(dev, "pbm")
## Portable network graphics, PBMplus
eval(sprintf ("gset term %s;", dev));
terminal_default = gget ("terminal");
if (use_color >= 0)
eval (sprintf ("gset term %s color medium;", dev));
else
eval (sprintf ("gset term %s mono medium;", dev));
endif
elseif strcmp(dev,"dxf") || strcmp(dev,"mf") || strcmp(dev, "hpgl")
## AutoCad DXF, METAFONT, HPGL
eval (sprintf ("gset terminal %s;", dev));
endif;
eval (sprintf ("gset output \"%s\";", name));
replot;
unwind_protect_cleanup
## Restore init state
if isempty (origout)
gset output;
else
eval (sprintf ("gset output %s;", origout));
end
if ! isempty (terminal_default)
eval (sprintf ("gset terminal %s;", terminal_default));
endif
eval (sprintf ("gset terminal %s;", origterm));
replot;
automatic_replot = _automatic_replot ;
end_unwind_protect
if !isempty(convertname)
command = [ "convert ", name, " ", convertname ];
[output, errcode] = system (command);
unlink (name);
if (errcode)
error ("print: could not convert");
endif
endif
if doprint
system(sprintf ("lpr %s %s", printer, printname));
unlink(printname);
endif
endfunction
============================================================
-------------------------------------------------------------
Octave is freely available under the terms of the GNU GPL.
Octave's home on the web: http://www.octave.org
How to fund new projects: http://www.octave.org/funding.html
Subscription information: http://www.octave.org/archive.html
-------------------------------------------------------------