Skip to content

Commit 4e49c1b

Browse files
committed
fix for barchart legends
1 parent f81bfea commit 4e49c1b

File tree

4 files changed

+34
-25
lines changed

4 files changed

+34
-25
lines changed

matplotlib2tikz/line2d.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -150,9 +150,11 @@ def draw_linecollection(data, obj):
150150
options.append(linestyle)
151151

152152
# TODO what about masks?
153-
data, cont = mypath.draw_path(data, path, draw_options=options, simplify=False)
153+
data, cont, _, _ = mypath.draw_path(
154+
data, path, draw_options=options, simplify=False
155+
)
154156

155-
content.append(cont)
157+
content.append(cont + "\n")
156158

157159
return data, content
158160

matplotlib2tikz/patch.py

Lines changed: 14 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -74,48 +74,43 @@ def _is_in_legend(obj):
7474
def _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

test/test_barchart_legend.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,3 +37,10 @@ def plot():
3737
def test():
3838
assert_equality(plot, "test_barchart_legend_reference.tex")
3939
return
40+
41+
42+
if __name__ == "__main__":
43+
import helpers
44+
45+
helpers.compare_mpl_latex(plot)
46+
# helpers.print_tree(plot())

test/test_barchart_legend_reference.tex

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
\begin{axis}[
44
legend cell align={left},
5-
legend entries={{Data 1},{Data 2},{Data 3}},
65
legend style={draw=white!80.0!black},
76
tick align=outside,
87
tick pos=left,
@@ -11,16 +10,22 @@
1110
y grid style={white!69.01960784313725!black},
1211
ymin=0, ymax=5.25
1312
]
14-
\addlegendimage{ybar,ybar legend,fill=blue,draw opacity=0};
1513
\draw[fill=blue,draw opacity=0] (axis cs:-0.375,0) rectangle (axis cs:-0.125,1);
14+
\addlegendimage{ybar,ybar legend,fill=blue,draw opacity=0};
15+
\addlegendentry{Data 1}
16+
1617
\draw[fill=blue,draw opacity=0] (axis cs:0.625,0) rectangle (axis cs:0.875,2);
1718
\draw[fill=blue,draw opacity=0] (axis cs:1.625,0) rectangle (axis cs:1.875,3);
18-
\addlegendimage{ybar,ybar legend,fill=green!50.0!black,draw opacity=0};
1919
\draw[fill=green!50.0!black,draw opacity=0] (axis cs:-0.125,0) rectangle (axis cs:0.125,3);
20+
\addlegendimage{ybar,ybar legend,fill=green!50.0!black,draw opacity=0};
21+
\addlegendentry{Data 2}
22+
2023
\draw[fill=green!50.0!black,draw opacity=0] (axis cs:0.875,0) rectangle (axis cs:1.125,2);
2124
\draw[fill=green!50.0!black,draw opacity=0] (axis cs:1.875,0) rectangle (axis cs:2.125,4);
22-
\addlegendimage{ybar,ybar legend,fill=red,draw opacity=0};
2325
\draw[fill=red,draw opacity=0] (axis cs:0.125,0) rectangle (axis cs:0.375,5);
26+
\addlegendimage{ybar,ybar legend,fill=red,draw opacity=0};
27+
\addlegendentry{Data 3}
28+
2429
\draw[fill=red,draw opacity=0] (axis cs:1.125,0) rectangle (axis cs:1.375,3);
2530
\draw[fill=red,draw opacity=0] (axis cs:2.125,0) rectangle (axis cs:2.375,1);
2631
\end{axis}

0 commit comments

Comments
 (0)