Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit ce5533c

Browse files
committed
Fix index out of bound error for testing first element of iterable.
1 parent f288977 commit ce5533c

File tree

2 files changed

+9
-1
lines changed

2 files changed

+9
-1
lines changed

lib/matplotlib/axes/_axes.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4188,7 +4188,8 @@ def scatter(self, x, y, s=None, c=None, marker=None, cmap=None, norm=None,
41884188
co is not None or
41894189
isinstance(c, str) or
41904190
(isinstance(c, collections.Iterable) and
4191-
isinstance(c[0], str))):
4191+
len(c) > 0 and
4192+
isinstance(cbook.safe_first_element(c), str))):
41924193
c_array = None
41934194
else:
41944195
try: # First, does 'c' look suitable for value-mapping?

lib/matplotlib/tests/test_cbook.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -526,3 +526,10 @@ def test_contiguous_regions():
526526
assert cbook.contiguous_regions([False]*5) == []
527527
# Empty mask
528528
assert cbook.contiguous_regions([]) == []
529+
530+
531+
def test_safe_first_element_pandas_series(pd):
532+
# delibrately create a pandas series with index not starting from 0
533+
s = pd.Series(range(5), index=range(10, 15))
534+
actual = cbook.safe_first_element(s)
535+
assert actual == 0

0 commit comments

Comments
 (0)