How to Reduce White Space Around Figure?

Dear All,
I am aware that this question has already been asked several times on the mailing list, see e.g.

http://bit.ly/aPzQTA

However, in the following snippet, nothing I tried has been able to reduce the amount of white space around the figure (including toying around with

ax = plt.axes([0.0, 0.0, 1.0, 1.0])
)
Of course, one can always resort to pdfcrop, but I believe there must be a better solution to resize the margins from matplotlib.
Please see the snippet at the end of the email.
Every suggestion is welcome.
Cheers

Lorenzo

##############################################################################3

#!/usr/bin/env python
"""
See pcolor_demo2 for a much faster way of generating pcolor plots
"""
from __future__ import division
from pylab import *

from matplotlib import rc

def func3(x,y):
    return (1- x/2 + x**5 + y**3)*exp(-x**2-y**2)

def func4(x,y):
    theta=arcsin(y)
    return cos(theta)

def func5(x,y):

    return abs(sin(y))

def func6(x,y):

    return abs(cos(y))

# make these smaller to increase the resolution
dx, dy = 0.0025, 0.0025

# x = arange(-1.0, 1.0, dx)
# y = arange(-1.0, 1.0, dy)

x = arange(-pi, pi, dx)
y = arange(-pi, pi, dy)

print("x is, " )

print (x)

X,Y = meshgrid(x, y)

Z = func6(X, Y)

# print "Z is, ", Z

ini=pi/2.+0.5

fig = plt.figure(figsize=(6,6))

ax = fig.add_subplot(111)

ax.axis('off')

figtext(.55, .8,r'${\bf J}^\perp$', fontdict=None,fontsize=30)

im = imshow(Z,cmap=cm.jet, extent=(-pi, pi, -pi, pi))
im.set_interpolation('bilinear')

im.set_clip_path(Circle((0,0),pi/2., transform=ax.transData))

# ax = plt.axes([0.0, 0.0, 1.0, 1.0]) # leaves no white space around the axes

annotate("", xy=(-pi/2., 0), xytext=(-ini, 0), arrowprops=dict(fc="g"))
annotate("", xy=(-pi/2., .2), xytext=(-ini, .2), arrowprops=dict(fc="g"))
annotate("", xy=(-pi/2., -.2), xytext=(-ini, -.2), arrowprops=dict(fc="g"))

annotate("", xy=(-pi/2., .4), xytext=(-ini, .4), arrowprops=dict(fc="g"))
annotate("", xy=(-pi/2., -.4), xytext=(-ini, -.4), arrowprops=dict(fc="g"))

annotate("", xy=(-pi/2., .6), xytext=(-ini, .6), arrowprops=dict(fc="g"))
annotate("", xy=(-pi/2., -.6), xytext=(-ini, -.6), arrowprops=dict(fc="g"))
annotate("", xy=(-pi/2., .8), xytext=(-ini, .8), arrowprops=dict(fc="g"))
annotate("", xy=(-pi/2., -.8), xytext=(-ini, -.8), arrowprops=dict(fc="g"))

annotate("", xy=(-pi/2., 1), xytext=(-ini, 1), arrowprops=dict(fc="g"))
annotate("", xy=(-pi/2., -1), xytext=(-ini, -1), arrowprops=dict(fc="g"))

annotate("", xy=(-pi/2., 1.2), xytext=(-ini, 1.2), arrowprops=dict(fc="g"))
annotate("", xy=(-pi/2., -1.2), xytext=(-ini, -1.2), arrowprops=dict(fc="g"))

annotate("", xy=(-pi/2., 1.4), xytext=(-ini, 1.4), arrowprops=dict(fc="g"))
annotate("", xy=(-pi/2., -1.4), xytext=(-ini, -1.4), arrowprops=dict(fc="g"))

annotate("", xy=(pi/2., 0), xytext=(ini, 0), arrowprops=dict(fc="g"))
annotate("", xy=(pi/2., .2), xytext=(ini, .2), arrowprops=dict(fc="g"))
annotate("", xy=(pi/2., -.2), xytext=(ini, -.2), arrowprops=dict(fc="g"))

