Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit65ca31c

Browse files
authored
Merge pull request#23956 from meeseeksmachine/auto-backport-of-pr-23751-on-v3.6.x
Backport PR#23751 on branch v3.6.x (FIX: show bars when the first location is nan)
2 parentsf751574 +7381e20 commit65ca31c

File tree

6 files changed

+52
-17
lines changed

6 files changed

+52
-17
lines changed

‎lib/matplotlib/axes/_axes.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2179,12 +2179,12 @@ def _convert_dx(dx, x0, xconv, convert):
21792179
# removes the units from unit packages like `pint` that
21802180
# wrap numpy arrays.
21812181
try:
2182-
x0=cbook._safe_first_non_none(x0)
2182+
x0=cbook._safe_first_finite(x0)
21832183
except (TypeError,IndexError,KeyError):
21842184
pass
21852185

21862186
try:
2187-
x=cbook._safe_first_non_none(xconv)
2187+
x=cbook._safe_first_finite(xconv)
21882188
except (TypeError,IndexError,KeyError):
21892189
x=xconv
21902190

@@ -2813,11 +2813,11 @@ def broken_barh(self, xranges, yrange, **kwargs):
28132813
"""
28142814
# process the unit information
28152815
iflen(xranges):
2816-
xdata=cbook._safe_first_non_none(xranges)
2816+
xdata=cbook._safe_first_finite(xranges)
28172817
else:
28182818
xdata=None
28192819
iflen(yrange):
2820-
ydata=cbook._safe_first_non_none(yrange)
2820+
ydata=cbook._safe_first_finite(yrange)
28212821
else:
28222822
ydata=None
28232823
self._process_unit_info(
@@ -3459,10 +3459,10 @@ def _upcast_err(err):
34593459
# safe_first_element because getitem is index-first not
34603460
# location first on pandas objects so err[0] almost always
34613461
# fails.
3462-
isinstance(cbook._safe_first_non_none(err),np.ndarray)
3462+
isinstance(cbook._safe_first_finite(err),np.ndarray)
34633463
):
34643464
# Get the type of the first element
3465-
atype=type(cbook._safe_first_non_none(err))
3465+
atype=type(cbook._safe_first_finite(err))
34663466
# Promote the outer container to match the inner container
34673467
ifatypeisnp.ndarray:
34683468
# Converts using np.asarray, because data cannot
@@ -4325,7 +4325,7 @@ def _parse_scatter_color_args(c, edgecolors, kwargs, xsize,
43254325
c_is_string_or_strings= (
43264326
isinstance(c,str)
43274327
or (np.iterable(c)andlen(c)>0
4328-
andisinstance(cbook._safe_first_non_none(c),str)))
4328+
andisinstance(cbook._safe_first_finite(c),str)))
43294329

43304330
definvalid_shape_exception(csize,xsize):
43314331
returnValueError(

‎lib/matplotlib/cbook/__init__.py

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1703,19 +1703,29 @@ def safe_first_element(obj):
17031703
This is an type-independent way of obtaining the first element,
17041704
supporting both index access and the iterator protocol.
17051705
"""
1706-
return_safe_first_non_none(obj,skip_none=False)
1706+
return_safe_first_finite(obj,skip_nonfinite=False)
17071707

17081708

