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

Skip to content

Commit e6958c3

Browse files
author
Francis Colas
committed
adding legend handler for PolyCollection
1 parent 0f049f7 commit e6958c3

3 files changed

Lines changed: 45 additions & 3 deletions

File tree

lib/matplotlib/collections.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -658,6 +658,10 @@ def update_scalarmappable(self):
658658
elif self._is_stroked:
659659
self._edgecolors = self.to_rgba(self._A, self._alpha)
660660

661+
def get_fill(self):
662+
'return whether fill is set'
663+
return self._is_filled
664+
661665
def update_from(self, other):
662666
'copy properties from other to self'
663667

lib/matplotlib/legend.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
from matplotlib.lines import Line2D
4040
from matplotlib.patches import Patch, Rectangle, Shadow, FancyBboxPatch
4141
from matplotlib.collections import LineCollection, RegularPolyCollection, \
42-
CircleCollection, PathCollection
42+
CircleCollection, PathCollection, PolyCollection
4343
from matplotlib.transforms import Bbox, BboxBase, TransformedBbox
4444
from matplotlib.transforms import BboxTransformTo, BboxTransformFrom
4545

@@ -487,7 +487,8 @@ def _approx_text_height(self, renderer=None):
487487
BarContainer: legend_handler.HandlerPatch(
488488
update_func=legend_handler.update_from_first_child),
489489
tuple: legend_handler.HandlerTuple(),
490-
PathCollection: legend_handler.HandlerPathCollection()
490+
PathCollection: legend_handler.HandlerPathCollection(),
491+
PolyCollection: legend_handler.HandlerPolyCollection()
491492
}
492493

493494
# (get|set|update)_default_handler_maps are public interfaces to

lib/matplotlib/legend_handler.py

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@ def legend_artist(self, legend, orig_handle, fontsize, handlebox):
3535
from matplotlib.lines import Line2D
3636
from matplotlib.patches import Rectangle
3737
import matplotlib.collections as mcoll
38-
38+
import matplotlib.colors as mcolors
39+
import matplotlib.artist as artist
3940

4041
def update_from_first_child(tgt, src):
4142
tgt.update_from(src.get_children()[0])
@@ -581,3 +582,39 @@ def create_artists(self, legend, orig_handle,
581582
a_list.extend(_a_list)
582583

583584
return a_list
585+
586+
587+
class HandlerPolyCollection(HandlerBase):
588+
"""
589+
Handler for PolyCollection used in fill_between and stackplot.
590+
"""
591+
def _update_prop(self, legend_handle, orig_handle):
592+
def first_color(colors):
593+
colors = mcolors.colorConverter.to_rgba_array(colors)
594+
if len(colors):
595+
return colors[0]
596+
else:
597+
return "none"
598+
def get_first(prop_array):
599+
if len(prop_array):
600+
return prop_array[0]
601+
else:
602+
return None
603+
#artist.Artist.update_from(legend_handle, orig_handle)
604+
legend_handle.set_edgecolor(first_color(orig_handle.get_edgecolor()))
605+
legend_handle.set_facecolor(first_color(orig_handle.get_facecolor()))
606+
legend_handle.set_fill(orig_handle.get_fill())
607+
legend_handle.set_hatch(orig_handle.get_hatch())
608+
legend_handle.set_linewidth(get_first(orig_handle.get_linewidths()))
609+
legend_handle.set_linestyle(get_first(orig_handle.get_linestyles()))
610+
legend_handle.set_transform(get_first(orig_handle.get_transforms()))
611+
legend_handle.set_figure(orig_handle.get_figure())
612+
legend_handle.set_alpha(orig_handle.get_alpha())
613+
614+
def create_artists(self, legend, orig_handle,
615+
xdescent, ydescent, width, height, fontsize, trans):
616+
p = Rectangle(xy=(-xdescent, -ydescent),
617+
width=width, height=height)
618+
self.update_prop(p, orig_handle, legend)
619+
p.set_transform(trans)
620+
return [p]

0 commit comments

Comments
 (0)