Difference between 'data' and 'axes fraction' in matplotlib?

Question:

What does xycoords=('data','axes fraction') mean? I have read

https://matplotlib.org/stable/tutorials/text/annotations.html and it says that 'data' uses the axes data coordinate system, what does this mean?

According to https://matplotlib.org/stable/tutorials/advanced/transforms_tutorial.html 'data' is the coordinate system of the data in the Axes; (0, 0) is bottom left of the axes, and (1, 1) is top right of the axes.

https://matplotlib.org/stable/tutorials/text/annotations.html says 'axes fraction' is (0, 0) is lower left of axes and (1, 1) is upper right. So are 'data' and 'axes fraction' the same thing?

ax.annotate(percent, xy=(x, 0), xycoords=('data','axes fraction'), xytext=(0,-32), textcoords='offset points',va='top' ,ha='center')
Asked By: TaterTots'

||

Answers:

According to matplotlib’s transforms tutorial, 'data' is the coordinate system of the data in the Axes; (0, 0) is bottom left of the axes, and (1, 1) is top right of the axes.

From what I’m seeing, the 'data' description does not include the second half about (0, 0) and (1, 1). If it’s somewhere on the page that I’ve missed, that’s a mistake and an issue/PR should be filed. That second half should only exist for the 'axes fraction' description.


To clarify by example:

  • A 'data' annotation coord of (2, 1) will show up at x=2 and y=1
  • An 'axes fraction' annotation coord of (2, 1) will show up at 200% of the x-axis and 100% y
  • A ('data', 'axes fraction') annotation coord of (2, 1) will show up at x=2 and 100% y
  • A ('axes fraction', 'data') annotation coord of (2, 1) will show up at 200% x and y=1

demo of data and frac coords

import matplotlib.pyplot as plt

plt.plot([0, 10], [0, 5])

# at x=2, y=1
plt.annotate('data @ (2, 1)', xy=(2, 1), xycoords='data')

# at 200% x, 100% y
plt.annotate('frac @ (2, 1)', xy=(2, 1), xycoords='axes fraction')

# at x=2, 100% y
plt.annotate('(data, frac) @ (2, 1)', xy=(2, 1), xycoords=('data', 'axes fraction'))

# at 200% x, y=1
plt.annotate('(frac, data) @ (2, 1)', xy=(2, 1), xycoords=('axes fraction', 'data'))
Answered By: tdy
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.