From 39ae61ad695f6d0f430ec6febbdc0edfa41296c4 Mon Sep 17 00:00:00 2001 From: Yong Cho Yin Date: Thu, 28 Feb 2019 17:56:52 -0500 Subject: [PATCH 1/3] FIX: legend crashes when bar chart is empty with label --- lib/matplotlib/legend_handler.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/lib/matplotlib/legend_handler.py b/lib/matplotlib/legend_handler.py index b94fc679ce16..0d66105af55e 100644 --- a/lib/matplotlib/legend_handler.py +++ b/lib/matplotlib/legend_handler.py @@ -35,7 +35,9 @@ def legend_artist(self, legend, orig_handle, fontsize, handlebox) def update_from_first_child(tgt, src): - tgt.update_from(src.get_children()[0]) + first_child = next(iter(src.get_children()), None) + if first_child is not None: + tgt.update_from(first_child) class HandlerBase(object): From 33d8f8eb0035e232f205f097d10f3904fee4845c Mon Sep 17 00:00:00 2001 From: Yong Cho Yin Date: Fri, 1 Mar 2019 19:38:35 -0500 Subject: [PATCH 2/3] add test with generating legend with empty bar chart --- .../test_legend/empty_bar_legend.svg | 381 ++++++++++++++++++ lib/matplotlib/tests/test_legend.py | 8 + 2 files changed, 389 insertions(+) create mode 100644 lib/matplotlib/tests/baseline_images/test_legend/empty_bar_legend.svg diff --git a/lib/matplotlib/tests/baseline_images/test_legend/empty_bar_legend.svg b/lib/matplotlib/tests/baseline_images/test_legend/empty_bar_legend.svg new file mode 100644 index 000000000000..6de5545babe0 --- /dev/null +++ b/lib/matplotlib/tests/baseline_images/test_legend/empty_bar_legend.svg @@ -0,0 +1,381 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/lib/matplotlib/tests/test_legend.py b/lib/matplotlib/tests/test_legend.py index b4bcecde81fa..0250ceea69e1 100644 --- a/lib/matplotlib/tests/test_legend.py +++ b/lib/matplotlib/tests/test_legend.py @@ -477,6 +477,14 @@ def test_handler_numpoints(): ax.legend(numpoints=0.5) +@image_comparison(baseline_images=['empty_bar_legend'], extensions=['svg']) +def test_empty_bar_chart_with_legend(): + """Test legend when bar chart is empty with a label.""" + # related to issue #13003 + plt.bar([], [], label='test') + plt.legend() + + def test_shadow_framealpha(): # Test if framealpha is activated when shadow is True # and framealpha is not explicitly passed''' From 528bb02a1abdc069e34d3b224009dd1aec7b079f Mon Sep 17 00:00:00 2001 From: Yong Cho Yin Date: Sat, 2 Mar 2019 13:09:57 -0500 Subject: [PATCH 3/3] remove image_comparison from legend test case --- .../test_legend/empty_bar_legend.svg | 381 ------------------ lib/matplotlib/tests/test_legend.py | 4 +- 2 files changed, 2 insertions(+), 383 deletions(-) delete mode 100644 lib/matplotlib/tests/baseline_images/test_legend/empty_bar_legend.svg diff --git a/lib/matplotlib/tests/baseline_images/test_legend/empty_bar_legend.svg b/lib/matplotlib/tests/baseline_images/test_legend/empty_bar_legend.svg deleted file mode 100644 index 6de5545babe0..000000000000 --- a/lib/matplotlib/tests/baseline_images/test_legend/empty_bar_legend.svg +++ /dev/null @@ -1,381 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/lib/matplotlib/tests/test_legend.py b/lib/matplotlib/tests/test_legend.py index 0250ceea69e1..beaa312ff4fe 100644 --- a/lib/matplotlib/tests/test_legend.py +++ b/lib/matplotlib/tests/test_legend.py @@ -477,10 +477,10 @@ def test_handler_numpoints(): ax.legend(numpoints=0.5) -@image_comparison(baseline_images=['empty_bar_legend'], extensions=['svg']) def test_empty_bar_chart_with_legend(): """Test legend when bar chart is empty with a label.""" - # related to issue #13003 + # related to issue #13003. Calling plt.legend() should not + # raise an IndexError. plt.bar([], [], label='test') plt.legend()