Skip to content

Commit eaf3472

Browse files
committed
Make sure legend displays with non-trellised plots and fix a bug with legend sorting
1 parent 07f1ce0 commit eaf3472

File tree

1 file changed

+27
-1
lines changed

1 file changed

+27
-1
lines changed

pandas/tools/rplot.py

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -773,7 +773,11 @@ def adjust_subplots(fig, axes, trellis, layers):
773773
legend = dictionary_union(legend, layer.legend)
774774
patches = []
775775
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):
777781
value = legend[key]
778782
patches.append(value)
779783
if len(key) == 2:
@@ -834,6 +838,28 @@ def render(self, fig=None):
834838
new_layers = sequence_layers(self.layers)
835839
for layer in new_layers:
836840
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')
837863
else:
838864
# We have a trellised plot.
839865
# First let's remove all other TrellisGrid instances from the layer list,

0 commit comments

Comments
 (0)