The bbox axes do not contain anything outside the "inside" axes (for example, it does not have labels for labels, title, etc.)
One quick way around this is to simply capture the entire area of ββthe piece when you hit. (For example, background = canvas.copy_from_bbox(fig.bbox) )
This can cause problems if you have several subplots and only want to revive one of them. In this case, you can do something along the lines of background = canvas.copy_from_bbox(ax.bbox.expanded(1.1, 1.2)) . However, you will need to evaluate the odds you need.
If you need an exact label for shortcuts, this is a little trickier. The easiest way is to loop through ticklabel objects and get union with ax.bbox . You can do this in one layer: ax.bbox.union([label.get_window_extent() for label in ax.get_xticklabels()]) .
Anyway, one of these three options should do what you need, I think.
source share