Thursday, March 12, 2009

GNUPlot: How to remove the blank margin space on the left

When you plot a figure using gnuplot, sometimes, there will be an extra blank margin on the left. To make the figure more condense, you can use a bash script to remove the extra blank margin. The basic idea is to adjust the Bounding Box for eps more intelligently.

Usage:
bash fixbb *.eps

The original image may look like this

The original eps file will be replaced with the adjusted eps file

The new eps file may look like this.




You can download this script from here.
Or, copy the following lines into a file named "fixbb" or something else whatever you like.

You can also copy this file (Assume you are using Linux)
to /usr/local/bin
and run "sudo chmod +x /usr/local/bin/fixbb"
Then in your terminal, you can directly run
"fixbb *.eps"

This script helps us a lot for our paper.

#!/bin/bash

# This is fixbb version 0.31. Copyright 2000 by Jeff Spirko.
# Redistributable under the GNU General Public License.
# See http://topquark.dhs.org/~spirko/fixbb/

# Changes by Petr Mikulik:
# - 8. 9. 2000: working file in the $TMP directory; remove backup file
# - 18. 10. 2000: AWKPARSEBB to support multiple-page (non-EPS) documents
# - 26. 10. 2000: error if bounding box not determined (GS < gsopts="-dQUIET -dBATCH -dNOPAUSE -sDEVICE=bbox" awkprog="'{" 1 ="=" awkprog="'" 1 ="=" found="1;" awkparsebb="'" x1="99999;" y1="99999;" x2="0;" y2="0;" page="0">$2 { x1=$2 }
y1>$3 { y1=$3 }
x2<$4 { x2=$4 } y2<$5 { y2=$5 } { page++; printf "Page " page ": " $0 " max = " x1 " " y1 " " x2 " " y2 " \t\r" >"/dev/stderr" }
END { printf "\t\t\t\t\t\t\t\r" >"/dev/stderr"
print x1 " " y1 " " x2 " " y2 }
'

export bbox

# Each command-line argument is assumed to be a file to process
for FILE in $* ; do

# BAK=${FILE}~
# if [ "$TMP" = "" ]; then TMP="."; fi; BAK=$TMP\fixbb__.ps
BAK='fixbb$$.ps'

# Get the correct BoundingBox from ghostscript.
# Old: bbox=`gs ${GSOPTS} ${FILE} 2>&1 | gsort | head -1 | cut -d\ -f 2-`
bbox=`/usr/bin/gs ${GSOPTS} ${FILE} 2>&1 | awk "$AWKPARSEBB"`

case $bbox in
"99999 99999 0 0")
echo "Bounding box was NOT determined (empty pages or Ghostscript >= 5.50 required)"
exit ;;
esac

echo "Bounding box for \""$FILE"\" is: "$bbox

# Save the old version of the file
mv $FILE $BAK

# Create the new version, replacing the old BoundingBox
awk "$AWKPROG" $BAK > $FILE

case `uname` in
"OS/2") dos2unix $FILE ;;
esac

# delete the backup file
rm -f $BAK

done

14 comments:

lsfeel0204 said...

This is awesome! Thanks alot!

Anonymous said...

i`m using wgnuplot on Windows Xp. I wanted to remove to extra white space unbounded by the Bounding Box. Can you help me?

t3od0r said...

That's nice. Thanks! I'm trying to reduce the white space between the title and the plot. Do you know how to do that?

Peter said...

Nice one! I've spent ages trying to adjust bounding boxes by hand, and I could never get it right. This script works like a charm. Thanks for making it available

Anonymous said...

This is very very helpful! I have tried it for the eps figure generated by other apps, it works well!

Anonymous said...

Thanks!
Just what I was looking for. Works like a charm.

Rachel said...

Thank you! That was very useful.

Anonymous said...

Thanks alot!

Unknown said...

works perfect! thanks!

Thomas Edwards said...

I get "Bounding box was NOT determined (empty pages or Ghostscript >= 5.50 required)"
when I try to use it. How do I fix this problem? I'm pretty sure I have at least version 8 of ghostscript

Carlos Completo said...

simply GREAT! :D

Anonymous said...

Thanks!

Anonymous said...

Thanks!

black_knight said...

i can not wait to read far more from you. this is actually terrific site.

www.n8fan.net