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

Alpha argument added to BezierTriangle and CurvedPolygon plotting functions #296

Merged
merged 2 commits into from
Aug 10, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
5 changes: 3 additions & 2 deletions src/python/bezier/_plot_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,14 +62,15 @@ def add_plot_boundary(ax, padding=0.125):
)


def add_patch(ax, color, pts_per_edge, *edges):
def add_patch(ax, color, pts_per_edge, *edges, alpha=None):
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should just be

Suggested change
def add_patch(ax, color, pts_per_edge, *edges, alpha=None):
def add_patch(ax, color, pts_per_edge, *edges, alpha=0.625):

based on the usage below, right?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking at the rest of the changes, I think it's fine to default to alpha=0.625 everywhere.

Though I appreciate why you went with alpha=None as the API choice.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Np, I'll switch over to 0.625 then

"""Add a polygonal surface patch to a plot.

Args:
ax (matplotlib.artist.Artist): A matplotlib axis.
color (Tuple[float, float, float]): Color as RGB profile.
pts_per_edge (int): Number of points to use in polygonal
approximation of edge.
alpha (Optional[float]): Alpha value of patch centre, between 0 and 1 inclusive.
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
alpha (Optional[float]): Alpha value of patch centre, between 0 and 1 inclusive.
alpha (Optional[float]): Alpha value of patch center, between 0 and 1 inclusive.

American spelling? For consistency?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah yep, thanks!

I'm an aussie, so that spelling was mostly muscle memory for me

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No worries!

*edges (Tuple[~bezier.curve.Curve, ...]): Curved edges defining
a boundary.
"""
Expand Down Expand Up @@ -99,6 +100,6 @@ def add_patch(ax, color, pts_per_edge, *edges):
# column but ``Path()`` wants ``x-y`` points in each row.
path = _path_mod.Path(polygon.T)
patch = patches.PathPatch(
path, facecolor=color, edgecolor=color, alpha=0.625
path, facecolor=color, edgecolor=color, alpha=(alpha or 0.625)
)
ax.add_patch(patch)
5 changes: 3 additions & 2 deletions src/python/bezier/curved_polygon.py
Original file line number Diff line number Diff line change
Expand Up @@ -259,20 +259,21 @@ def __repr__(self):
"""
return f"<{self.__class__.__name__} (num_sides={self._num_sides:d})>"

def plot(self, pts_per_edge, color=None, ax=None):
def plot(self, pts_per_edge, color=None, ax=None, alpha=None):
"""Plot the current curved polygon.

Args:
pts_per_edge (int): Number of points to plot per curved edge.
color (Optional[Tuple[float, float, float]]): Color as RGB profile.
ax (Optional[matplotlib.artist.Artist]): matplotlib axis object
to add plot to.
alpha (Optional[float]): Alpha value of patch centre, between 0 and 1 inclusive.
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
alpha (Optional[float]): Alpha value of patch centre, between 0 and 1 inclusive.
alpha (Optional[float]): Alpha value of patch center, between 0 and 1 inclusive.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll get this one too, thanks!


Returns:
matplotlib.artist.Artist: The axis containing the plot. This
may be a newly created axis.
"""
if ax is None:
ax = _plot_helpers.new_axis()
_plot_helpers.add_patch(ax, color, pts_per_edge, *self._edges)
_plot_helpers.add_patch(ax, color, pts_per_edge, *self._edges, alpha=alpha)
return ax
5 changes: 3 additions & 2 deletions src/python/bezier/triangle.py
Original file line number Diff line number Diff line change
Expand Up @@ -701,7 +701,7 @@ def evaluate_cartesian_multi(self, param_vals, verify=True):
self._nodes, self._degree, param_vals, self._dimension
)

def plot(self, pts_per_edge, color=None, ax=None, with_nodes=False):
def plot(self, pts_per_edge, color=None, ax=None, with_nodes=False, alpha=None):
"""Plot the current triangle.

Args:
Expand All @@ -711,6 +711,7 @@ def plot(self, pts_per_edge, color=None, ax=None, with_nodes=False):
to add plot to.
with_nodes (Optional[bool]): Determines if the control points
should be added to the plot. Off by default.
alpha (Optional[float]): Alpha value of patch centre, between 0 and 1 inclusive.

Returns:
matplotlib.artist.Artist: The axis containing the plot. This
Expand All @@ -728,7 +729,7 @@ def plot(self, pts_per_edge, color=None, ax=None, with_nodes=False):