annotate("", xy=(pi/2., .4), xytext=(ini, .4), arrowprops=dict(fc="g"))
annotate("", xy=(pi/2., -.4), xytext=(ini, -.4), arrowprops=dict(fc="g"))

annotate("", xy=(pi/2., .6), xytext=(ini, .6), arrowprops=dict(fc="g"))
annotate("", xy=(pi/2., -.6), xytext=(ini, -.6), arrowprops=dict(fc="g"))
annotate("", xy=(pi/2., .8), xytext=(ini, .8), arrowprops=dict(fc="g"))
annotate("", xy=(pi/2., -.8), xytext=(ini, -.8), arrowprops=dict(fc="g"))

annotate("", xy=(pi/2., 1), xytext=(ini, 1), arrowprops=dict(fc="g"))
annotate("", xy=(pi/2., -1), xytext=(ini, -1), arrowprops=dict(fc="g"))

annotate("", xy=(pi/2., 1.2), xytext=(ini, 1.2), arrowprops=dict(fc="g"))
annotate("", xy=(pi/2., -1.2), xytext=(ini, -1.2), arrowprops=dict(fc="g"))

annotate("", xy=(pi/2., 1.4), xytext=(ini, 1.4), arrowprops=dict(fc="g"))
annotate("", xy=(pi/2., -1.4), xytext=(ini, -1.4), arrowprops=dict(fc="g"))

# annotate("", xy=( -1.4, -pi/2), xytext=(-1.4,ini), arrowprops=dict(fc="g"))

annotate("", xy=(0., ini+1.), xytext=(0, -ini), arrowprops=dict(fc="black"))

ax.annotate('', xy=(-.3, 2.4), xycoords='data',
                 xytext=(-.4, 2.2), # textcoords='offset points',
                 arrowprops=dict(arrowstyle="->",
connectionstyle="angle3,angleA=0,angleB=-10"),
                 )

savefig("first-plot.pdf")

clf()

Lorenzo,

Have you tried this:

savefig(“first-plot.pdf”, bbox_inches=‘tight’)

It might not work properly if you have annotations outside the plot area, but give it a shot to see if that works for you. If not, there are some other “tricks” that might work.

Ben Root

···

On Mon, Oct 25, 2010 at 11:56 AM, Lorenzo Isella <lorenzo.isella@…287…> wrote:

Dear All,

I am aware that this question has already been asked several times on

the mailing list, see e.g.

http://bit.ly/aPzQTA

However, in the following snippet, nothing I tried has been able to

reduce the amount of white space around the figure (including toying

around with

ax = plt.axes([0.0, 0.0, 1.0, 1.0])

)

Of course, one can always resort to pdfcrop, but I believe there must be

a better solution to resize the margins from matplotlib.

Please see the snippet at the end of the email.

Every suggestion is welcome.

Cheers

Lorenzo

##############################################################################3

#!/usr/bin/env python

“”"

See pcolor_demo2 for a much faster way of generating pcolor plots

“”"

from future import division

from pylab import *

from matplotlib import rc

def func3(x,y):

return (1- x/2 + x**5 + y**3)*exp(-x**2-y**2)

def func4(x,y):

theta=arcsin(y)

return cos(theta)

def func5(x,y):

return abs(sin(y))

def func6(x,y):

return abs(cos(y))

make these smaller to increase the resolution

dx, dy = 0.0025, 0.0025

x = arange(-1.0, 1.0, dx)

y = arange(-1.0, 1.0, dy)

x = arange(-pi, pi, dx)

y = arange(-pi, pi, dy)

print("x is, " )

print (x)

X,Y = meshgrid(x, y)

Z = func6(X, Y)

print "Z is, ", Z

ini=pi/2.+0.5

fig = plt.figure(figsize=(6,6))

ax = fig.add_subplot(111)

ax.axis(‘off’)

figtext(.55, .8,r’${\bf J}^\perp$’, fontdict=None,fontsize=30)

