@@ -773,7 +773,11 @@ def adjust_subplots(fig, axes, trellis, layers):
773
773
legend = dictionary_union (legend , layer .legend )
774
774
patches = []
775
775
labels = []
776
- for key in sorted (legend .keys (), key = lambda tup : (tup [1 ], tup [3 ])):
776
+ if len (legend .keys ()[0 ]) == 2 :
777
+ key_function = lambda tup : (tup [1 ])
778
+ else :
779
+ key_function = lambda tup : (tup [1 ], tup [3 ])
780
+ for key in sorted (legend .keys (), key = key_function ):
777
781
value = legend [key ]
778
782
patches .append (value )
779
783
if len (key ) == 2 :
@@ -834,6 +838,28 @@ def render(self, fig=None):
834
838
new_layers = sequence_layers (self .layers )
835
839
for layer in new_layers :
836
840
layer .work (fig = fig )
841
+ legend = {}
842
+ for layer in new_layers :
843
+ legend = dictionary_union (legend , layer .legend )
844
+ patches = []
845
+ labels = []
846
+ if len (legend .keys ()[0 ]) == 2 :
847
+ key_function = lambda tup : (tup [1 ])
848
+ else :
849
+ key_function = lambda tup : (tup [1 ], tup [3 ])
850
+ for key in sorted (legend .keys (), key = key_function ):
851
+ value = legend [key ]
852
+ patches .append (value )
853
+ if len (key ) == 2 :
854
+ col , val = key
855
+ labels .append ("%s" % str (val ))
856
+ elif len (key ) == 4 :
857
+ col1 , val1 , col2 , val2 = key
858
+ labels .append ("%s, %s" % (str (val1 ), str (val2 )))
859
+ else :
860
+ raise ValueError ("Maximum 2 categorical attributes to display a lengend of" )
861
+ if len (legend ):
862
+ fig .legend (patches , labels , loc = 'upper right' )
837
863
else :
838
864
# We have a trellised plot.
839
865
# First let's remove all other TrellisGrid instances from the layer list,
0 commit comments