1 A short guide to PostScript files created by gnuplot's "postscript" terminal
3 by Dick Crawford, aka rccrawford@lanl.gov
5 Before we begin, let me say a few words about PostScript. It is highly ver-
6 satile, but with versatility comes complexity. Syntax is extremely important;
7 a misplaced command can do unexpected things. Thus the adventurous soul who
8 wishes to alter the PostScript but has not been initiated into the arcana of
9 PostScript would be well-advised to change only those items (like numerical
10 values, font names or heights, or the letters defining symbols) that are
11 fairly obvious as to their meaning.
13 PostScript is stack-oriented and works in Reverse Polish notation:
14 'a b add' takes 'a' and 'b' off of the stack and replaces them with 'a+b'.
16 The 'stroke' command actually draws lines on the page. It uses whatever line
17 width and style are currently defined. Thus if we specify a bunch of commands
18 like 'LT1 1 2 moveto 3 4 lineto LT3 stroke' [LT1 and LT3 are line type defi-
19 nitions, 'x y moveto' moves the (virtual) pen to (x,y) and 'x y lineto' draws
20 a line from wherever the pen was to (x,y)], the line connecting points (1,2)
21 and (3,4) will be in type LT3.
23 PostScript is case-sensitive.
25 The sample file below has been pruned of repeated commands, just to keep its
26 length down. Comment lines begin with '%'. Lines beginning with '%%' are
27 inserted for "encapsulated PostScript" -- these lines are read by applications
28 that import PostScript. Lines beginning with '%#' have been added to the
29 sample by me for explanatory purposes.
31 I have merged files created by the postscript terminal with no options, with
32 the "eps" option and with the "enhanced" option. The differences will be
33 clearly pointed out when appropriate.
39 %%Creator: gnuplot 3.5 (pre 3.6) patchlevel beta 338
40 %%CreationDate: Fri Jan 16 13:18:18 1998
41 %%DocumentFonts: (atend)
42 %%BoundingBox: 50 50 554 770
43 %%Orientation: Landscape
44 %# In 'eps' mode, the preceding two lines would look like:
45 %# %%BoundingBox: 50 50 410 302
46 %# %%Orientation: Portrait
50 %# The 'dictionary' contains stuff defined by the user.
55 %# The following switch toggles between color and monochromatic.
59 %# The following switch toggles between solid and dot-dash lines.
63 %# The following parameter scales all linewidths in the plot.
65 /gnulinewidth 5.000 def
66 /userlinewidth gnulinewidth def
68 %# The following parameter specifies the vertical displacement of the labels
69 %# and titles (it is used in the 'show' commands defined below). It should
70 %# be about 1/3 of the font height.
74 %# The following parameter scales the lengths of all dot-dash patterns.
78 %# The following two parameters scale the horizontal and vertical sizes of
79 %# the symbols used by 'plot with points'.
86 %# The following four commands are aliases of the four 'pen movement'
87 %# commands. Use of these aliases significantly shortens the file. Note
88 %# that the first two are 'absolute' movements and the latter two are
89 %# 'relative'. M and R move to the specified position; L and V draw a
90 %# straight line (from the current position) to it.
97 %# These are a couple more parameters used in plotting symbols. Why
98 %# they are here instead of up with 'hpt' and 'vpt' I know not.
103 %# The 'show' command writes out a character string. The following three
104 %# varieties do so as left-, right-, and center-justified. [Remember,
105 %# we're still in the dictionary--the font doesn't need to be specified
106 %# until we actually use one of these.]
108 /Lshow { currentpoint stroke M
109 0 vshift R show } def
110 /Rshow { currentpoint stroke M
111 dup stringwidth pop neg vshift R show } def
112 /Cshow { currentpoint stroke M
113 dup stringwidth pop -2 div vshift R show } def
115 %# The following commands define the various line types (normal, bold,
116 %# dashed, etc.) used by gnuplot.
118 %# UP, DL, and UL are busywork commands used by others here
120 /UP { dup vpt_ mul /vpt exch def hpt_ mul /hpt exch def
121 /hpt2 hpt 2 mul def /vpt2 vpt 2 mul def } def
122 /DL { Color {setrgbcolor Solid {pop []} if 0 setdash }
123 {pop pop pop Solid {pop []} if 0 setdash} ifelse } def
124 /BL { stroke gnulinewidth 2 mul setlinewidth } def %# twice the linewidth
125 /AL { stroke gnulinewidth 2 div setlinewidth } def %# half the linewidth
126 /UL { gnulinewidth mul /userlinewidth exch def } def
127 /PL { stroke userlinewidth setlinewidth } def %# normal linewidth
128 /LTb { BL [] 0 0 0 DL } def
129 /LTa { AL [1 dl 2 dl] 0 setdash 0 0 0 setrgbcolor } def
130 /LT0 { PL [] 0 1 0 DL } def
131 /LT1 { PL [4 dl 2 dl] 0 0 1 DL } def
133 %# ...and a bunch more.
134 %# In the LT's, the first command ('PL' for LT1) sets the linewidth,
135 %# the stuff in [...] defines the dot-dash pattern, and the three numbers
136 %# define the rgb color.
138 %# The following commands define the symbols used to plot data points.
140 /Pnt { stroke [] 0 setdash
141 gsave 1 setlinecap M 0 0 V stroke grestore } def
142 /Dia { stroke [] 0 setdash 2 copy vpt add M
143 hpt neg vpt neg V hpt vpt neg V
144 hpt vpt V hpt neg vpt V closepath stroke
147 %# ...and a bunch (dozens) more.
149 %# The 'MF...' commands are used to handle the 'enhanced' syntax. If the
150 %# 'enhanced' mode is not invoked, these commands won't appear in the file.
152 /MFshow {{dup dup 0 get findfont exch 1 get scalefont setfont
153 [ currentpoint ] exch dup 2 get 0 exch rmoveto dup dup 5 get exch 4 get
154 {show} {stringwidth pop 0 rmoveto}ifelse dup 3 get
155 {2 get neg 0 exch rmoveto pop} {pop aload pop moveto}ifelse} forall} bind def
156 /MFwidth {0 exch {dup 3 get{dup dup 0 get findfont exch 1 get scalefont setfont
157 5 get stringwidth pop add}
158 {pop} ifelse} forall} bind def
159 /MLshow { currentpoint stroke M
160 0 exch R MFshow } bind def
161 /MRshow { currentpoint stroke M
162 exch dup MFwidth neg 3 -1 roll R MFshow } def
163 /MCshow { currentpoint stroke M
164 exch dup MFwidth -2 div 3 -1 roll R MFshow } def
167 %# The dictionary is now complete. We activate it, save what went before
168 %# (the 'gsave' command saves everything on a different stack) and begin.
175 %# First we position the plot on the page and scale it.
177 %# The 'translate' command moves the origin to the specified position.
178 %# [The PostScript default origin is near the lower left-hand corner.]
179 %# The 'scale' command changes the units used in the plot.
180 %# [The PostScript default unit is the point: 72 points equal one inch.]
181 %# The 'rotation' command rotates the coordinates clockwise through the
182 %# specified angle (degrees).
183 %# [The PostScript default orientation is profile.]
185 %# This sample changes the orientation to landscape (the gnuplot default)
186 %# and the unit to tenths of a point. Note that if the first two commands
187 %# were interchanged, the translation would be only five points in each
188 %# direction, instead of fifty.
190 %# If this had been generated in 'eps' mode, the "90 rotate" and "0 -5040
191 %# translate" commands would not appear and the units would be 0.050 instead
194 %# If you want to change the size or the position of the plot, this is where
204 %# Define the default font. The number is the height. As usual, fonts
205 %# used primarily for text have characters only about 70% the specified
206 %# height, because spacing between lines is built-in. The Symbol font and
207 %# others that are normally not used for text produce characters that are
208 %# more nearly the specified height. Thus if you want to intersperse, for
209 %# example, Greek and Roman letters (as you might when writing an equation),
210 %# you'll need to play with the heights of the two fonts in order for them
211 %# to look reasonably proportioned.
213 %# The font defined here is used for the tick labels first, then the axis
214 %# labels and plot titles, and finally the key. If you want to change the
215 %# font, simply insert the modified line after you are done with the old one
216 %# (i.e. the last 'show' command to use it) but before the new one is needed.
217 %# Note that if you change the font height, you'll also want to change the
218 %# value of 'vshift' (it's near the top of the dictionary). I'll give an
219 %# example of this later on...
221 (Helvetica) findfont 140 scalefont setfont
224 %# Label and draw the ticks along the y-axis. I've given you three
225 %# y-tics in different formats...
227 %# The tick length is 63 units.
229 %# This first y-tic is in 'normal' mode:
238 %# This y-tic is in 'enhanced' mode:
246 [ [(Helvetica) 140.0 0.0 true true (-5)]
249 %# This one is in 'normal' mode, but is rotated:
256 currentpoint gsave translate 90 rotate 0 0 M
260 %# Now the x-tics. Here's one:
268 [ [(Helvetica) 140.0 0.0 true true (-10)]
272 %# Now draw the left and lower axes. Were this drawn by 'splot'
273 %# instead of 'plot', there might be some other axes drawn.
281 %# Now come labels (both for the axes and those on "set label" commands)
282 %# and other titles. I just give you axis labels here, in 'enhanced' mode:
285 currentpoint gsave translate 90 rotate 0 0 moveto
286 [ [(Helvetica) 140.0 0.0 true true (ylabel)]
290 [ [(Helvetica) 140.0 0.0 true true (xlabel)]
293 %# We're finally ready to plot functions and/or data.
295 %# The key is drawn just before the applicable data.
297 %# Choose a line type and write the key.
298 %# In this sample, it is drawn with lines.
302 6465 4767 M (x) Rshow %# the function label in the key
303 6549 4767 M %# the sample line in the key
306 %# Now the data (this is drawn with some combination of absolute and
307 %# relative lineto's and moveto's):
317 %# We don't need to eplicitly 'stroke' to draw the lines for each function
318 %# because the 'stroke' command is included in the line-type definition
319 %# commands. Thus switching line types automatically 'strokes' previous
320 %# lines. Clever program, that gnuplot...
322 %# We can now repeat commands for the key and data for as many items
323 %# as were specified on the "plot" or "splot" command.
325 %# Here's a second function, plotted with dots:
341 %# Since we're now done, we 'stroke' the last lines, close the dictionary
342 %# and restore the previous settings (those saved by the 'gsave' at the top).
343 %# [The 'gsave'/'grestore' pair is included so that if this file is embedded
344 %# in another PostScript file, this patch won't mess up the other parts of
345 %# the picture. It's considered good PostScript style to do this.]
351 %# And, finally, we send the page to the printer. [If we do embed this
352 %# file into another PostScript file, we'd remove this 'showpage'. Unless,
353 %# of course, this was appended to the other file, in which case we'd
354 %# remove the 'showpage' from the end of that file.]
359 %%DocumentFonts: Helvetica