Skip to content

Commit

Permalink
Merge pull request #18 from tomicapretto/support_data_xycoords
Browse files Browse the repository at this point in the history
Allow users to do xycoords='data'
  • Loading branch information
tomicapretto authored Jan 19, 2025
2 parents 63f9fca + 675a58c commit 74b15f2
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 3 deletions.
7 changes: 4 additions & 3 deletions flexitext/flexitext.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ def plot(
Vertical alignment for text within multiline texts. Can be one of `"top"`, `"bottom"`,
`"left"`, `"right"`, `"center"`, or `"baseline"`. Defaults to `"baseline"`.
xycoords: str
The coordinate system for `x` and `y`. Must be one of `'axes fraction'` or
The coordinate system for `x` and `y`. Must be one of `'data'`, `'axes fraction'`, or
`'figure fraction'`.
ax: matplotlib.axes.Axes
Matplotlib `Axes`. The default value means the `Axes` is obtained with `plt.gca()`
Expand All @@ -67,14 +67,15 @@ def plot(

if ax is None:
ax = plt.gca()
if xycoords == "axes fraction":
if xycoords in ("axes fraction", "data"):
parent = ax
elif xycoords == "figure fraction":
parent = ax.figure
xycoords = ax.figure.transFigure
else:
raise ValueError(
f"'xycoords' must be one of 'axes fraction' or 'figure fraction', not {xycoords}"
"'xycoords' must be one of 'data', 'axes fraction' or 'figure fraction', "
f"not {xycoords}"
)

offsetbox = self._make_offset_box(ma, mva)
Expand Down
6 changes: 6 additions & 0 deletions flexitext/tests/test_flexitext.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,9 @@ def test_flexitext():

with pytest.raises(ValueError):
flexitext(0.5, 0.5, text, xycoords="other stuff")

# Test that xycoords="data" works
_, ax = plt.subplots()
ax.plot([10, 20], [50, 80])
text = "<color:blue, size:16>This is blue text</> and this is regular text"
flexitext(15, 60, text, ha="center", xycoords="data")

0 comments on commit 74b15f2

Please sign in to comment.