Vector Graphics with PostScript

pix
Go back to home page

This page provides an introduction to vector graphics using the PostScript language, and Python to generate Poscript code.

This page is obsolete.

The latest page is at itmetr.net.

last modified: 04:57 PM MDT, Sun 15 Jun 2008

This tutorial provides experience in in editing, repairing and making PostScript figures that are intended for use in scientific publications and presentations. We will investigate a simple python module vplot to write custom postscript plots. But first you should learn a little bit of Postscript:


postscript from the keyboard

Download art.ps. Then type

ggv  art.ps 
(Be sure to scroll down to see the art!)

Also open art.ps with a text editor to see the following:

%!PS-Adobe-2.0 % %just !PS is sometimes sufficient
/inch {72 mul} def	% Convert inches->points (1/72 inch)
/Helvetica findfont 40 scalefont setfont %use 40 pt Helvetica font
newpath			% Start a new path
1 inch 1 inch moveto	% an inch in from the lower left
2 inch 1 inch lineto	% bottom side
2 inch 2 inch lineto	% right side
1 inch 2 inch lineto	% top side
20 setlinewidth 	% fat line 20 pts wide
closepath		% Automatically add left side to close path
stroke			% nothing is drawn until you stroke the path!
1.5 inch 2 inch moveto %move to new point to start new object	
1 inch  .1 inch rlineto	% bottom side using relative movement 
-.1 inch 1 inch rlineto	% right side "
-1 inch -.1 inch rlineto 	% top side "
closepath		% Automatically add left side to close path
gsave			% Save the above path, for later reuse	
.5 .2 0 setrgbcolor	% change the color to brown
fill			% Fill in the box 
grestore		% restore the previous path, to reuse it
0 0 1 setrgbcolor %blue
10 setlinewidth 	% 10 pts wide
stroke 			%draw the perimeter of the box
1 inch 1 inch moveto
52 rotate
1 0 0 setrgbcolor %red
(But is it art?) show
showpage		% We're done... eject the page

Now download art.eps and try the same. The only difference in the art.eps file is the the first line of art.ps is replaced with the two lines:

%!PS-Adobe-2.0 EPSF-2.0
%%BoundingBox: 50 50 200 250

and the showpage command had been removed. art.eps is an encapsulated postscript file. The BoundingBox specifies a region beyond which nothing should be rendered. You can find the coordinates of a BoundingBox of your choice by using the cursor in a gvv rendering of your .ps file and finding lower left and upper right coordinates that are desirable for a bounding box. (The cursor coordinates are displayed in the upper left of gvv window. See them change as you move the cursor?). Note: to be consistent with the postscript coordinates, you will need to set "Coordinate units" to points in the ggv preferences. Encapsulated postscript files are most useful when used for figures within documents generated by "word processors" (LaTeX being the preferred one, of course!)

Now you try it. Can you edit art.eps and improve its artistic merit? You may want to learn more about black and white PostScript from A First Guide to Postscript. I did not find the advice about encapsulated postscript to be quite functional on my computers. It was recommended that the first line of a .eps file should be %!PS-Adobe EPSF-3.0. I found that %!PS-Adobe-2.0 EPSF-2.0 was needed.


Links


repairing the postscript made by others

Type which gnuplot. If you have it on your computer, continue with:

gnuplot
gnuplot> plot sin(x)
gnuplot> set term postscript eps color
gnuplot> set output 'sin.eps'
gnuplot> replot
gnuplot> quit 

Try ggv sin.eps. Then have a look at sin.eps with an editor. It may look rather complicated. Near the end of the file you should find lots of commands like:

64 -108 V
63 -199 V
64 -283 V
63 -355 V
64 -413 V
64 -453 V
63 -476 V

What is the "V"? Near the top of the file we see that "V" is defined to be the same as "rlineto" with the command:

/V {rlineto} bind def

There are some other abbreviations defined there also. Search for "LT0" and find the command:

/LT0 { PL [] 0 1 0 DL } def

The 0 1 0 are the RGB colors, intense green in this case. Search again to find "LT0" is used just before plotting sin(x) begins:

LT0
6486 4766 M
(sin\(x\)) Rshow
6570 4766 M
252 0 V
672 3884 M
64 -427 V

Things to try:

  • Try changing the color of LT0 by changing the three colors in the definition
  • Try "commenting out" the legend with a %
  • Try changing the linewidth by placing the command:
    30 setlinewidth
    

    right after LT0.

  • Try changing the size of the font in
    /Helvetica findfont 140 scalefont setfont
    

    (140 is scaled by statement 0.050 0.050 scale so the font size is 7 pts, quite small!)

  • Try the statement plot sin(x),cos(x) in gnuplot, and contend with dashed lines.

converting postscript graphics to raster graphics

With your sin.eps made with gnuplot, (preferably freshly made, before you did any editing experiments) try the following:
convert sin.eps sinc.png
convert +antialias sin.eps sinca.png
Compare the sizes of the files (ls -l *.png). Compare the appearance, for examples with eog. The difference may appear rather subtle. The +antialias option actually turned off the antialiasing, as you may read in the details about the antialias option, which is found in the online convert manual.

As an alternative to eog, you can try display. Left click on the image. In the menu that pops open, click on Miscelleny>>Image Info. You will be told that sinc.png has aproximately 64 colors, while sinca.png has three. All those extra colors are pinks and grays, used to soften the corner regions between sharply contrasting colored pixels (the red and white, the black and white). sinc.png looks better, but it is a larger file. Here is a short definition of antialias.

Note that gnuplot can make PNG files directly.

gnuplot
gnuplot> set term png color
gnuplot> set output 'direct.png'
gnuplot> plot sin(x)
gnuplot> quit 

If you are curious, you may want to investigate what the default antialias option is within gnuplot, by inspecting the properties of direct.png.


The task for Spring 2006 is to learn how to make maps with Python. You will need to visit vplot. Download vplot.py which is called by the demo vplotex.py. After familiarization with vplot, download:


this is an obsolete site
go to new site
go to obsolete home page

pix
Move to top of page