Does anyone know of an easy way to expand the plot area to include annotations? I have a figure where some labels are long and/or multiline strings, and rather than clipping these to the axes, I want to expand the axes to include the annotations.
Autoscale_view doesn't do it, and ax.relim doesn't pick up the position of the annotations, so that doesn't seem to be an option.
I've tried to do something like the code below, which loops over all the annotations (assuming they are in data coordinates) to get their extents and then updates the axes accordingly, but ideally I don't want my annotations in data coordinates (they are offset from the actual data points).
xmin, xmax = plt.xlim()
ymin, ymax = plt.ylim()
# expand figure to include labels
for l in my_labels:
# get box surrounding text, in data coordinates
bbox = l.get_window_extent(renderer=plt.gcf().canvas.get_renderer())
l_xmin, l_ymin, l_xmax, l_ymax = bbox.extents
xmin = min(xmin, l_xmin); xmax = max(xmax, l_xmax); ymin = min(ymin, l_ymin); ymax = max(ymax, l_ymax)
plt.xlim(xmin, xmax)
plt.ylim(ymin, ymax)
For me
tight_layout
usually solved the problem, but in some cases I had to use 'manual' adjustments with subplots_adjust, like this:The numbers do not usually change dramatically, so you can fix them rather then try to calculate from the actual plot.
BTW, setting xlim as you do in your example changes only the range of the data you plot and not the white area around all your labels.
In
matplotlib1.1
thetight_layout
is introduced to solve some of the layout problems. There is a nice tutorial here.I struggled with this too. The key point is that matplotlib doesn't determine how big the text is going to be until it has actually drawn it. So you need to explicitly call
plt.draw()
, then adjust your bounds, and then draw it again.The
get_window_extent
method is supposed to give an answer in display coordinates, not data coordinates, per the documentation. But if the canvas hasn't been drawn yet, it seems to respond in whatever coordinate system you specified in thetextcoords
keyword argument toannotate
. That's why your code above works usingtextcoords='data'
, but not'offset points'
.Here's an example:
We want to change the limits so that the text label is within the axes. The value of
bbox
given isn't much help: since it's in points relative to the labeled point: offset by 12 points in x, a string that evidently will be a little over 30 points long, in 10 point font (-5 to 5 in y). It's nontrivial to figure out how to get from there to a new set of axes bounds.However, if we call the method again now that we've drawn it, we get a totally different bbox:
Now we get
This is in display coordinates, which we can transform with
ax.transData
like we're used to. So to get our labels into the bounds, we can do:Note it's no longer necessary to explicitly pass
plt.gcf().canvas.get_renderer()
toget_window_extent
after the plot has been drawn once. Also, I'm usingupdate_datalim
instead ofxlim
andylim
directly, so that the autoscaling can notch itself up to a round number automatically.I posted this answer in notebook format here.