Seyed Omid Nabavi
Seyed Omid Nabavi

Reputation: 497

how to adjust space between columns in xarray faceted plot with basemap?

I want to make a faceted plot using xarray. The problem occurs when I try to add a bassemap to each subplot using basemap. Too much space is put between columns. I have already tried plt.subplots_adjust(hspace=0.01, wspace=0) and plt.tight_layout(pad=0), but no luck yet. How can I control the space between columns?

The sample dataset (ds_sub) can be found here. The following code produces the below figure.

enter image description here

Thanks.

import pickle
from matplotlib import pyplot
from matplotlib import cm, colors
from mpl_toolkits.basemap import Basemap
import xarray as xr

with open('/data/direcotory/test_data.pkl', "rb") as f:
    ds_sub = pickle.load(f)
    f.close()

plt.close("all")
plt.figure()

levels = np.arange(0, ds_sub['sum'].quantile(0.99),
                   (ds_sub['sum'].quantile(0.99) / 10)).tolist()

norm = colors.BoundaryNorm(levels, len(levels))

p = ds_sub['sum'].loc[[0, 3, 6, 9], :, :].plot(add_colorbar=False, row='time', cmap='OrRd', col_wrap=2,
                                               norm=norm)

mappable = p.axes[0][0].collections[0]
cax = plt.axes([0.85, 0.2, 0.05, 0.6])
cbar1 = plt.colorbar(mappable, ticks=levels,
                     values=levels, cax=cax, orientation='vertical')
cbar1.ax.tick_params(labelsize=14)

for i, ax in enumerate(p.axes.flatten()):
    ax.set_xlabel('')
    ax.set_ylabel('')
    ax.margins(0, 0)
    lon_0 = ds_sub['lon'].mean()
    lat_0 = ds_sub['lat'].mean()
    m = Basemap(resolution='f',
                lat_ts=20, lat_0=lat_0, lon_0=lon_0,
                llcrnrlon=ds_sub['lon'].min(),
                llcrnrlat=ds_sub['lat'].min(),
                urcrnrlon=ds_sub['lon'].max(),
                urcrnrlat=ds_sub['lat'].max(), ax=ax)
    m.drawmapboundary()
    m.drawrivers()
    m.drawcoastlines()
    m.drawcountries()
    m.drawstates()
    dohax, dohay = m(51.534817, 25.286106)
    print(i)
plt.subplots_adjust(hspace=0.01, wspace=0)
plt.tight_layout(pad=0)
plt.show()

Upvotes: 0

Views: 582

Answers (1)

Seyed Omid Nabavi
Seyed Omid Nabavi

Reputation: 497

In case others encounter a similar issue, here's a possible solution to remove the space between subplots using plt.subplots_adjust(). You can specify negative values for the hspace and wspace parameters to reduce the vertical and horizontal spacing, respectively. Just as an example (you need to play with values):

plt.subplots_adjust(hspace=-1.2, wspace=-1.2)

Upvotes: 0

Related Questions