GNUPLOT

- not so Frequently Asked Questions -

update 2004/9/16

After Plotting (No.3)

1 | 2 | 3

How do I change colors in a PostScript figure ?

You cannot specify colors of lines and symbols directly from gnuplot. One of the easiest way to do is, make an obj file of Tgif by setting set term tgif , and edit your figure with Tgif, then save it in a color PS format.

Here is a tricky way to change the colors. Edit the postscript file by hand. The PS file generated by gnuplot is rather easy to read. With the following gnuplot commands you can get a color index. Of course the test command yields the color index, too.

gnuplot> set term postscript enhanced color
gnuplot> set output "colorindex.ps"
gnuplot> set size 0.5,0.5
gnuplot> set noborder
gnuplot> set nokey
gnuplot> set linestyle  1 linetype  1 linewidth 8
gnuplot> set linestyle  2 linetype  2 linewidth 8
gnuplot> set linestyle  3 linetype  3 linewidth 8
gnuplot> set linestyle  4 linetype  4 linewidth 8
gnuplot> set linestyle  5 linetype  5 linewidth 8
gnuplot> set linestyle  6 linetype  6 linewidth 8
gnuplot> set linestyle  7 linetype  7 linewidth 8
gnuplot> set linestyle  8 linetype  8 linewidth 8
gnuplot> set linestyle  9 linetype  9 linewidth 8
gnuplot> set linestyle 10 linetype 10 linewidth 8
gnuplot> # for gnuplot ver.4
gnuplot> # set style line 1 line type 1 linewidth 8
gnuplot> # ... etc
gnuplot> set noxtics
gnuplot> set ytics nomirror 1
gnuplot> set yrange [ -1.5 : 10.5 ]
gnuplot> plot  1 w l ls  1,  2 w l ls  2,  3 w l ls  3, 4 w l ls  4,\
gnuplot>       5 w l ls  5,  6 w l ls  6,  7 w l ls  7, 8 w l ls  8,\
gnuplot>       9 w l ls  9, 10 w l ls 10, -1 w line -1, 0 with line 0
gnuplot> pause -1

Ten line types are defined in the PostScript terminal, those are numbered from 1 to 9. Above, functions y=1 to y=10 are displayed with the various line types. In order to see the color clearly very thick lines are used there.

Do the same thing but with a B/W PostScript. Remove the color option, and save the output into "monoindex.ps". You can see the difference between "colorindex.ps" and "monoindex.ps" with the diff command on UNIX. The difference is only one line, /Color true def and /Color false def . Therefore you can choose color or B/W without gnuplot by editing this part /Color {true |false}.

Lets read the PS file generated above, you can easily find the place where colors are defined. Near line 40th, you see the following section (extra spaces are inserted here to align).

/LT0 {PL [                                         ] 1   0   0   DL} def
/LT1 {PL [4 dl 2   dl                              ] 0   1   0   DL} def
/LT2 {PL [2 dl 3   dl                              ] 0   0   1   DL} def
/LT3 {PL [1 dl 1.5 dl                              ] 1   0   1   DL} def
/LT4 {PL [5 dl 2   dl 1 dl 2 dl                    ] 0   1   1   DL} def
/LT5 {PL [4 dl 3   dl 1 dl 3 dl                    ] 1   1   0   DL} def
/LT6 {PL [2 dl 2   dl 2 dl 4 dl                    ] 0   0   0   DL} def
/LT7 {PL [2 dl 2   dl 2 dl 2 dl 2 dl 4 dl          ] 1   0.3 0   DL} def
/LT8 {PL [2 dl 2   dl 2 dl 2 dl 2 dl 2 dl 2 dl 4 dl] 0.5 0.5 0.5 DL} def

Those lines correspond to the line types 1 to 9. Three numbers in LT0, "1 0 0" defines RGB (Red, Green, and Blue), then LT0 becomes red. In the same manner, LT1 is green, LT2 is blue...and (1,0,1)=Magenta, (0,1,1)=Cyan, (1,1,0)=Yellow, (0,0,0)=Black, (1,0.3,0)=Orange, and (0.5,0.5,0.5)=Gray. Now you can change the line color by changing these RGB values.

To make a gradation from red to yellow, fix the R=1 and Blue=0, and change G from 0 to 1 gradually.

/LT0 {PL [                                         ] 1   0    0   DL} def
/LT1 {PL [4 dl 2   dl                              ] 1   0.1  0   DL} def
/LT2 {PL [2 dl 3   dl                              ] 1   0.2  0   DL} def
/LT3 {PL [1 dl 1.5 dl                              ] 1   0.3  0   DL} def
/LT4 {PL [5 dl 2   dl 1 dl 2 dl                    ] 1   0.4  0   DL} def
/LT5 {PL [4 dl 3   dl 1 dl 3 dl                    ] 1   0.5  0   DL} def
/LT6 {PL [2 dl 2   dl 2 dl 4 dl                    ] 1   0.6  0   DL} def
/LT7 {PL [2 dl 2   dl 2 dl 2 dl 2 dl 4 dl          ] 1   0.7  0   DL} def
/LT8 {PL [2 dl 2   dl 2 dl 2 dl 2 dl 2 dl 2 dl 4 dl] 1   0.8  0   DL} def

The results are as follows. The first image is generated with gnuplot, while the second one is made with editing the PS file as described above. With this method you can use any colors in you figure. An automatic conversion of those colors can be achieved with some programs like perl or sed.

fig/sample8.4a ColorPS generated by gnuplot
fig/sample8.4b PS file edited

Now, go back to the postscript data above, you can see "[4 dl 2 dl]" which defines a line pattern to draw dotted, dot-dashed, and dashed lines. For LT1, the pattern is defined as "[4 dl 2 dl]". This gives a solid line of 4 unit length and a space of 2 unit length, so that it becomes a dashed-line. "[5 dl 2 dl 1 dl 2 dl]" defines a solid line of 5 unit length, space of 2, line of 1, and space of 2, and so on. If you need an extra line pattern, edit here to make your own.

In the PS file you can find the following 2 lines, those are just above the definition of LT0.

/LTb { BL [] 0 0 0 DL } def
/LTa { AL [1 dl 2 dl] 0 setdash 0 0 0 setrgbcolor } def

The line LTb is used for the border of graph, and LTa is for the zero axes. Their color is black since RGB is "0 0 0". If you want to change those color, edit here.

up

I want to get rid of a right-side margin in a square figure.

A square figure can be drawn by set size square . However, an EPS file generated with gnuplot is not square but still rectangular, and you get extra margin. The reason is that gnuplot determines BoundingBox so as to cover the whole screen. In order to get rid of the right-side margin, you need to edit the EPS file by hand. The BoundingBox is defined at the top of the generated EPS file.

%%BoundingBox: 50 50 410 302

This "410" is too large in this case, then reduce it. An appropriate BoundingBox coordinate depends on the label and margin, so that you may find the best number after some try-and-errors. In the above case, about 320 works fine because the vertical size is 302.

If you are working on Unix or Linux, and your system has GhostScript, probably you have ps2ps, eps2eps commands. Those shell scripts convert your PostScript graphs generated by gnuplot into more proper PS file. You can adjust your BoundingBox with the following command.

% eps2eps input.eps output.eps
from Russell in Universiteit van Amsterdam. Thanks !
up