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

Skip to content

Commit bcb372b

Browse files
authored
Merge pull request #12673 from esvhd/axes_iob
Fix for _axes.scatter() array index out of bound error
2 parents d779b8b + e2ca262 commit bcb372b

File tree

3 files changed

+26
-3
lines changed

3 files changed

+26
-3
lines changed

lib/matplotlib/axes/_axes.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4195,8 +4195,9 @@ def scatter(self, x, y, s=None, c=None, marker=None, cmap=None, norm=None,
41954195
if (c_none or
41964196
co is not None or
41974197
isinstance(c, str) or
4198-
(isinstance(c, collections.abc.Iterable) and
4199-
isinstance(c[0], str))):
4198+
(isinstance(c, collections.Iterable) and
4199+
len(c) > 0 and
4200+
isinstance(cbook.safe_first_element(c), str))):
42004201
c_array = None
42014202
else:
42024203
try: # First, does 'c' look suitable for value-mapping?

lib/matplotlib/tests/test_axes.py

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5828,7 +5828,7 @@ def test_spines_properbbox_after_zoom():
58285828
bb = ax.spines['bottom'].get_window_extent(fig.canvas.get_renderer())
58295829
# this is what zoom calls:
58305830
ax._set_view_from_bbox((320, 320, 500, 500), 'in',
5831-
None, False, False)
5831+
None, False, False)
58325832
bb2 = ax.spines['bottom'].get_window_extent(fig.canvas.get_renderer())
58335833
np.testing.assert_allclose(bb.get_points(), bb2.get_points(), rtol=1e-6)
58345834

@@ -5856,3 +5856,18 @@ def test_gettightbbox_ignoreNaN():
58565856
t = ax.text(np.NaN, 1, 'Boo')
58575857
renderer = fig.canvas.get_renderer()
58585858
np.testing.assert_allclose(ax.get_tightbbox(renderer).width, 532.444444)
5859+
5860+
5861+
def test_scatter_series_non_zero_index(pd):
5862+
# create non-zero index
5863+
ids = range(10, 18)
5864+
x = pd.Series(np.random.uniform(size=8), index=ids)
5865+
y = pd.Series(np.random.uniform(size=8), index=ids)
5866+
c = pd.Series([1, 1, 1, 1, 1, 0, 0, 0], index=ids)
5867+
plt.scatter(x, y, c)
5868+
5869+
5870+
def test_scatter_empty_data():
5871+
# making sure this does not raise an exception
5872+
plt.scatter([], [])
5873+
plt.scatter([], [], s=[], c=[])

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)