@@ -74,48 +74,43 @@ def _is_in_legend(obj):
7474def _draw_rectangle (data , obj , draw_options ):
7575 """Return the PGFPlots code for rectangles.
7676 """
77-
78- # Objects with labels are plot objects (from bar charts, etc).
79- # Even those without labels explicitly set have a label of
80- # "_nolegend_". Everything else should be skipped because
81- # they likely correspong to axis/legend objects which are
82- # handled by PGFPlots
77+ # Objects with labels are plot objects (from bar charts, etc). Even those without
78+ # labels explicitly set have a label of "_nolegend_". Everything else should be
79+ # skipped because they likely correspong to axis/legend objects which are handled by
80+ # PGFPlots
8381 label = obj .get_label ()
8482 if label == "" :
8583 return data , []
8684
87- # get real label, bar charts by default only give rectangles
88- # labels of "_nolegend_"
89- # See
90- # <http://stackoverflow.com/questions/35881290/how-to-get-the-label-on-bar-plot-stacked-bar-plot-in-matplotlib>
85+ # Get actual label, bar charts by default only give rectangles labels of
86+ # "_nolegend_". See <https://stackoverflow.com/q/35881290/353337>.
9187 handles , labels = obj .axes .get_legend_handles_labels ()
9288 labelsFound = [
9389 label for h , label in zip (handles , labels ) if obj in h .get_children ()
9490 ]
9591 if len (labelsFound ) == 1 :
9692 label = labelsFound [0 ]
9793
98- legend = ""
99- if label != "_nolegend_" and label not in data ["rectangle_legends" ]:
100- data ["rectangle_legends" ].add (label )
101- legend = ("\\ addlegendimage{{ybar,ybar legend,{}}};\n " ).format (
102- "," .join (draw_options )
103- )
104-
10594 left_lower_x = obj .get_x ()
10695 left_lower_y = obj .get_y ()
10796 ff = data ["float format" ]
10897 cont = (
109- "{} \\ draw[{}] (axis cs:" + ff + "," + ff + ") "
98+ "\\ draw[{}] (axis cs:" + ff + "," + ff + ") "
11099 "rectangle (axis cs:" + ff + "," + ff + ");\n "
111100 ).format (
112- legend ,
113101 "," .join (draw_options ),
114102 left_lower_x ,
115103 left_lower_y ,
116104 left_lower_x + obj .get_width (),
117105 left_lower_y + obj .get_height (),
118106 )
107+
108+ if label != "_nolegend_" and label not in data ["rectangle_legends" ]:
109+ data ["rectangle_legends" ].add (label )
110+ cont += "\\ addlegendimage{{ybar,ybar legend,{}}};\n " .format (
111+ "," .join (draw_options )
112+ )
113+ cont += "\\ addlegendentry{{{}}}\n \n " .format (label )
119114 return data , cont
120115
121116
0 commit comments