im = imshow(Z,cmap=cm.jet, extent=(-pi, pi, -pi, pi))

im.set_interpolation(‘bilinear’)

im.set_clip_path(Circle((0,0),pi/2., transform=ax.transData))

ax = plt.axes([0.0, 0.0, 1.0, 1.0]) # leaves no white space around the

axes

annotate("", xy=(-pi/2., 0), xytext=(-ini, 0), arrowprops=dict(fc=“g”))

annotate("", xy=(-pi/2., .2), xytext=(-ini, .2), arrowprops=dict(fc=“g”))

annotate("", xy=(-pi/2., -.2), xytext=(-ini, -.2), arrowprops=dict(fc=“g”))

annotate("", xy=(-pi/2., .4), xytext=(-ini, .4), arrowprops=dict(fc=“g”))

annotate("", xy=(-pi/2., -.4), xytext=(-ini, -.4), arrowprops=dict(fc=“g”))

annotate("", xy=(-pi/2., .6), xytext=(-ini, .6), arrowprops=dict(fc=“g”))

annotate("", xy=(-pi/2., -.6), xytext=(-ini, -.6), arrowprops=dict(fc=“g”))

annotate("", xy=(-pi/2., .8), xytext=(-ini, .8), arrowprops=dict(fc=“g”))

annotate("", xy=(-pi/2., -.8), xytext=(-ini, -.8), arrowprops=dict(fc=“g”))

annotate("", xy=(-pi/2., 1), xytext=(-ini, 1), arrowprops=dict(fc=“g”))

annotate("", xy=(-pi/2., -1), xytext=(-ini, -1), arrowprops=dict(fc=“g”))

annotate("", xy=(-pi/2., 1.2), xytext=(-ini, 1.2), arrowprops=dict(fc=“g”))

annotate("", xy=(-pi/2., -1.2), xytext=(-ini, -1.2),

arrowprops=dict(fc=“g”))

annotate("", xy=(-pi/2., 1.4), xytext=(-ini, 1.4), arrowprops=dict(fc=“g”))

annotate("", xy=(-pi/2., -1.4), xytext=(-ini, -1.4),

arrowprops=dict(fc=“g”))

annotate("", xy=(pi/2., 0), xytext=(ini, 0), arrowprops=dict(fc=“g”))

annotate("", xy=(pi/2., .2), xytext=(ini, .2), arrowprops=dict(fc=“g”))

annotate("", xy=(pi/2., -.2), xytext=(ini, -.2), arrowprops=dict(fc=“g”))

annotate("", xy=(pi/2., .4), xytext=(ini, .4), arrowprops=dict(fc=“g”))

annotate("", xy=(pi/2., -.4), xytext=(ini, -.4), arrowprops=dict(fc=“g”))

annotate("", xy=(pi/2., .6), xytext=(ini, .6), arrowprops=dict(fc=“g”))

annotate("", xy=(pi/2., -.6), xytext=(ini, -.6), arrowprops=dict(fc=“g”))

annotate("", xy=(pi/2., .8), xytext=(ini, .8), arrowprops=dict(fc=“g”))

annotate("", xy=(pi/2., -.8), xytext=(ini, -.8), arrowprops=dict(fc=“g”))

annotate("", xy=(pi/2., 1), xytext=(ini, 1), arrowprops=dict(fc=“g”))

annotate("", xy=(pi/2., -1), xytext=(ini, -1), arrowprops=dict(fc=“g”))

annotate("", xy=(pi/2., 1.2), xytext=(ini, 1.2), arrowprops=dict(fc=“g”))

annotate("", xy=(pi/2., -1.2), xytext=(ini, -1.2), arrowprops=dict(fc=“g”))

annotate("", xy=(pi/2., 1.4), xytext=(ini, 1.4), arrowprops=dict(fc=“g”))

annotate("", xy=(pi/2., -1.4), xytext=(ini, -1.4), arrowprops=dict(fc=“g”))

annotate("", xy=( -1.4, -pi/2), xytext=(-1.4,ini),

arrowprops=dict(fc=“g”))

annotate("", xy=(0., ini+1.), xytext=(0, -ini), arrowprops=dict(fc=“black”))

ax.annotate(’’, xy=(-.3, 2.4), xycoords=‘data’,

             xytext=(-.4, 2.2), # textcoords='offset points',

             arrowprops=dict(arrowstyle="->",

connectionstyle=“angle3,angleA=0,angleB=-10”),

             )

savefig(“first-plot.pdf”)

clf()

Hi,
And thanks for your suggestion. The improvement (if any) if unfortunately rather modest.
I'd like to hear about the other "tricks".
Cheers

Lorenzo

···

On 10/25/2010 11:36 PM, Benjamin Root wrote:

, bbox_inches='tight'

[cut out code snippet]

You can always use subplots_adjust. I haven't looked into the details of your code, but it appears as though the actual plot (the actual graphics) is well within the margins of your subplot (extending the boundaries of the subplot would still leave a lot of white space). To counteract this you can use negative padding (and padding greater than 1); e.g.

subplots_adjust(top=1, bottom=-0.2, left=-0.3, right=1.3)

(you can add this right before "savefig".) This means that the actual boundaries of the subplot extend outside the figure (which normally have extents from 0 to 1). The above gives pretty good results. To get any better, I think you need to adjust the aspect ratio of the figure to match the plot (you can do this by creating a "figure" and passing a value for "figsize").

-Tony

P.S. since you posted code, I'll offer an unsolicited suggestion. :slight_smile: You can replace all your annotate commands (except for the last 2) with two short loops:

for y in np.arange(-1.4, 1.5, 0.2):
    annotate("", xy=(-pi/2., y), xytext=(-ini, y), arrowprops=dict(fc="g"))
for y in np.arange(-1.4, 1.5, 0.2):
    annotate("", xy=(pi/2., y), xytext=(ini, y), arrowprops=dict(fc="g"))

···

On Oct 25, 2010, at 12:56 PM, Lorenzo Isella wrote:

Dear All,
I am aware that this question has already been asked several times on
the mailing list, see e.g.

http://bit.ly/aPzQTA

However, in the following snippet, nothing I tried has been able to
reduce the amount of white space around the figure (including toying
around with

ax = plt.axes([0.0, 0.0, 1.0, 1.0])
)
Of course, one can always resort to pdfcrop, but I believe there must be
a better solution to resize the margins from matplotlib.
Please see the snippet at the end of the email.
Every suggestion is welcome.
Cheers

Lorenzo

Thanks.
This indeed improves the situation (and your suggestions about a for loop is appreciated).
I am surprised at the fact that I have been able to generate such a plot via scripting only (well, with a lot of help from the list) by browsing the online examples and with my limited knowledge of matplotlib.
Keep up the good work.
Cheers

Lorenzo

···

On 10/26/2010 06:07 AM, Tony S Yu wrote:

On Oct 25, 2010, at 12:56 PM, Lorenzo Isella wrote:

Dear All,
I am aware that this question has already been asked several times on
the mailing list, see e.g.

http://bit.ly/aPzQTA

However, in the following snippet, nothing I tried has been able to
reduce the amount of white space around the figure (including toying
around with

ax = plt.axes([0.0, 0.0, 1.0, 1.0])
)
Of course, one can always resort to pdfcrop, but I believe there must be
a better solution to resize the margins from matplotlib.
Please see the snippet at the end of the email.
Every suggestion is welcome.
Cheers

Lorenzo

[cut out code snippet]

You can always use subplots_adjust. I haven't looked into the details of your code, but it appears as though the actual plot (the actual graphics) is well within the margins of your subplot (extending the boundaries of the subplot would still leave a lot of white space). To counteract this you can use negative padding (and padding greater than 1); e.g.

subplots_adjust(top=1, bottom=-0.2, left=-0.3, right=1.3)

