preventing extra whitespace around figure

Does anyone know of a way of preventing the generation of extra whitespace around the edge of a figure? The code sample below demonstrates the problem I’m having - I’ve tried postprocessing the postscript output with ImageMagick’s “mogrify -trim”, but unfortunately it makes it into a raster image in the process, and I really need the output to stay in vector postscript.

···

#!/usr/bin/python

from matplotlib.toolkits.basemap import Basemap

from pylab import *

clat = 21.813100

clon = 120.529800

xmin=117.629800

ymin=19.079100

xmax=123.546467

ymax=24.579100

bounds = [xmin,xmax,ymin,ymax]

figwidth = 5.4

dx = bounds[1] - bounds[0]

dy = bounds[3] - bounds[2]

clat = bounds[2] + (bounds[3] - bounds[2])/2

clon = bounds[0] + (bounds[1] - bounds[0])/2

aspect = dy/dx

figheight = aspect * figwidth

fig = figure(figsize=(figwidth,figheight))

ax1 = fig.add_axes([0,0,0.98,0.98])

m = Basemap(llcrnrlon=xmin,llcrnrlat=ymin,urcrnrlon=xmax,urcrnrlat=ymax,

rsphere=(6378137.00,6356752.3142),

resolution=‘h’,projection=‘merc’,

lat_ts=clat)

water_color = [.47,.60,.81]

m.drawrivers(color=water_color)

m.drawcoastlines(linewidth=0.1)

#draw inset map

ax2 = fig.add_axes((0.1,0.1,0.25,0.25))

map = Basemap(resolution=‘l’,

projection=‘ortho’,

lon_0=clon,lat_0=clat,ax=ax2)

print ‘map created.’

map.drawcountries(linewidth=0.1,color=[0.2,0.2,0.2])

map.drawcoastlines(linewidth=0.05,color=[0.2,0.2,0.2])

map.drawlsmask((230,230,230,255),(119,155,207,255))

meridians = arange(-180,210,30)

parallels = arange(-90,120,30)

map.drawmeridians(meridians,linewidth=0.1,dashes=[1,0],color=[0.2,0.2,0.2])

map.drawparallels(parallels,linewidth=0.1,dashes=[1,0],color=[0.2,0.2,0.2])

pcx,pcy = map(clon,clat)

print ‘Lat: %f, Lon: %f’ % (clat,clon)

map.plot(array([pcx]),array([pcy]),‘rD’,linewidth=2,markersize=5)

map.drawmapboundary(color=‘k’,linewidth=2.0)

savefig(‘maptest.eps’)

close(‘all’)



Michael Hearne

mhearne@…924…

(303) 273-8620

USGS National Earthquake Information Center

1711 Illinois St. Golden CO 80401

Senior Software Engineer

Synergetics, Inc.


I think I recall that eps2eps can reset your bounding box,
if that's the problem.
http://www.linuxcommand.org/man_pages/eps2eps1.html

Cheers,
Alan Isaac

I gave this a shot, and eps2eps seems not to have any effect on the bounding box. I’ve done some experiments where I reduce the bounding box by hand, which works really well - the only problem is I need a way to determine where the edge of my plot really is.

In Basemap, there is a box drawn around my map. I’m pretty sure this is just the axis border, and I need to find a way to determine which of the many drawing commands in the postscript file represents this border.

Is there a way to set the color of this plot box in matplotlib, so that I can use that as a clue in the postscript?

Thanks,

Mike

···

On Feb 29, 2008, at 4:38 AM, Alan G Isaac wrote:

I think I recall that eps2eps can reset your bounding box,

if that’s the problem.

http://www.linuxcommand.org/man_pages/eps2eps1.html

Cheers,

Alan Isaac


This SF.net email is sponsored by: Microsoft

Defy all challenges. Microsoft(R) Visual Studio 2008.

http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/


Matplotlib-users mailing list

Matplotlib-users@lists.sourceforge.net

https://lists.sourceforge.net/lists/listinfo/matplotlib-users


Michael Hearne

mhearne@…924…

(303) 273-8620

USGS National Earthquake Information Center

1711 Illinois St. Golden CO 80401

Senior Software Engineer

Synergetics, Inc.


Hmmm. That is just what eps2eps should do.
Perhaps there is something drawn outside what
you believe your picture is?

Here are more possibilities:
http://www.physics.ohio-state.edu/cgi-bin/fom?_recurse=1&file=103#file_105

Some Python code to exploit this GS ability is here:

Cheers,
Alan Isaac

···

On Fri, 29 Feb 2008, Michael Hearne apparently wrote:

I gave this a shot, and eps2eps seems not to have any effect on the
bounding box. I've done some experiments where I reduce the bounding
box by hand, which works really well - the only problem is I need a
way to determine where the edge of my plot really is.

Have you tried pdfcrop ? It computes margin automatically, you could then transfer the file back to eps.

David

PDFCROP 1.5, 2004/06/24 - Copyright (c) 2002, 2004 by Heiko Oberdiek.
Syntax: pdfcrop [options] <input[.pdf]> [output file]
Function: Margins are calculated and removed for each page in the file.
Options: (defaults:)
–help print usage
–(no)verbose verbose printing (false)
–(no)debug debug informations (false)
–gscmd call of ghostscript (gs)
–pdftexcmd call of pdfTeX (pdftex)
–margins “ ” (0 0 0 0)
add extra margins, unit is bp. If only one number is
given, then it is used for all margins, in the case
of two numbers they are also used for right and bottom.
–(no)clip clipping support, if margins are set (false)
–(no)hires using %%HiResBoundingBox' (false) instead of %%BoundingBox’
–papersize parameter for gs’s -sPAPERSIZE=,
use only with older gs versions <7.32 ()
Examples:
pdfcrop --margins 10 input.pdf output.pdf
pdfcrop --margins ‘5 10 5 20’ --clip input.pdf output.pdf

2008/2/29, Alan G Isaac <aisaac@…310…>:

···

On Fri, 29 Feb 2008, Michael Hearne apparently wrote:

I gave this a shot, and eps2eps seems not to have any effect on the
bounding box. I’ve done some experiments where I reduce the bounding
box by hand, which works really well - the only problem is I need a

way to determine where the edge of my plot really is.

Hmmm. That is just what eps2eps should do.
Perhaps there is something drawn outside what
you believe your picture is?

Here are more possibilities:

http://www.physics.ohio-state.edu/cgi-bin/fom?_recurse=1&file=103#file_105

Some Python code to exploit this GS ability is here:

http://citadel.tistory.com/130

Cheers,
Alan Isaac


This SF.net email is sponsored by: Microsoft

Defy all challenges. Microsoft(R) Visual Studio 2008.
http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/


Matplotlib-users mailing list
Matplotlib-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/matplotlib-users

Did not know about it.
<URL:http://www.ctan.org/tex-archive/support/pdfcrop/&gt;
Hmmm, is it legitimate to link to perl code here? :wink:

Thanks,
Alan

···

On Fri, 29 Feb 2008, David Huard apparently wrote:

Have you tried pdfcrop ?