Skip to content

Commit cc761ea

Browse files
committed
Added fallback, api change note and test
1 parent 0bf4d39 commit cc761ea

File tree

4 files changed

+17
-2
lines changed

4 files changed

+17
-2
lines changed
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Invalid linewidths coerced to 0
2+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
3+
Negative or non-finite (NaN, infinity) linewidth inputs are now
4+
automatically coerced to 0.

lib/matplotlib/backend_bases.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -928,8 +928,11 @@ def set_joinstyle(self, js):
928928
self._joinstyle = JoinStyle(js)
929929

930930
def set_linewidth(self, w):
931-
"""Set the linewidth in points."""
932-
self._linewidth = float(w)
931+
"""
932+
Set the linewidth in points.
933+
Any infinite, NaN, or negative values will be set to 0.
934+
"""
935+
self._linewidth = float(w) if np.isfinite(w) and w > 0 else 0.0
933936

934937
def set_url(self, url):
935938
"""Set the url for links in compatible backends."""

lib/matplotlib/collections.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -664,6 +664,7 @@ def set_linewidth(self, lw):
664664
# scale all of the dash patterns.
665665
self._linewidths, self._linestyles = self._bcast_lwls(
666666
self._us_lw, self._us_linestyles)
667+
self._linewidths = np.nan_to_num(self._linewidths, nan=0, posinf=0).clip(0)
667668
self.stale = True
668669

669670
def set_linestyle(self, ls):

lib/matplotlib/tests/test_collections.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1538,3 +1538,10 @@ def mock_draw_path_collection(self, gc, master_transform, paths, all_transforms,
15381538
ax.add_collection(coll)
15391539

15401540
plt.draw()
1541+
1542+
1543+
@pytest.mark.parametrize('lw', [np.nan, -5.0, np.inf])
1544+
def test_invalid_linewidths(lw):
1545+
lc = LineCollection([[(0, 0), (1, 1)]])
1546+
lc.set_linewidth(lw)
1547+
assert lc.get_linewidth() == 0.0

0 commit comments

Comments
 (0)