8000 Merge pull request #4710 from tacaswell/fix_empty_scatter · matplotlib/matplotlib@1cfdc2e · GitHub
[go: up one dir, main page]

Skip to content

Commit 1cfdc2e

Browse files
committed
Merge pull request #4710 from tacaswell/fix_empty_scatter
FIX: gracefully deal with empty size lists
2 parents cefab28 + 0bc3c49 commit 1cfdc2e

File tree

2 files changed

+26
-2
lines changed

2 files changed

+26
-2
lines changed

lib/matplotlib/legend_handler.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -305,8 +305,11 @@ def get_numpoints(self, legend):
305305
def get_sizes(self, legend, orig_handle,
306306
xdescent, ydescent, width, height, fontsize):
307307
if self._sizes is None:
308-
size_max = max(orig_handle.get_sizes()) * legend.markerscale ** 2
309-
size_min = min(orig_handle.get_sizes()) * legend.markerscale ** 2
308+
handle_sizes = orig_handle.get_sizes()
309+
if not len(handle_sizes):
310+
handle_sizes = [1]
311+
size_max = max(handle_sizes) * legend.markerscale ** 2
312+
size_min = min(handle_sizes) * legend.markerscale ** 2
310313

311314
numpoints = self.get_numpoints(legend)
312315
if numpoints < 4:

lib/matplotlib/tests/test_legend.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,27 @@ def test_legend_stackplot():
240240
ax.legend(loc=0)
241241

242242

243+
@cleanup
244+
def test_nanscatter():
245+
fig, ax = plt.subplots()
246+
247+
h = ax.scatter([np.nan], [np.nan], marker="o",
248+
facecolor="r", edgecolor="r", s=3)
249+
250+
ax.legend([h], ["scatter"])
251+
252+
fig, ax = plt.subplots()
253+
for color in ['red', 'green', 'blue']:
254+
n = 750
255+
x, y = np.random.rand(2, n)
256+
scale = 200.0 * np.random.rand(n)
257+
ax.scatter(x, y, c=color, s=scale, label=color,
258+
alpha=0.3, edgecolors='none')
259+
260+
ax.legend()
261+
ax.grid(True)
262+
263+
243264
if __name__ == '__main__':
244265
import nose
245266
nose.runmodule(argv=['-s', '--with-doctest'], exit=False)

0 commit comments

Comments
 (0)
0