When constructing inset axes using the tutorial on this page the gradient of the colorbar does not appear in the constructed inset axis but somewhere else on the figure (location seems to depend on figure size) once saved to a PDF with multiple other figures if bbox_inches='tight'. Similar to #27143 and #25176.
The gradient for the colorbar appears outside of the inset axes on each of the plots test_small.pdf
Expected outcome
The result for this particular example when bbox_inches='tight' is not called in savefig(). Note: when running in jupyter lab this is always the outcome--only when saving the figure does the gradient jump out of the inset axis. test_loose.pdf
Additional information
I tried the same suggestion as in #27143 and used axins1 = axes.inset_axes([0.05, 0.7, 0.1, 0.2], transform = axes.transAxes) in my own code which seemed to fix this. Unsure why the example constructor for inset_axes here doesn't work in this case.
similar problem with cartopy and inset. when saving figure in pdf with bbox_inches='tight' the colorbar's colors is moving with matplotlib 3.8.3. Solved by using matplotlib 3.7.1
I'm wondering if there is an update to this , I'm having the same issue where the color of the bar is getting rasterized and so showing up at bottom left of imshow when I don't want it there
The temporary solution I found was to use something like axins1 = axes.inset_axes([0.05, 0.7, 0.1, 0.2], transform = axes.transAxes) to make an inset axis in a specific place and then to put the color bar into that inset axis when plotting.
To summarize this issue is when we have a combination of:
bbox_inches='tight'
rasterized artists in an inset axes
The way bbox_inches='tight' works is by updating the base transform in a somewhat fancy way. The amount of offset seems to depend on the original figure size which suggests that this is either a caching or a rounding issue.
Activity
jklymak commented on Feb 8, 2024
"Users should consider using ax.inset_axes instead". https://matplotlib.org/stable/users/explain/axes/colorbar_placement.html#colorbar-placement
rcomer commented on Feb 9, 2024
This can be reproduced more simply with
The png is fine, the pdf shows the problem. Confirmed broken on main.
rcomer commented on Feb 9, 2024
I can reproduce this in v3.7.3 but not in v3.7.1. The code won't run at v3.7.2 because of #26287.
rcomer commented on Feb 9, 2024
If I branch off v3.7.x and drop the commit from #25499 (which caused #26287), I cannot reproduce.
jklymak commented on Feb 9, 2024
import matplotlib.pyplot as plt import numpy as np fig, axes = plt.subplots(1, 1, layout='constrained') x = np.random.randint(100, size=(30, 200)) c = axes.imshow(x, origin='lower', cmap='Reds', aspect=1) axins1 = axes.inset_axes([0.05, 0.8, 0.2, 0.09]) fig.colorbar(c, cax=axins1, orientation="horizontal") fig.savefig('test_single.png', bbox_inches='tight') fig.savefig('test_single.pdf', bbox_inches='tight')Also seems to have a small error in the placement of the colorbar's colors in PDF:
This doesn't happen if you dont' use bbox_inches='tight', so maybe the same bug, but much more subtle
l4nc3l0t commented on Feb 22, 2024
similar problem with cartopy and inset. when saving figure in pdf with bbox_inches='tight' the colorbar's colors is moving with matplotlib 3.8.3. Solved by using matplotlib 3.7.1
imcvis commented on Apr 1, 2024
same problem here, solved downgrading to 3.7.1
rcomer commented on Apr 2, 2024
I think this may have something to do with a dpi mismatch. If I remove this line
matplotlib/lib/matplotlib/backend_bases.py
Lines 2208 to 2210 in acfef85
the problem is fixed (though of course other tests fail).
Edit: it also doesn't fix the more subtle case from #27763 (comment)
rcomer commented on Apr 2, 2024
Also specific to rasterization. If we skip this line
matplotlib/lib/matplotlib/colorbar.py
Line 583 in acfef85
the colours are positioned where they should be (though they don't look great) both for the main axes_grid1 case and the case from #27763 (comment).
Do rasterized objects only get drawn on the first draw or something?
andrewkhardy commented on Jun 13, 2024
I'm wondering if there is an update to this , I'm having the same issue where the color of the bar is getting rasterized and so showing up at bottom left of imshow when I don't want it there
ericasaw commented on Jun 13, 2024
The temporary solution I found was to use something like axins1 = axes.inset_axes([0.05, 0.7, 0.1, 0.2], transform = axes.transAxes) to make an inset axis in a specific place and then to put the color bar into that inset axis when plotting.
tacaswell commented on Jun 13, 2024
To summarize this issue is when we have a combination of:
The way bbox_inches='tight' works is by updating the base transform in a somewhat fancy way. The amount of offset seems to depend on the original figure size which suggests that this is either a caching or a rounding issue.