(you can add this right before "savefig".) This means that the actual boundaries of the subplot extend outside the figure (which normally have extents from 0 to 1). The above gives pretty good results. To get any better, I think you need to adjust the aspect ratio of the figure to match the plot (you can do this by creating a "figure" and passing a value for "figsize").

-Tony

P.S. since you posted code, I'll offer an unsolicited suggestion. :slight_smile: You can replace all your annotate commands (except for the last 2) with two short loops:

for y in np.arange(-1.4, 1.5, 0.2):
     annotate("", xy=(-pi/2., y), xytext=(-ini, y), arrowprops=dict(fc="g"))
for y in np.arange(-1.4, 1.5, 0.2):
     annotate("", xy=(pi/2., y), xytext=(ini, y), arrowprops=dict(fc="g"))

the "bbox_inches" option does not work well for your case since the
axes frame (although not visible) occupy much larger area than your
plot elements. Make axis limits more tighter, then try to use
bbox_inches option (with this, you don't need to fiddle with subplot
params).

Here is my try.

ax.set_xlim(-ini, ini)
ax.set_ylim(-ini, ini+1)
ax.set_xticks([]) # this is needed for bbox_inches
ax.set_yticks([])

savefig("first-plot.pdf", bbox_inches="tight")

Regards,

-JJ

···

On Tue, Oct 26, 2010 at 7:26 PM, Lorenzo Isella <lorenzo.isella@...287...> wrote:

Thanks.
This indeed improves the situation (and your suggestions about a for
loop is appreciated).
I am surprised at the fact that I have been able to generate such a plot
via scripting only (well, with a lot of help from the list) by browsing
the online examples and with my limited knowledge of matplotlib.
Keep up the good work.
Cheers

Lorenzo

On 10/26/2010 06:07 AM, Tony S Yu wrote:

On Oct 25, 2010, at 12:56 PM, Lorenzo Isella wrote:

Dear All,
I am aware that this question has already been asked several times on
the mailing list, see e.g.

http://bit.ly/aPzQTA

However, in the following snippet, nothing I tried has been able to
reduce the amount of white space around the figure (including toying
around with

ax = plt.axes([0.0, 0.0, 1.0, 1.0])
)
Of course, one can always resort to pdfcrop, but I believe there must be
a better solution to resize the margins from matplotlib.
Please see the snippet at the end of the email.
Every suggestion is welcome.
Cheers

Lorenzo

[cut out code snippet]

You can always use subplots_adjust. I haven't looked into the details of your code, but it appears as though the actual plot (the actual graphics) is well within the margins of your subplot (extending the boundaries of the subplot would still leave a lot of white space). To counteract this you can use negative padding (and padding greater than 1); e.g.

subplots_adjust(top=1, bottom=-0.2, left=-0.3, right=1.3)

(you can add this right before "savefig".) This means that the actual boundaries of the subplot extend outside the figure (which normally have extents from 0 to 1). The above gives pretty good results. To get any better, I think you need to adjust the aspect ratio of the figure to match the plot (you can do this by creating a "figure" and passing a value for "figsize").

-Tony

P.S. since you posted code, I'll offer an unsolicited suggestion. :slight_smile: You can replace all your annotate commands (except for the last 2) with two short loops:

for y in np.arange(-1.4, 1.5, 0.2):
annotate("", xy=(-pi/2., y), xytext=(-ini, y), arrowprops=dict(fc="g"))
for y in np.arange(-1.4, 1.5, 0.2):
annotate("", xy=(pi/2., y), xytext=(ini, y), arrowprops=dict(fc="g"))

------------------------------------------------------------------------------
Nokia and AT&T present the 2010 Calling All Innovators-North America contest
Create new apps & games for the Nokia N8 for consumers in U.S. and Canada
$10 million total in prizes - $4M cash, 500 devices, nearly $6M in marketing
Develop with Nokia Qt SDK, Web Runtime, or Java and Publish to Ovi Store
http://p.sf.net/sfu/nokia-dev2dev
_______________________________________________
Matplotlib-users mailing list
Matplotlib-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/matplotlib-users