1709-
def_safe_first_non_none(obj,skip_none=True):
1709+
def_safe_first_finite(obj,*,skip_nonfinite=True):
17101710
"""
1711-
Return the first non-None element in *obj*.
1711+
Return the first non-None (and optionally finite) element in *obj*.
1712+
17121713
This is a method for internal use.
17131714
17141715
This is an type-independent way of obtaining the first non-None element,
17151716
supporting both index access and the iterator protocol.
17161717
The first non-None element will be obtained when skip_none is True.
17171718
"""
1718-
ifskip_noneisFalse:
1719+
defsafe_isfinite(val):
1720+
ifvalisNone:
1721+
returnFalse
1722+
try:
1723+
returnnp.isfinite(val)ifnp.isscalar(val)elseTrue
1724+
exceptTypeError:
1725+
# This is something that numpy can not make heads or tails
1726+
# of, assume "finite"
1727+
returnTrue
1728+
ifskip_nonfiniteisFalse:
17191729
ifisinstance(obj,collections.abc.Iterator):
17201730
# needed to accept `array.flat` as input.
17211731
# np.flatiter reports as an instance of collections.Iterator
@@ -1730,12 +1740,13 @@ def _safe_first_non_none(obj, skip_none=True):
17301740
"as input")
17311741
returnnext(iter(obj))
17321742
elifisinstance(obj,np.flatiter):
1743+
# TODO do the finite filtering on this
17331744
returnobj[0]
17341745
elifisinstance(obj,collections.abc.Iterator):
17351746
raiseRuntimeError("matplotlib does not "
17361747
"support generators as input")
17371748
else:
1738-
returnnext(valforvalinobjifvalisnotNone)
1749+
returnnext(valforvalinobjifsafe_isfinite(val))
17391750

17401751

17411752
defsanitize_sequence(data):

‎lib/matplotlib/dates.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1907,7 +1907,7 @@ def default_units(x, axis):
19071907
x=x.ravel()
19081908

19091909
try:
1910-
x=cbook._safe_first_non_none(x)
1910+
x=cbook._safe_first_finite(x)
19111911
except (TypeError,StopIteration):
19121912
pass
19131913

‎lib/matplotlib/tests/test_axes.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8109,3 +8109,27 @@ def test_get_xticklabel():
81098109
forindinrange(10):
81108110
assertax.get_xticklabels()[ind].get_text()==f'{ind}'
81118111
assertax.get_yticklabels()[ind].get_text()==f'{ind}'
8112+
8113+
8114+
deftest_bar_leading_nan():
8115+
8116+
barx=np.arange(3,dtype=float)
8117+
barheights=np.array([0.5,1.5,2.0])
8118+
barstarts=np.array([0.77]*3)
8119+
8120+
barx[0]=np.NaN
8121+
8122+
fig,ax=plt.subplots()
8123+
8124+
bars=ax.bar(barx,barheights,bottom=barstarts)
8125+
8126+
hbars=ax.barh(barx,barheights,left=barstarts)
8127+
8128+
forbar_setin (bars,hbars):
8129+
# the first bar should have a nan in the location
8130+
nanful,*rest=bar_set
8131+
assert (~np.isfinite(nanful.xy)).any()
8132+
assertnp.isfinite(nanful.get_width())
8133+
forbinrest:
8134+
assertnp.isfinite(b.xy).all()
8135+
assertnp.isfinite(b.get_width())

‎lib/matplotlib/tests/test_cbook.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -602,7 +602,7 @@ def test_flatiter():
602602
it=x.flat
603603
assert0==next(it)
604604
assert1==next(it)
605-
ret=cbook._safe_first_non_none(it)
605+
ret=cbook._safe_first_finite(it)
606606
assertret==0
607607

608608
assert0==next(it)
@@ -758,7 +758,7 @@ def test_contiguous_regions():
758758
deftest_safe_first_element_pandas_series(pd):
759759
# deliberately create a pandas series with index not starting from 0
760760
s=pd.Series(range(5),index=range(10,15))
761-
actual=cbook._safe_first_non_none(s)
761+
actual=cbook._safe_first_finite(s)
762762
assertactual==0
763763

764764

@@ -893,5 +893,5 @@ def test_format_approx():
893893
deftest_safe_first_element_with_none():
894894
datetime_lst= [date.today()+timedelta(days=i)foriinrange(10)]
895895
datetime_lst[0]=None
896-
actual=cbook._safe_first_non_none(datetime_lst)
896+
actual=cbook._safe_first_finite(datetime_lst)
897897
assertactualisnotNoneandactual==datetime_lst[1]

‎lib/matplotlib/units.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ def get_converter(self, x):
197197
exceptKeyError:
198198
pass
199199
try:# If cache lookup fails, look up based on first element...
200-
first=cbook._safe_first_non_none(x)
200+
first=cbook._safe_first_finite(x)
201201
except (TypeError,StopIteration):
202202
pass
203203
else:

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp