Skip to content

Error for bbox_inches='tight' in savefig with make_axes_locatable #12355

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
jiaozhh opened this issue Oct 1, 2018 · 6 comments · Fixed by #12363
Closed

Error for bbox_inches='tight' in savefig with make_axes_locatable #12355

jiaozhh opened this issue Oct 1, 2018 · 6 comments · Fixed by #12363
Assignees
Milestone

Comments

@jiaozhh
Copy link

jiaozhh commented Oct 1, 2018

Bug report

Bug summary

When setting bbox_inches='tight' in savefig function, the colorbar is plotted in the wrong way.

Code for reproduction

import matplotlib.pyplot as plt
import numpy as np
from mpl_toolkits.basemap import Basemap

outfile = r'E:\wrong.tif'

# read in data on lat/lon grid.
hgt = np.loadtxt(r'E:\tmp\basemap\500hgtdata.gz')
lons = np.loadtxt(r'E:\tmp\basemap\500hgtlons.gz')
lats = np.loadtxt(r'E:\tmp\basemap\500hgtlats.gz')
lons, lats = np.meshgrid(lons, lats)

mnh = Basemap(lon_0=-105, boundinglat=20.,
              resolution='c', area_thresh=10000., projection='nplaea')
xnh, ynh = mnh(lons, lats)
CS = mnh.contour(xnh, ynh, hgt, 15, linewidths=0.5, colors='k')
CS = mnh.contourf(xnh, ynh, hgt, 15, cmap=plt.cm.Spectral)

mnh.drawcoastlines(linewidth=0.5)
delat = 30.
circles = np.arange(0., 90., delat).tolist() + \
          np.arange(-delat, -90, -delat).tolist()
mnh.drawparallels(circles, labels=[1, 0, 0, 0])
delon = 45.
meridians = np.arange(0, 360, delon)
mnh.drawmeridians(meridians, labels=[1, 0, 0, 1])
plt.title('NH 500 hPa Height (cm.Spectral)')

# colorbar on bottom.
mnh.colorbar(pad='5%')
plt.savefig(outfile, dpi=300, bbox_inches='tight')  # wrong
# plt.savefig(outfile, dpi=300) # right
plt.show()

Actual outcome

The wrong one:
default

# If applicable, paste the console output here
# None
#

Expected outcome

The correct figure:
default

Matplotlib version

  • Operating system: Windows 10 x64
  • Matplotlib version: 3.0.0
  • Matplotlib backend (print(matplotlib.get_backend())): module://backend_interagg
  • Python version: 3.6.6
  • Jupyter version (if applicable): None
  • basemap version: 1.2.0
  • PyCharm version: 2018.2.4

I install matplotlib and basemap from https://www.lfd.uci.edu/~gohlke/pythonlibs.

@jklymak
Copy link
Member

jklymak commented Oct 1, 2018

This should probably be reported in the base map subgroup. Can you make this self contained?

Things changed for drawing colorbars in MPL 3.0 and it’s possible we broke something for basemap, but it’s hard to know what.

@jiaozhh
Copy link
Author

jiaozhh commented Oct 1, 2018

OK, I will report it in the basemap subgroup.

@jklymak
Copy link
Member

jklymak commented Oct 1, 2018

OK I can reproduce in Matplotlib:

Edit: has nothing to do with colorbar:

import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.axes_grid1 import make_axes_locatable

fig, ax = plt.subplots()
divider = make_axes_locatable(ax)
cax = divider.append_axes('right', size='5%', pad='2%')
plt.savefig('boo.pdf', dpi=300, bbox_inches='tight')  # wrong
  • Note that its something wrong w/ the bbox_inches='tight' interaction - plt.show() and plt.tight_layout() work as expected.
  • it doesn't matter if the colorbar is on the right or bottom of the page...

@jklymak
Copy link
Member

jklymak commented Oct 1, 2018

Crossref matplotlib/basemap#426

@jklymak
Copy link
Member

jklymak commented Oct 1, 2018

Bisect says it was my PR: #9855

@jklymak
Copy link
Member

jklymak commented Oct 1, 2018

This is quite a wild goose chase: #10682 also made this not work even if I roll back #9855 changes.

@jklymak jklymak changed the title Error for bbox_inches='tight' in savefig Error for bbox_inches='tight' in savefig with make_axes_locatable Oct 1, 2018
@jklymak jklymak self-assigned this Oct 1, 2018
@QuLogic QuLogic added this to the v3.0.x milestone Oct 12, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants