Traceback (most recent call last):
File "test.py", line 3, in <module>
plt.legend()
File "/opt/conda/lib/python3.6/site-packages/matplotlib/pyplot.py", line 3823, in legend
ret = gca().legend(*args, **kwargs)
File "/opt/conda/lib/python3.6/site-packages/matplotlib/axes/_axes.py", line 557, in legend
self.legend_ = mlegend.Legend(self, handles, labels, **kwargs)
File "/opt/conda/lib/python3.6/site-packages/matplotlib/legend.py", line 699, in __init__
self._init_legend_box(handles, labels, markerfirst)
File "/opt/conda/lib/python3.6/site-packages/matplotlib/legend.py", line 954, in _init_legend_box
fontsize, handlebox))
File "/opt/conda/lib/python3.6/site-packages/matplotlib/legend_handler.py", line 119, in legend_artist
fontsize, handlebox.get_transform())
File "/opt/conda/lib/python3.6/site-packages/matplotlib/legend_handler.py", line 303, in create_artists
self.update_prop(p, orig_handle, legend)
File "/opt/conda/lib/python3.6/site-packages/matplotlib/legend_handler.py", line 76, in update_prop
self._update_prop(legend_handle, orig_handle)
File "/opt/conda/lib/python3.6/site-packages/matplotlib/legend_handler.py", line 69, in _update_prop
self._update_prop_func(legend_handle, orig_handle)
File "/opt/conda/lib/python3.6/site-packages/matplotlib/legend_handler.py", line 42, in update_from_first_child
tgt.update_from(src.get_children()[0])
IndexError: list index out of range
Hi, this is my first time contributing to a large scale open source project, please do pardon my beginner knowledge.
The error originates the function update_from_first_child in legend_handler, which is called by the legend handler of BarContainer defined at legend.py:682
Activity
timhoffm commented on Dec 17, 2018
Thanks for the report.
Current behavior with bar()
prints ([<BarContainer object of 0 artists>], ['foo'])
Current behavior with plot() and raises the above error.
prints ([<matplotlib.lines.Line2D object at 0x7f14fb7c10f0>], ['foo']) and renders an empty plot with a legend.

Not sure we can bar() behave similarly.
choyiny commented on Feb 26, 2019
Hi, this is my first time contributing to a large scale open source project, please do pardon my beginner knowledge.
The error originates the function update_from_first_child in legend_handler, which is called by the legend handler of BarContainer defined at legend.py:682
def update_from_first_child(tgt, src): tgt.update_from(src.get_children()[0])In the case where there is no children from the original handler src, it will result in an IndexError. A simple fix like so would fix the issue:
def update_from_first_child(tgt, src): try: tgt.update_from(src.get_children()[0]) except IndexError: passSimilarly, we could first check the length of src.get_children() then only update if there is something.
Could anyone please advise if this is the correct approach to this particular issue?
VanDavv commented on Feb 26, 2019
@choyiny I'd rather do
And handle None value either here or in tgt.update_from method