I am plotting with imshow() and create a colorbar. When saving the figure as .eps with savefig() and option bbox_inches='tight', the colorbar (without the spines) is displaced.
changed the title [-]Colorbar is displaced when saving as .eps with bbox_inches='tight'[/-][+][Bug]: Colorbar is displaced when saving as .eps with bbox_inches='tight'[/+]on Feb 8, 2023
i.e. _print_ps needs to have an explicit bbox_inches_restore kwarg (due to the implementation of signature sniffing in _switch_canvas_and_get_print_method). I'll try to write up a PR in the coming days, but anyone should feel free to adopt the patch if they can get this in faster.
Note that this is actually a regression in 3.6.0 that has been around for the whole 3.6 release cycle, so I guess it's technically not release critical for 3.7, but perhaps still a good idea to fix asap.
@Hexanders I think your problem is slightly different as it only shows when you include the inset axes. Please open a new issue so that we can track it.
Activity
jklymak commented on Feb 9, 2023
Confirmed on master as well. Note this doesn't happen for PDF, so it's an eps-only bug.
This bisects to c712f35, #21395
jklymak commented on Feb 9, 2023
@anntzer did you have time to look at what went wrong here?
anntzer commented on Feb 10, 2023
Oops, good catch. The fix is
i.e. _print_ps needs to have an explicit bbox_inches_restore kwarg (due to the implementation of signature sniffing in _switch_canvas_and_get_print_method). I'll try to write up a PR in the coming days, but anyone should feel free to adopt the patch if they can get this in faster.
Note that this is actually a regression in 3.6.0 that has been around for the whole 3.6 release cycle, so I guess it's technically not release critical for 3.7, but perhaps still a good idea to fix asap.
Fix displacement of colorbar for eps with bbox_inches='tight'
Fix displacement of colorbar for eps with bbox_inches='tight'
Hexanders commented on Oct 19, 2023
I am experiencing the same problem using inset_axes and savefigure() as pdf with the option bbox_inches='tight'.
import matplotlib.pyplot as plt import numpy as np import matplotlib.cbook as cbook import matplotlib.cm as cm from matplotlib.patches import PathPatch from matplotlib.path import Path np.random.seed(19680801) delta = 0.025 x = y = np.arange(-3.0, 3.0, delta) X, Y = np.meshgrid(x, y) Z1 = np.exp(-X**2 - Y**2) Z2 = np.exp(-(X - 1)**2 - (Y - 1)**2) Z = (Z1 - Z2) * 2 fig, ax = plt.subplots() im = ax.imshow(Z, interpolation='bilinear', cmap=cm.RdYlGn, origin='lower', extent=[-3, 3, -3, 3], vmax=abs(Z).max(), vmin=-abs(Z).max()) cb = plt.colorbar(mappable=im) plt.show() axins = inset_axes(ax, width='20%', height='20%', loc='upper right') Z2 = axins.imshow(Z, interpolation='bilinear', cmap=cm.RdYlGn, origin='lower', extent=[-3, 3, -3, 3], vmax=abs(Z).max(), vmin=-abs(Z).max()) x1, x2, y1, y2 = -1,0,0,1 axins.set_xlim(x1, x2) axins.set_ylim(y1, y2) rectpatch, connects = ax.indicate_inset_zoom(axins, edgecolor = 'k',alpha=1, lw = 1) connects[0].set_visible(False) connects[1].set_visible(True) connects[2].set_visible(True) connects[3].set_visible(False) fig.savefig("temp.pdf", bbox_inches='tight', transparent=True, format="pdf")temp.pdf
OS: Linux Fedora 37 (Workstation Edition)
Python version : 3.11.5
Matplotlib version: 3.8.0
rcomer commented on Oct 19, 2023
@Hexanders I think your problem is slightly different as it only shows when you include the inset axes. Please open a new issue so that we can track it.
Hexanders commented on Oct 19, 2023
@rcomer thank you for your quiq response. I will do so immediately.
Fix displacement of colorbar for eps with bbox_inches='tight'
Fix displacement of colorbar for eps with bbox_inches='tight'
Fix displacement of colorbar for eps with bbox_inches='tight'