if ax is None:
ax = _plot_helpers.new_axis()
_plot_helpers.add_patch(ax, color, pts_per_edge, *self._get_edges())
_plot_helpers.add_patch(ax, color, pts_per_edge, *self._get_edges(), alpha=alpha)
if with_nodes:
ax.plot(
self._nodes[0, :],
Expand Down
8 changes: 4 additions & 4 deletions tests/unit/test__plot_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,10 +86,10 @@ def test_with_padding(self):

class Test_add_patch(utils.NumPyTestCase):
@staticmethod
def _call_function_under_test(ax, color, pts_per_edge, *edges):
def _call_function_under_test(ax, color, pts_per_edge, *edges, alpha=None):
from bezier import _plot_helpers

return _plot_helpers.add_patch(ax, color, pts_per_edge, *edges)
return _plot_helpers.add_patch(ax, color, pts_per_edge, *edges, alpha=alpha)

def _path_val(self, path, expected_transpose):
self.assertEqual(path.Path.call_count, 1)
Expand Down Expand Up @@ -143,7 +143,7 @@ def test_it(self):
"matplotlib.path": path,
}
func = functools.partial(
self._call_function_under_test, ax, color, pts_per_edge, *edges
self._call_function_under_test, ax, color, pts_per_edge, *edges, alpha=0.5
)
result = run_fake_modules(modules, func)
self.assertIsNone(result)
Expand All @@ -153,7 +153,7 @@ def test_it(self):
path.Path.return_value,
facecolor=color,
edgecolor=color,
alpha=0.625,
alpha=0.5,
)
ax.add_patch.assert_called_once_with(patches.PathPatch.return_value)
line.get_color.assert_called_once_with()
Expand Down
7 changes: 4 additions & 3 deletions tests/unit/test_curved_polygon.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ def test_plot_defaults(self, add_patch_mock, new_axis_mock):
# Verify mocks.
new_axis_mock.assert_called_once_with()
add_patch_mock.assert_called_once_with(
ax, None, pts_per_edge, *curved_poly._edges
ax, None, pts_per_edge, *curved_poly._edges, alpha=None
)

@unittest.mock.patch("bezier._plot_helpers.new_axis")
Expand All @@ -140,10 +140,11 @@ def test_plot_explicit(self, add_patch_mock, new_axis_mock):
color = (0.5, 0.5, 0.5)
curved_poly = self._make_default()
pts_per_edge = 16
result = curved_poly.plot(pts_per_edge, color=color, ax=ax)
alpha = 0.5
result = curved_poly.plot(pts_per_edge, color=color, ax=ax, alpha=alpha)
self.assertIs(result, ax)
# Verify mocks.
new_axis_mock.assert_not_called()
add_patch_mock.assert_called_once_with(
ax, color, pts_per_edge, *curved_poly._edges
ax, color, pts_per_edge, *curved_poly._edges, alpha=alpha
)
7 changes: 4 additions & 3 deletions tests/unit/test_triangle.py
Original file line number Diff line number Diff line change
Expand Up @@ -463,7 +463,7 @@ def test_plot_defaults(self, add_patch_mock, new_axis_mock):
# Verify mocks.
new_axis_mock.assert_called_once_with()
add_patch_mock.assert_called_once_with(
ax, None, pts_per_edge, *curve._edges
ax, None, pts_per_edge, *curve._edges, alpha=None
)

@unittest.mock.patch("bezier._plot_helpers.new_axis")
Expand All @@ -473,12 +473,13 @@ def test_plot_explicit(self, add_patch_mock, new_axis_mock):
color = (0.5, 0.5, 0.5)
curve = self._make_one(self.UNIT_TRIANGLE, 1, copy=False)
pts_per_edge = 16
result = curve.plot(pts_per_edge, color=color, ax=ax, with_nodes=True)
alpha = 0.5
result = curve.plot(pts_per_edge, color=color, ax=ax, with_nodes=True, alpha=alpha)
self.assertIs(result, ax)
# Verify mocks.
new_axis_mock.assert_not_called()
add_patch_mock.assert_called_once_with(
ax, color, pts_per_edge, *curve._edges
ax, color, pts_per_edge, *curve._edges, alpha=alpha
)
# Check the call to ax.plot(). We can't assert_any_call()
# since == breaks on NumPy arrays.
Expand Down