Skip to content

Commit

Permalink
use mpl triangulation parser
Browse files Browse the repository at this point in the history
  • Loading branch information
cvanelteren committed Jan 8, 2025
1 parent 5dc8b44 commit 5bb8ac1
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 39 deletions.
12 changes: 3 additions & 9 deletions ultraplot/axes/plot.py
Original file line number Diff line number Diff line change
Expand Up @@ -4408,9 +4408,7 @@ def tricontour(self, *args, **kwargs):
%(plot.tricontour)s
"""
kw = kwargs.copy()

# Parse inputs to get triangulation and z
triangulation, z = _parse_tricontourf_inputs(*args, **kwargs)
triangulation, z, args, kwargs = inputs._parse_triangulation_inputs(*args, **kwargs)

# Update kwargs and handle cmap
kw.update(_pop_props(kw, "collection"))
Expand Down Expand Up @@ -4440,9 +4438,7 @@ def tricontourf(self, *args, **kwargs):
%(plot.tricontourf)s
"""
kw = kwargs.copy()

# Parse inputs to get triangulation and z
triangulation, z = inputs._parse_tricontourf_inputs(*args, **kwargs)
triangulation, z, args, kw = inputs._parse_triangulation_inputs(*args, **kwargs)

# Update kwargs and handle contour parameters
kw.update(_pop_props(kw, "collection"))
Expand Down Expand Up @@ -4478,9 +4474,7 @@ def tripcolor(self, *args, **kwargs):
%(plot.tripcolor)s
"""
kw = kwargs.copy()

# Parse inputs to get triangulation and z
triangulation, z = _parse_tricontourf_inputs(*args, **kwargs)
triangulation, z, args, kw = inputs._parse_triangulation_inputs(*args, **kwargs)

# Update kwargs and handle cmap
kw.update(_pop_props(kw, "collection"))
Expand Down
38 changes: 8 additions & 30 deletions ultraplot/internals/inputs.py
Original file line number Diff line number Diff line change
Expand Up @@ -254,39 +254,17 @@ def _from_data(data, *args):
return args


def _parse_tricontourf_inputs(*args, **kwargs):
def _parse_triangulation_inputs(*args, **kwargs):
"""
Parse inputs for the tricontourf function and return triangulation and z values.
Supports the following input patterns:
1. (triangulation, z)
2. (x, y, z)
3. (x, y, triangulation, z)
Parse inputs using Matplotlib's `get_from_args_and_kwargs` method.
Returns a Triangulation object, z values, and updated args/kwargs.
"""
from matplotlib.tri import Triangulation

if len(args) == 2:
# (triangulation, z)
triangulation, z = args
if not isinstance(triangulation, Triangulation):
raise TypeError(
"First argument must be a Triangulation object when two arguments are provided."
)
elif len(args) == 3:
# (x, y, z)
x, y, z = args
triangulation = Triangulation(x, y, mask=kwargs.get("mask"))
elif len(args) == 4:
x, y, triangulation, z = args
if not isinstance(triangulation, Triangulation):
raise TypeError(
"Third argument must be a Triangulation object when four arguments are provided."
)
else:
raise ValueError(
"Invalid inputs. Provide (triangulation, z), (x, y, z), or (x, y, triangulation, z)."
)

return triangulation, z
triangulation, args, kwargs = Triangulation.get_from_args_and_kwargs(*args, **kwargs)
if not args:
raise ValueError("No z values provided. Provide at least one positional argument for z.")
z = args[0] # Assume the first remaining argument is z
return triangulation, z, args[1:], kwargs


def _preprocess_or_redirect(*keys, keywords=None, allow_extra=True):
Expand Down

0 comments on commit 5bb8ac1

Please sign in to comment.