|
From: mdekauwe <mde...@gm...> - 2010-06-08 23:47:46
|
Hi, when I make plots with the Basemap package I seem to be getting a large amount of white space at the top and bottom of the plot, even if I use the option bbox_inches='tight'. Does anyone know how I can stop this happening? Example image... http://old.nabble.com/file/p28824528/diffmap_JULES_DJF_snowFrac.GSWP2_vs_SSMI.jpg What I am currently doing... def makeMap(image, ofname, imageMin, imageMax, ticks): """ dump out the array as a nice map... """ fig = plt.figure(figsize=(8, 6)) m = Basemap(projection='cyl', llcrnrlon=-179.5, llcrnrlat=-60.0, \ urcrnrlon=179.5, urcrnrlat=89.5, resolution='c') # add an axes, leaving room for colorbar on the right. ax = fig.add_axes([0.1, 0.1, 0.7, 0.7]) m.ax = ax colourmap = plt.cm.RdYlBu colourmap.set_bad('w', 1.0) im = m.imshow(image, colourmap, colors.Normalize(vmin=imageMin, vmax=imageMax, clip=False),\ interpolation='nearest') pos = ax.get_position() l, b, w, h = pos.bounds cax = plt.axes([l + w + 0.045, b, 0.05, h]) norm = colors.BoundaryNorm(ticks, colourmap.N) cbar = mpl.colorbar.ColorbarBase(cax, cmap=colourmap, norm=norm, ticks=ticks) #cbar = plt.colorbar(im, cax=cax) # xlabel and ylabel were overwriting the tick labels so i need to move them # transAxes set the position from the bottom left of the axes (1,1 would be the top right) ax.text(0.5, -0.15, 'Longitude', transform=ax.transAxes, ha='center', va='center') ax.text(-0.1, 0.5, 'Latitude', transform=ax.transAxes, ha= 'center', va='center', rotation='vertical') #ax.set_title("Sign.Different: GSPW2 - Princeton") m.drawcoastlines(linewidth=0.2, color='k') m.drawcountries(linewidth=0.1, color='k') #m.drawrivers() m.drawparallels(np.array([-90.0, -60.0, -30.0, 0.0, 30.0, 60.0, 90.0]), labels=[1,0,0,1],\ fontsize=10, rotation=20) m.drawmeridians(np.array([-180.0, -120.0, -60.0, 0.0, 60.0, 120.0, 180.0]), labels=[0,0,0,1], \ fontsize=10, rotation=20) fig.savefig(ofname + '.png', dpi=300, bbox_inches='tight') # for LaTeX doc... fig.savefig(os.path.join("/users/eow/mgdk/doc/snowy_notes/figures/", (ofname + ".eps")), dpi=300,\ bbox_inches='tight') Many thanks, Martin -- View this message in context: http://old.nabble.com/Removing-white-space-surrounding-plots-tp28824528p28824528.html Sent from the matplotlib - users mailing list archive at Nabble.com. |
|
From: mdekauwe <mde...@gm...> - 2010-06-08 23:50:12
|
Note - in case it isn't clear that white space between where I mention the example and the image is what I am referring to and below it again. mdekauwe wrote: > > Hi, > > when I make plots with the Basemap package I seem to be getting a large > amount of white space at the top and bottom of the plot, even if I use the > option bbox_inches='tight'. Does anyone know how I can stop this > happening? > > Example image... > > http://old.nabble.com/file/p28824528/diffmap_JULES_DJF_snowFrac.GSWP2_vs_SSMI.jpg > > What I am currently doing... > > def makeMap(image, ofname, imageMin, imageMax, ticks): > """ dump out the array as a nice map... """ > fig = plt.figure(figsize=(8, 6)) > m = Basemap(projection='cyl', llcrnrlon=-179.5, llcrnrlat=-60.0, \ > urcrnrlon=179.5, urcrnrlat=89.5, resolution='c') > # add an axes, leaving room for colorbar on the right. > ax = fig.add_axes([0.1, 0.1, 0.7, 0.7]) > m.ax = ax > colourmap = plt.cm.RdYlBu > colourmap.set_bad('w', 1.0) > im = m.imshow(image, colourmap, colors.Normalize(vmin=imageMin, > vmax=imageMax, clip=False),\ > interpolation='nearest') > pos = ax.get_position() > l, b, w, h = pos.bounds > cax = plt.axes([l + w + 0.045, b, 0.05, h]) > norm = colors.BoundaryNorm(ticks, colourmap.N) > cbar = mpl.colorbar.ColorbarBase(cax, cmap=colourmap, norm=norm, > ticks=ticks) > #cbar = plt.colorbar(im, cax=cax) > # xlabel and ylabel were overwriting the tick labels so i need to move > them > # transAxes set the position from the bottom left of the axes (1,1 > would be the top right) > ax.text(0.5, -0.15, 'Longitude', transform=ax.transAxes, ha='center', > va='center') > ax.text(-0.1, 0.5, 'Latitude', transform=ax.transAxes, ha= 'center', > va='center', rotation='vertical') > #ax.set_title("Sign.Different: GSPW2 - Princeton") > m.drawcoastlines(linewidth=0.2, color='k') > m.drawcountries(linewidth=0.1, color='k') > #m.drawrivers() > m.drawparallels(np.array([-90.0, -60.0, -30.0, 0.0, 30.0, 60.0, > 90.0]), labels=[1,0,0,1],\ > fontsize=10, rotation=20) > m.drawmeridians(np.array([-180.0, -120.0, -60.0, 0.0, 60.0, 120.0, > 180.0]), labels=[0,0,0,1], \ > fontsize=10, rotation=20) > fig.savefig(ofname + '.png', dpi=300, bbox_inches='tight') > # for LaTeX doc... > fig.savefig(os.path.join("/users/eow/mgdk/doc/snowy_notes/figures/", > (ofname + ".eps")), dpi=300,\ > bbox_inches='tight') > > Many thanks, > > Martin > -- View this message in context: http://old.nabble.com/Removing-white-space-surrounding-plots-tp28824528p28824544.html Sent from the matplotlib - users mailing list archive at Nabble.com. |
|
From: Jae-Joon L. <lee...@gm...> - 2010-06-09 18:29:19
|
What version of matplotlib are you using?
With 0.99.1.1 (and likely later) and also with the svn version, using
the bbox_inches="tight" actually produces an output with too small
margin.
With the svn version, following code should generate an output with an
adequate margin.
Otherwise, you cannot rely on bbox_inches features. Instead, you have
to manually adjust the size of the figure (and subplots_adjust).
Regards,
-JJ
t1=ax.text(0.5, -0.15, 'Longitude', transform=ax.transAxes,
ha='center', va='center')
t2=ax.text(-0.1, 0.5, 'Latitude', transform=ax.transAxes, ha=
'center', va='center', rotation='vertical')
#ax.set_title("Sign.Different: GSPW2 - Princeton")
fig.savefig('test.png', dpi=300, bbox_inches='tight',
bbox_extra_artists=[t1, t2])
On Tue, Jun 8, 2010 at 7:47 PM, mdekauwe <mde...@gm...> wrote:
>
> Hi,
>
> when I make plots with the Basemap package I seem to be getting a large
> amount of white space at the top and bottom of the plot, even if I use the
> option bbox_inches='tight'. Does anyone know how I can stop this happening?
>
> Example image...
> http://old.nabble.com/file/p28824528/diffmap_JULES_DJF_snowFrac.GSWP2_vs_SSMI.jpg
>
> What I am currently doing...
>
> def makeMap(image, ofname, imageMin, imageMax, ticks):
> """ dump out the array as a nice map... """
> fig = plt.figure(figsize=(8, 6))
> m = Basemap(projection='cyl', llcrnrlon=-179.5, llcrnrlat=-60.0, \
> urcrnrlon=179.5, urcrnrlat=89.5, resolution='c')
> # add an axes, leaving room for colorbar on the right.
> ax = fig.add_axes([0.1, 0.1, 0.7, 0.7])
> m.ax = ax
> colourmap = plt.cm.RdYlBu
> colourmap.set_bad('w', 1.0)
> im = m.imshow(image, colourmap, colors.Normalize(vmin=imageMin,
> vmax=imageMax, clip=False),\
> interpolation='nearest')
> pos = ax.get_position()
> l, b, w, h = pos.bounds
> cax = plt.axes([l + w + 0.045, b, 0.05, h])
> norm = colors.BoundaryNorm(ticks, colourmap.N)
> cbar = mpl.colorbar.ColorbarBase(cax, cmap=colourmap, norm=norm,
> ticks=ticks)
> #cbar = plt.colorbar(im, cax=cax)
> # xlabel and ylabel were overwriting the tick labels so i need to move
> them
> # transAxes set the position from the bottom left of the axes (1,1 would
> be the top right)
> ax.text(0.5, -0.15, 'Longitude', transform=ax.transAxes, ha='center',
> va='center')
> ax.text(-0.1, 0.5, 'Latitude', transform=ax.transAxes, ha= 'center',
> va='center', rotation='vertical')
> #ax.set_title("Sign.Different: GSPW2 - Princeton")
> m.drawcoastlines(linewidth=0.2, color='k')
> m.drawcountries(linewidth=0.1, color='k')
> #m.drawrivers()
> m.drawparallels(np.array([-90.0, -60.0, -30.0, 0.0, 30.0, 60.0, 90.0]),
> labels=[1,0,0,1],\
> fontsize=10, rotation=20)
> m.drawmeridians(np.array([-180.0, -120.0, -60.0, 0.0, 60.0, 120.0,
> 180.0]), labels=[0,0,0,1], \
> fontsize=10, rotation=20)
> fig.savefig(ofname + '.png', dpi=300, bbox_inches='tight')
> # for LaTeX doc...
> fig.savefig(os.path.join("/users/eow/mgdk/doc/snowy_notes/figures/",
> (ofname + ".eps")), dpi=300,\
> bbox_inches='tight')
>
> Many thanks,
>
> Martin
> --
> View this message in context: http://old.nabble.com/Removing-white-space-surrounding-plots-tp28824528p28824528.html
> Sent from the matplotlib - users mailing list archive at Nabble.com.
>
>
> ------------------------------------------------------------------------------
> ThinkGeek and WIRED's GeekDad team up for the Ultimate
> GeekDad Father's Day Giveaway. ONE MASSIVE PRIZE to the
> lucky parental unit. See the prize list and enter to win:
> http://p.sf.net/sfu/thinkgeek-promo
> _______________________________________________
> Matplotlib-users mailing list
> Mat...@li...
> https://lists.sourceforge.net/lists/listinfo/matplotlib-users
>
|
|
From: mdekauwe <mde...@gm...> - 2010-06-10 13:28:46
|
Hmm OK thanks. It seems I am using
In [3]: matplotlib.__version__
Out[3]: '0.98.5.3'
so by the sounds of it an outdated version? But your example will work on a
newer version?
What version of matplotlib are you using?
With 0.99.1.1 (and likely later) and also with the svn version, using
the bbox_inches="tight" actually produces an output with too small
margin.
With the svn version, following code should generate an output with an
adequate margin.
Otherwise, you cannot rely on bbox_inches features. Instead, you have
to manually adjust the size of the figure (and subplots_adjust).
Regards,
-JJ
t1=ax.text(0.5, -0.15, 'Longitude', transform=ax.transAxes,
ha='center', va='center')
t2=ax.text(-0.1, 0.5, 'Latitude', transform=ax.transAxes, ha=
'center', va='center', rotation='vertical')
#ax.set_title("Sign.Different: GSPW2 - Princeton")
fig.savefig('test.png', dpi=300, bbox_inches='tight',
bbox_extra_artists=[t1, t2])
--
View this message in context: http://old.nabble.com/Removing-white-space-surrounding-plots-tp28824528p28843153.html
Sent from the matplotlib - users mailing list archive at Nabble.com.
|
|
From: Jae-Joon L. <lee...@gm...> - 2010-06-10 15:46:52
|
On Thu, Jun 10, 2010 at 9:28 AM, mdekauwe <mde...@gm...> wrote: > so by the sounds of it an outdated version? But your example will work on a > newer version? I think bbox_inches option is not supported with your version of matplotlib. You may upgrade to newer version, or you have to manually adjust the figure size etc. Regards, -JJ |
|
From: mdekauwe <mde...@gm...> - 2010-06-10 17:55:11
|
Hi OK assuming upgrading is going to be a hassle. I just looked into and some of the libraries need changing etc etc. Do you know what I would change as a short term work around? When you said adjust size, did you mean this line fig = plt.figure(figsize=(8, 6)) or this ax = fig.add_axes([0.1, 0.1, 0.7, 0.7]) ? Thanks Jae-Joon Lee wrote: > > On Thu, Jun 10, 2010 at 9:28 AM, mdekauwe <mde...@gm...> wrote: >> so by the sounds of it an outdated version? But your example will work on >> a >> newer version? > > I think bbox_inches option is not supported with your version of > matplotlib. > You may upgrade to newer version, or you have to manually adjust the > figure size etc. > > Regards, > > -JJ > > ------------------------------------------------------------------------------ > ThinkGeek and WIRED's GeekDad team up for the Ultimate > GeekDad Father's Day Giveaway. ONE MASSIVE PRIZE to the > lucky parental unit. See the prize list and enter to win: > http://p.sf.net/sfu/thinkgeek-promo > _______________________________________________ > Matplotlib-users mailing list > Mat...@li... > https://lists.sourceforge.net/lists/listinfo/matplotlib-users > > -- View this message in context: http://old.nabble.com/Removing-white-space-surrounding-plots-tp28824528p28846476.html Sent from the matplotlib - users mailing list archive at Nabble.com. |
|
From: Jae-Joon L. <lee...@gm...> - 2010-06-10 18:15:01
|
On Thu, Jun 10, 2010 at 1:55 PM, mdekauwe <mde...@gm...> wrote: > fig = plt.figure(figsize=(8, 6)) This. Also play with subplots_adjust. -JJ |
|
From: mdekauwe <mde...@gm...> - 2010-06-10 20:41:32
|
Unless I am mistaken subplots_adjust doesn't do anything as I am building an axes instance. Doing... fig = plt.figure(figsize=(8, 4)) ax = fig.add_axes([0.1, 0.15, 0.7, 0.7]) has shrunk the white space, though I think the font of the legend looks a bit less well defined, but perhaps that is just my eyes! http://old.nabble.com/file/p28848239/diffmap_JULES_DJF_snowFrac.GSWP2_vs_SSMI.jpeg Jae-Joon Lee wrote: > > On Thu, Jun 10, 2010 at 1:55 PM, mdekauwe <mde...@gm...> wrote: >> fig = plt.figure(figsize=(8, 6)) > > This. > Also play with subplots_adjust. > > -JJ > > ------------------------------------------------------------------------------ > ThinkGeek and WIRED's GeekDad team up for the Ultimate > GeekDad Father's Day Giveaway. ONE MASSIVE PRIZE to the > lucky parental unit. See the prize list and enter to win: > http://p.sf.net/sfu/thinkgeek-promo > _______________________________________________ > Matplotlib-users mailing list > Mat...@li... > https://lists.sourceforge.net/lists/listinfo/matplotlib-users > > -- View this message in context: http://old.nabble.com/Removing-white-space-surrounding-plots-tp28824528p28848239.html Sent from the matplotlib - users mailing list archive at Nabble.com. |
|
From: Benjamin R. <ben...@ou...> - 2010-06-10 21:13:52
|
Just to make sure, were you calling subplots_adjust() *before* calling subplot()? Calling it after subplot() shouldn't have an effect on the already created axes (I think...). Ben Root On Thu, Jun 10, 2010 at 3:41 PM, mdekauwe <mde...@gm...> wrote: > > Unless I am mistaken subplots_adjust doesn't do anything as I am building > an > axes instance. Doing... > > fig = plt.figure(figsize=(8, 4)) > ax = fig.add_axes([0.1, 0.15, 0.7, 0.7]) > > has shrunk the white space, though I think the font of the legend looks a > bit less well defined, but perhaps that is just my eyes! > > http://old.nabble.com/file/p28848239/diffmap_JULES_DJF_snowFrac.GSWP2_vs_SSMI.jpeg > > > > > Jae-Joon Lee wrote: > > > > On Thu, Jun 10, 2010 at 1:55 PM, mdekauwe <mde...@gm...> wrote: > >> fig = plt.figure(figsize=(8, 6)) > > > > This. > > Also play with subplots_adjust. > > > > -JJ > > > > > ------------------------------------------------------------------------------ > > ThinkGeek and WIRED's GeekDad team up for the Ultimate > > GeekDad Father's Day Giveaway. ONE MASSIVE PRIZE to the > > lucky parental unit. See the prize list and enter to win: > > http://p.sf.net/sfu/thinkgeek-promo > > _______________________________________________ > > Matplotlib-users mailing list > > Mat...@li... > > https://lists.sourceforge.net/lists/listinfo/matplotlib-users > > > > > > -- > View this message in context: > http://old.nabble.com/Removing-white-space-surrounding-plots-tp28824528p28848239.html > Sent from the matplotlib - users mailing list archive at Nabble.com. > > > > ------------------------------------------------------------------------------ > ThinkGeek and WIRED's GeekDad team up for the Ultimate > GeekDad Father's Day Giveaway. ONE MASSIVE PRIZE to the > lucky parental unit. See the prize list and enter to win: > http://p.sf.net/sfu/thinkgeek-promo > _______________________________________________ > Matplotlib-users mailing list > Mat...@li... > https://lists.sourceforge.net/lists/listinfo/matplotlib-users > |
|
From: mdekauwe <mde...@gm...> - 2010-06-10 21:29:04
|
Hi, yes as I understand it (happy to be corrected)... fig = plt.figure(figsize=(8, 4)) fig.subplots_adjust(bottom=0.05) does nothing unless you also do ax = fig.add_subplot(111) but as I am also doing ax = fig.add_axes([0.1, 0.15, 0.7, 0.7]), it means I would just get two frames. Benjamin Root-2 wrote: > > Just to make sure, were you calling subplots_adjust() *before* calling > subplot()? Calling it after subplot() shouldn't have an effect on the > already created axes (I think...). > > Ben Root > > On Thu, Jun 10, 2010 at 3:41 PM, mdekauwe <mde...@gm...> wrote: > >> >> Unless I am mistaken subplots_adjust doesn't do anything as I am building >> an >> axes instance. Doing... >> >> fig = plt.figure(figsize=(8, 4)) >> ax = fig.add_axes([0.1, 0.15, 0.7, 0.7]) >> >> has shrunk the white space, though I think the font of the legend looks a >> bit less well defined, but perhaps that is just my eyes! >> >> http://old.nabble.com/file/p28848239/diffmap_JULES_DJF_snowFrac.GSWP2_vs_SSMI.jpeg >> >> >> >> >> Jae-Joon Lee wrote: >> > >> > On Thu, Jun 10, 2010 at 1:55 PM, mdekauwe <mde...@gm...> wrote: >> >> fig = plt.figure(figsize=(8, 6)) >> > >> > This. >> > Also play with subplots_adjust. >> > >> > -JJ >> > >> > >> ------------------------------------------------------------------------------ >> > ThinkGeek and WIRED's GeekDad team up for the Ultimate >> > GeekDad Father's Day Giveaway. ONE MASSIVE PRIZE to the >> > lucky parental unit. See the prize list and enter to win: >> > http://p.sf.net/sfu/thinkgeek-promo >> > _______________________________________________ >> > Matplotlib-users mailing list >> > Mat...@li... >> > https://lists.sourceforge.net/lists/listinfo/matplotlib-users >> > >> > >> >> -- >> View this message in context: >> http://old.nabble.com/Removing-white-space-surrounding-plots-tp28824528p28848239.html >> Sent from the matplotlib - users mailing list archive at Nabble.com. >> >> >> >> ------------------------------------------------------------------------------ >> ThinkGeek and WIRED's GeekDad team up for the Ultimate >> GeekDad Father's Day Giveaway. ONE MASSIVE PRIZE to the >> lucky parental unit. See the prize list and enter to win: >> http://p.sf.net/sfu/thinkgeek-promo >> _______________________________________________ >> Matplotlib-users mailing list >> Mat...@li... >> https://lists.sourceforge.net/lists/listinfo/matplotlib-users >> > > ------------------------------------------------------------------------------ > ThinkGeek and WIRED's GeekDad team up for the Ultimate > GeekDad Father's Day Giveaway. ONE MASSIVE PRIZE to the > lucky parental unit. See the prize list and enter to win: > http://p.sf.net/sfu/thinkgeek-promo > _______________________________________________ > Matplotlib-users mailing list > Mat...@li... > https://lists.sourceforge.net/lists/listinfo/matplotlib-users > > -- View this message in context: http://old.nabble.com/Removing-white-space-surrounding-plots-tp28824528p28848795.html Sent from the matplotlib - users mailing list archive at Nabble.com. |