Discussion:
[Matplotlib-users] possible regression in plt.draw (mpl 1.4.3 -> 1.5.0) when using fig.text using transformations
Oliver Willekens
2016-03-01 09:54:43 UTC
Permalink
Dear matplotlib users and developers,

I'm using `plt.draw()` to force the rendering of all artists and then,
based on their newly calculated positions, place a text label on the figure
window in figure coordinates.

The goal is to add a text label near the conventional y-axis, at the top,
right-aligned. Example code that demonstrates the problem:

```
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import numpy as np
import matplotlib.pyplot as plt
import matplotlib as mpl
print(mpl.__version__)

x = np.linspace(0, 50)
y = 4*np.sin(x) + 5

fig = plt.figure(figsize=(18,9.8))
ax = fig.add_axes((0.1, 0.1, 0.8, 0.8),
frameon=True,
aspect='equal',
adjustable='box',
xlim=(x.min(), x.max()),
ylim=(0, 10),
xticks=[x.min(), x.max()],
yticks=[0, 10],
xlabel='dimension (unit)')
ax.plot(x, y)
plt.draw() # force redraw

ylabel_pos =
fig.transFigure.inverted().transform_point(ax.transAxes.transform_point((0,1)))
label1 = fig.text(ylabel_pos[0], ylabel_pos[1], "label1", ha="right",
va="bottom")
plt.savefig('/tmp/test_pre_mpl_v_{}.png'.format(mpl.__version__))
ylabel_pos =
fig.transFigure.inverted().transform_point(ax.transAxes.transform_point((0,1)))
label2 = fig.text(ylabel_pos[0], ylabel_pos[1], "label2", ha="right",
va="bottom")
plt.savefig('/tmp/test_post_mpl_v_{}.png'.format(mpl.__version__))
```

The code shows that in mpl 1.4.3 both label1 and label2 end up at the same
(desired) position. However, mpl 1.5.0 and 1.5.1 (just installed to check)
show that label1 is at a height of 0.9 in the figure coordinates. After the
first call to `savefig`, the figure is rendered with the axes getting a new
height and width (due to the call to `aspect='equal', adjustable='box'`)
and so the subsequent call to `savefig` renders label2 in the correct
position.

Using `ax.text(x=0, y=1, s='label', transform=ax.transAxes, ha="right",
va="bottom")` gets the job done alright (both in 1.4.3, as well as 1.5.0),
but the call to `fig.text` using the subsequent transforms should have
worked, I believe.

Kind regards,

Oliver

Loading...