Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added named_arrays.plt.text() function. #29

Merged
merged 1 commit into from
Feb 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions named_arrays/_scalars/scalar_named_array_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -570,6 +570,46 @@
return result


@_implements(na.plt.text)
def plt_text(
x: float | u.Quantity | na.AbstractScalarArray,
y: float | u.Quantity | na.AbstractScalarArray,
s: str | na.AbstractScalarArray,
ax: None | matplotlib.axes.Axes | na.AbstractScalarArray = None,
**kwargs,
) -> na.AbstractScalarArray:

if ax is None:
ax = plt.gca()

Check warning on line 583 in named_arrays/_scalars/scalar_named_array_functions.py

View check run for this annotation

Codecov / codecov/patch

named_arrays/_scalars/scalar_named_array_functions.py#L582-L583

Added lines #L582 - L583 were not covered by tests

try:
x = scalars._normalize(x)
y = scalars._normalize(y)
s = scalars._normalize(s)
ax = scalars._normalize(ax)

Check warning on line 589 in named_arrays/_scalars/scalar_named_array_functions.py

View check run for this annotation

Codecov / codecov/patch

named_arrays/_scalars/scalar_named_array_functions.py#L585-L589

Added lines #L585 - L589 were not covered by tests
except na.ScalarTypeError: # pragma: nocover
return NotImplemented

shape = na.shape_broadcasted(x, y, s, ax)

Check warning on line 593 in named_arrays/_scalars/scalar_named_array_functions.py

View check run for this annotation

Codecov / codecov/patch

named_arrays/_scalars/scalar_named_array_functions.py#L593

Added line #L593 was not covered by tests

x = x.broadcast_to(shape)
y = y.broadcast_to(shape)
s = s.broadcast_to(shape)
ax = ax.broadcast_to(shape)

Check warning on line 598 in named_arrays/_scalars/scalar_named_array_functions.py

View check run for this annotation

Codecov / codecov/patch

named_arrays/_scalars/scalar_named_array_functions.py#L595-L598

Added lines #L595 - L598 were not covered by tests

result = na.ScalarArray.empty(shape, dtype=matplotlib.axes.Axes)

Check warning on line 600 in named_arrays/_scalars/scalar_named_array_functions.py

View check run for this annotation

Codecov / codecov/patch

named_arrays/_scalars/scalar_named_array_functions.py#L600

Added line #L600 was not covered by tests

for index in na.ndindex(shape):
result[index] = ax[index].ndarray.text(

Check warning on line 603 in named_arrays/_scalars/scalar_named_array_functions.py

View check run for this annotation

Codecov / codecov/patch

named_arrays/_scalars/scalar_named_array_functions.py#L602-L603

Added lines #L602 - L603 were not covered by tests
x=x[index].ndarray,
y=y[index].ndarray,
s=s[index].ndarray,
**kwargs,
)

return result

Check warning on line 610 in named_arrays/_scalars/scalar_named_array_functions.py

View check run for this annotation

Codecov / codecov/patch

named_arrays/_scalars/scalar_named_array_functions.py#L610

Added line #L610 was not covered by tests


@_implements(na.jacobian)
def jacobian(
function: Callable[[na.AbstractScalar], na.AbstractScalar],
Expand Down
10 changes: 10 additions & 0 deletions named_arrays/_scalars/tests/test_scalars.py
Original file line number Diff line number Diff line change
Expand Up @@ -957,6 +957,16 @@
assert isinstance(result, na.ScalarArray)
assert result.dtype == matplotlib.image.AxesImage

class TestPltText:

Check warning on line 960 in named_arrays/_scalars/tests/test_scalars.py

View check run for this annotation

Codecov / codecov/patch

named_arrays/_scalars/tests/test_scalars.py#L960

Added line #L960 was not covered by tests

def test_plt_text(self, array):
result = na.plt.text(

Check warning on line 963 in named_arrays/_scalars/tests/test_scalars.py

View check run for this annotation

Codecov / codecov/patch

named_arrays/_scalars/tests/test_scalars.py#L962-L963

Added lines #L962 - L963 were not covered by tests
x=array,
y=array,
s="test label",
)
assert isinstance(result, na.ScalarArray)

Check warning on line 968 in named_arrays/_scalars/tests/test_scalars.py

View check run for this annotation

Codecov / codecov/patch

named_arrays/_scalars/tests/test_scalars.py#L968

Added line #L968 was not covered by tests

@pytest.mark.parametrize(
argnames="function",
argvalues=[
Expand Down
71 changes: 71 additions & 0 deletions named_arrays/plt.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from typing import Literal
import matplotlib
import matplotlib.pyplot as plt
import astropy.units as u
import numpy.typing as npt
import named_arrays as na

Expand All @@ -12,6 +13,7 @@
"scatter",
"imshow",
"pcolormesh",
"text",
]


Expand Down Expand Up @@ -627,3 +629,72 @@
components=components,
**kwargs,
)


def text(
x: float | u.Quantity | na.AbstractScalar,
y: float | u.Quantity | na.AbstractScalar,
s: str | na.AbstractScalar,
ax: None | matplotlib.axes.Axes | na.AbstractScalar = None,
**kwargs,
) -> na.AbstractScalar:
"""
A thin wrapper around :meth:`matplotlib.axes.Axes.text` for named arrays.

Parameters
----------
x
The horizontal position of the text in data coordinates.
y
The vertical position of the text in data coordinates.
s
The text to plot.
ax
The matplotlib axes instance on which to plot the text.
kwargs
Additional keyword arguments to pass to :meth:`matplotlib.axes.Axes.text`.

Examples
--------

Plot an array of text values at different locations.

.. jupyter-execute::

import numpy as np
import matplotlib.pyplot as plt
import astropy.units as u
import named_arrays as na

# Create an array of azimuths where the next will be placed
azimuth = na.linspace(0, 360, axis="azimuth", num=4, endpoint=False) * u.deg

# Compute the x and y coordinates for the given azimuth
x = np.cos(azimuth)
y = np.sin(azimuth)

# Create an array of strings to plot at the chosen positions
s = na.ScalarArray(
ndarray=np.array(["East", "North", "West", "South"]),
axes="azimuth"
)

# Plot the array of strings
fig, ax = plt.subplots()
na.plt.text(
x=x,
y=y,
s=s,
ax=ax,
);
ax.set_xlim(-2, 2);
ax.set_ylim(-2, 2);
"""
return na._named_array_function(

Check warning on line 693 in named_arrays/plt.py

View check run for this annotation

Codecov / codecov/patch

named_arrays/plt.py#L693

Added line #L693 was not covered by tests
text,
x=x,
y=y,
s=s,
ax=ax,
**kwargs,
)
Loading