8000 Merge pull request #12673 from esvhd/axes_iob · matplotlib/matplotlib@a305c53 · GitHub
[go: up one dir, main page]

Skip to content

Commit a305c53

Browse files
phobsonImportanceOfBeingErnest
authored andcommitted
Merge pull request #12673 from esvhd/axes_iob
Fix for _axes.scatter() array index out of bound error
1 parent 7e1e701 commit a305c53

File tree

3 files changed

+70
-3
lines changed

3 files changed

+70
-3
lines changed

lib/matplotlib/axes/_axes.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4199,8 +4199,9 @@ def scatter(self, x, y, s=None, c=None, marker=None, cmap=None, norm=None,
41994199
if (c_none or
42004200
co is not None or
42014201
isinstance(c, str) or
4202-
(isinstance(c, collections.abc.Iterable) and
4203-
isinstance(c[0], str))):
4202+
(isinstance(c, collections.Iterable) and
4203+
len(c) > 0 and
4204+
isinstance(cbook.safe_first_element(c), str))):
42044205
c_array = None
42054206
else:
42064207
try: # First, does 'c' look suitable for value-mapping?

lib/matplotlib/tests/test_axes.py

Lines changed: 16 additions & 1 deletion
Original file l 8000 ine numberDiff line numberDiff line change
@@ -5820,7 +5820,7 @@ def test_spines_properbbox_after_zoom():
58205820
bb = ax.spines['bottom'].get_window_extent(fig.canvas.get_renderer())
58215821
# this is what zoom calls:
58225822
ax._set_view_from_bbox((320, 320, 500, 500), 'in',
5823-
None, False, False)
5823+
None, False, False)
58245824
bb2 = ax.spines['bottom'].get_window_extent(fig.canvas.get_renderer())
58255825
np.testing.assert_allclose(bb.get_points(), bb2.get_points(), rtol=1e-6)
58265826

@@ -5848,3 +5848,18 @@ def test_gettightbbox_ignoreNaN():
58485848
t = ax.text(np.NaN, 1, 'Boo')
58495849
renderer = fig.canvas.get_renderer()
58505850
np.testing.assert_allclose(ax.get_tightbbox(renderer).width, 532.444444)
5851+
5852+
5853+
def test_scatter_series_non_zero_index(pd):
5854+
# create non-zero index
5855+
ids = range(10, 18)
5856+
x = pd.Series(np.random.uniform(size=8), index=ids)
5857+
y = pd.Series(np.random.uniform(size=8), index=ids)
5858+
c = pd.Series([1, 1, 1, 1, 1, 0, 0, 0], index=ids)
5859+
plt.scatter(x, y, c)
5860+
5861+
5862+
def test_scatter_empty_data():
5863+
# making sure this does not raise an exception
5864+
plt.scatter([], [])
5865+
plt.scatter([], [], s=[], c=[])

lib/matplotlib/tests/test_cbook.py

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -482,3 +482,54 @@ def test_flatiter():
482482

483483
assert 0 == next(it)
484484
assert 1 == next(it)
485+
486+
487+
def test_reshape2d():
488+
class dummy():
489+
pass
490+
x = [dummy() for j in range(5)]
491+
xnew = cbook._reshape_2D(x, 'x')
492+
assert np.shape(xnew) == (1, 5)
493+
494+
x = np.arange(5)
495+
xnew = cbook._reshape_2D(x, 'x')
496+
assert np.shape(xnew) == (1, 5)
497+
498+
x = [[dummy() for j in range(5)] for i in range(3)]
499+
xnew = cbook._reshape_2D(x, 'x')
500+
assert np.shape(xnew) == (3, 5)
501+
502+
# this is strange behaviour, but...
503+
x = np.random.rand(3, 5)
504+
xnew = cbook._reshape_2D(x, 'x')
505+
assert np.shape(xnew) == (5, 3)
506+
507+
508+
def test_contiguous_regions():
509+
a, b, c = 3, 4, 5
510+
# Starts and ends with True
511+
mask = [True]*a + [False]*b + [True]*c
512+
expected = [(0, a), (a+b, a+b+c)]
513+
assert cbook.contiguous_regions(mask) == expected
514+
d, e = 6, 7
515+
# Starts with True ends with False
516+
mask = mask + [False]*e
517+
assert cbook.contiguous_regions(mask) == expected
518+
# Starts with False ends with True
519+
mask = [False]*d + mask[:-e]
520+
expected = [(d, d+a), (d+a+b, d+a+b+c)]
521+
assert cbook.contiguous_regions(mask) == expected
522+
# Starts and ends with False
523+
mask = mask + [False]*e
524+
assert cbook.contiguous_regions(mask) == expected
525+
# No True in mask
526+
assert cbook.contiguous_regions([False]*5) == []
527+
# Empty mask
528+
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)
0