FIX: show bars when the first location is nan by tacaswell · Pull Request #23751 · matplotlib/matplotlib · GitHub
Skip to content
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
14 changes: 7 additions & 7 deletions lib/matplotlib/axes/_axes.py
21 changes: 16 additions & 5 deletions lib/matplotlib/cbook/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -1703,19 +1703,29 @@ def safe_first_element(obj):
This is an type-independent way of obtaining the first element,
supporting both index access and the iterator protocol.
"""
return _safe_first_non_none(obj, skip_none=False)
return _safe_first_finite(obj, skip_nonfinite=False)


def _safe_first_non_none(obj, skip_none=True):
def _safe_first_finite(obj, *, skip_nonfinite=True):
"""
Return the first non-None element in *obj*.
Return the first non-None (and optionally finite) element in *obj*.

This is a method for internal use.

This is an type-independent way of obtaining the first non-None element,
supporting both index access and the iterator protocol.
The first non-None element will be obtained when skip_none is True.
"""
if skip_none is False:
def safe_isfinite(val):
if val is None:
return False
try:
return np.isfinite(val) if np.isscalar(val) else True
Comment thread
timhoffm marked this conversation as resolved.
except TypeError:
# This is something that numpy can not make heads or tails
# of, assume "finite"
return True
if skip_nonfinite is False:
if isinstance(obj, collections.abc.Iterator):
# needed to accept `array.flat` as input.
# np.flatiter reports as an instance of collections.Iterator
Expand All @@ -1730,12 +1740,13 @@ def _safe_first_non_none(obj, skip_none=True):
"as input")
return next(iter(obj))
elif isinstance(obj, np.flatiter):
# TODO do the finite filtering on this
return obj[0]
elif isinstance(obj, collections.abc.Iterator):
raise RuntimeError("matplotlib does not "
"support generators as input")
else:
return next(val for val in obj if val is not None)
return next(val for val in obj if safe_isfinite(val))


def sanitize_sequence(data):
Expand Down
2 changes: 1 addition & 1 deletion lib/matplotlib/dates.py
Original file line number Diff line number Diff line change
Expand Up @@ -1908,7 +1908,7 @@ def default_units(x, axis):
x = x.ravel()

try:
x = cbook._safe_first_non_none(x)
x = cbook._safe_first_finite(x)
except (TypeError, StopIteration):
pass

Expand Down
24 changes: 24 additions & 0 deletions lib/matplotlib/tests/test_axes.py
Original file line number Diff line number Diff line change
Expand Up @@ -8109,3 +8109,27 @@ def test_get_xticklabel():
for ind in range(10):
assert ax.get_xticklabels()[ind].get_text() == f'{ind}'
assert ax.get_yticklabels()[ind].get_text() == f'{ind}'


def test_bar_leading_nan():

barx = np.arange(3, dtype=float)
barheights = np.array([0.5, 1.5, 2.0])
barstarts = np.array([0.77]*3)

barx[0] = np.NaN

fig, ax = plt.subplots()

bars = ax.bar(barx, barheights, bottom=barstarts)

hbars = ax.barh(barx, barheights, left=barstarts)

for bar_set in (bars, hbars):
# the first bar should have a nan in the location
nanful, *rest = bar_set
assert (~np.isfinite(nanful.xy)).any()
assert np.isfinite(nanful.get_width())
for b in rest:
assert np.isfinite(b.xy).all()
assert np.isfinite(b.get_width())
6 changes: 3 additions & 3 deletions lib/matplotlib/tests/test_cbook.py
Original file line number Diff line number Diff line change
Expand Up @@ -602,7 +602,7 @@ def test_flatiter():
it = x.flat
assert 0 == next(it)
assert 1 == next(it)
ret = cbook._safe_first_non_none(it)
ret = cbook._safe_first_finite(it)
assert ret == 0

assert 0 == next(it)
Expand Down Expand Up @@ -758,7 +758,7 @@ def test_contiguous_regions():
def test_safe_first_element_pandas_series(pd):
# deliberately create a pandas series with index not starting from 0
s = pd.Series(range(5), index=range(10, 15))
actual = cbook._safe_first_non_none(s)
actual = cbook._safe_first_finite(s)
assert actual == 0


Expand Down Expand Up @@ -893,5 +893,5 @@ def test_format_approx():
def test_safe_first_element_with_none():
datetime_lst = [date.today() + timedelta(days=i) for i in range(10)]
datetime_lst[0] = None
actual = cbook._safe_first_non_none(datetime_lst)
actual = cbook._safe_first_finite(datetime_lst)
assert actual is not None and actual == datetime_lst[1]
2 changes: 1 addition & 1 deletion lib/matplotlib/units.py