Inconsistent figure coordinates in matplotlib with equal aspect ratio

I am preparing a figure with subtitles and arrows going from one to another, which is discussed here: Drawing lines between two graphs in Matplotlib

In my figure, all subnets have the same aspect ratio, and this seems to ruin the conversion from data coordinates to figure coordinates, so the Line2D objects that I create do not go where I want them.

Here is a simple example (modified from the link above) that demonstrates the problem and does not even require subtitles:

import matplotlib
import matplotlib.pyplot as plt
import numpy as np

fig = plt.figure(figsize=(6,4))
ax = fig.add_subplot(111)
ax.set_aspect('equal')

x = [0.2, 0.9]
y = [0.3, 0.7]

ax.plot(x,y,'k--', lw=4)

transFigure = fig.transFigure.inverted()

coord1 = transFigure.transform(ax.transData.transform([x[0],y[0]]))
coord2 = transFigure.transform(ax.transData.transform([x[1],y[1]]))

line = matplotlib.lines.Line2D((coord1[0],coord2[0]),(coord1[1],coord2[1]),
                           transform=fig.transFigure)

fig.lines.append(line)

plt.show()

By resizing the figure, it is easy to see that the Line2D object changes its slope, while the graph on the axes maintains its slope (optional for an equal aspect ratio).

( ), Line2D ?

+4
1

, matplotlib, - . , . , x = [0.2, 0.9]; y = [0.55, 0.6], .

enter image description here

-

fig.canvas.draw()

plot, - . , , ; .

enter image description here

+4

Source: https://habr.com/ru/post/1667597/


All Articles