Skip to content

Commit 29a7177

Browse files
committed
remove extra spaces, better title handling
1 parent 76ef4b9 commit 29a7177

File tree

11 files changed

+57
-45
lines changed

11 files changed

+57
-45
lines changed

matplotlib2tikz/axes.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ def __init__(self, data, obj):
3636

3737
# get plot title
3838
title = obj.get_title()
39+
data["current axis title"] = title
3940
if title:
4041
self.axis_options.append("title={{{}}}".format(title))
4142

matplotlib2tikz/save.py

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
from . import quadmesh as qmsh
1919
from . import path
2020
from . import patch
21-
from . import text as mytext
21+
from . import text
2222

2323
from .__about__ import __version__
2424

@@ -223,7 +223,6 @@ def get_tikz_code(
223223
\\usepackage[utf8]{{inputenc}}
224224
\\usepackage{{pgfplots}}
225225
\\usepgfplotslibrary{{groupplots}}
226-
\\usetikzlibrary{{shapes.arrows}}
227226
\\pgfplotsset{{compat=newest}}
228227
\\begin{{document}}
229228
{}
@@ -277,7 +276,7 @@ def _print_pgfplot_libs_message(data):
277276
pgfplotslibs = ",".join(list(data["pgfplots libs"]))
278277
tikzlibs = ",".join(list(data["tikz libs"]))
279278

280-
print("=========================================================")
279+
print(70 * "=")
281280
print("Please add the following lines to your LaTeX preamble:\n")
282281
print("\\usepackage[utf8]{inputenc}")
283282
print("\\usepackage{fontspec} % This line only for XeLaTeX and LuaLaTeX")
@@ -286,7 +285,7 @@ def _print_pgfplot_libs_message(data):
286285
print("\\usetikzlibrary{" + tikzlibs + "}")
287286
if pgfplotslibs:
288287
print("\\usepgfplotslibrary{" + pgfplotslibs + "}")
289-
print("=========================================================")
288+
print(70 * "=")
290289
return
291290

292291

@@ -360,9 +359,6 @@ def _recurse(data, obj):
360359
elif isinstance(child, mpl.image.AxesImage):
361360
data, cont = img.draw_image(data, child)
362361
content.extend(cont, child.get_zorder())
363-
# # Really necessary?
364-
# data, children_content = _recurse(data, child)
365-
# content.extend(children_content)
366362
elif isinstance(child, mpl.patches.Patch):
367363
data, cont = patch.draw_patch(data, child)
368364
content.extend(cont, child.get_zorder())
@@ -384,19 +380,15 @@ def _recurse(data, obj):
384380
data = legend.draw_legend(data, child)
385381
if data["legend colors"]:
386382
content.extend(data["legend colors"], 0)
387-
elif isinstance(
388-
child, (mpl.axis.XAxis, mpl.axis.YAxis, mpl.spines.Spine, mpl.text.Text)
389-
):
383+
elif isinstance(child, (mpl.text.Text, mpl.text.Annotation)):
384+
data, cont = text.draw_text(data, child)
385+
content.extend(cont, child.get_zorder())
386+
elif isinstance(child, (mpl.axis.XAxis, mpl.axis.YAxis, mpl.spines.Spine)):
390387
pass
391388
else:
392389
warnings.warn(
393390
"matplotlib2tikz: Don't know how to handle object {}.".format(
394391
type(child)
395392
)
396393
)
397-
# XXX: This is ugly
398-
if isinstance(obj, (mpl.axes.Subplot, mpl.figure.Figure)):
399-
for text in obj.texts:
400-
data, cont = mytext.draw_text(data, text)
401-
content.extend(cont, text.get_zorder())
402394
return data, content.flatten()

matplotlib2tikz/text.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,12 @@ def draw_text(data, obj):
2121
# -------1--------2---3--4--
2222
pos = obj.get_position()
2323
text = obj.get_text()
24+
25+
if text in ["", data["current axis title"]]:
26+
# Text nodes which are direct children of Axes are typically titles. They are
27+
# already captured by the `title` property of pgfplots axes, so skip them here.
28+
return data, content
29+
2430
size = obj.get_size()
2531
bbox = obj.get_bbox_patch()
2632
converter = mpl.colors.ColorConverter()
@@ -109,8 +115,8 @@ def draw_text(data, obj):
109115
text = text.replace("\n ", "\\\\")
110116

111117
content.append(
112-
"\\node at {}[\n {}\n]{{{} {}}};\n".format(
113-
tikz_pos, ",\n ".join(properties), " ".join(style), text
118+
"\\node at {}[\n {}\n]{{{}}};\n".format(
119+
tikz_pos, ",\n ".join(properties), " ".join(style + [text])
114120
)
115121
)
116122
return data, content

test/helpers.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ def compare_mpl_latex(plot):
8989

9090
# Convert PDF to PNG.
9191
subprocess.check_output(
92-
["pdftoppm", "-png", pdf_file, "test"], stderr=subprocess.STDOUT
92+
["pdftoppm", "-r", "1000", "-png", pdf_file, "test"], stderr=subprocess.STDOUT
9393
)
9494
png_path = os.path.join(pdf_dirname, "test-1.png")
9595

test/test_annotate.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,3 +34,9 @@ def plot():
3434
def test():
3535
assert_equality(plot, "test_annotate_reference.tex")
3636
return
37+
38+
39+
if __name__ == "__main__":
40+
import helpers
41+
# helpers.compare_mpl_latex(plot)
42+
helpers.print_tree(plot())

test/test_annotate_reference.tex

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,13 +42,13 @@
4242
anchor=base west,
4343
text=black,
4444
rotate=0.0
45-
]{ text};
45+
]{text};
4646
\node at (axis cs:-50,30)[
4747
scale=0.5,
4848
anchor=base west,
4949
text=black,
5050
rotate=0.0
51-
]{ arrowstyle};
51+
]{arrowstyle};
5252
\end{axis}
5353

5454
\end{tikzpicture}

test/test_arrows_reference.tex

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
anchor= east,
3535
text=black,
3636
rotate=0.0
37-
]{ -};
37+
]{-};
3838
\draw[->,black] (axis cs:2,5.8) -- (axis cs:3.2,5.8);
3939
\node at (axis cs:2,5.8)[
4040
scale=0.7,
@@ -45,7 +45,7 @@
4545
anchor= east,
4646
text=black,
4747
rotate=0.0
48-
]{ -$>$};
48+
]{-$>$};
4949
\draw[-|,black] (axis cs:2,4.8) -- (axis cs:3.2,4.8);
5050
\node at (axis cs:2,4.8)[
5151
scale=0.7,
@@ -56,7 +56,7 @@
5656
anchor= east,
5757
text=black,
5858
rotate=0.0
59-
]{ -[};
59+
]{-[};
6060
\draw[-latex,black] (axis cs:2,3.8) -- (axis cs:3.2,3.8);
6161
\node at (axis cs:2,3.8)[
6262
scale=0.7,
@@ -67,7 +67,7 @@
6767
anchor= east,
6868
text=black,
6969
rotate=0.0
70-
]{ -$|$$>$};
70+
]{-$|$$>$};
7171
\draw[<-,black] (axis cs:2,2.8) -- (axis cs:3.2,2.8);
7272
\node at (axis cs:2,2.8)[
7373
scale=0.7,
@@ -78,7 +78,7 @@
7878
anchor= east,
7979
text=black,
8080
rotate=0.0
81-
]{ $<$-};
81+
]{$<$-};
8282
\draw[<->,black] (axis cs:2,1.8) -- (axis cs:3.2,1.8);
8383
\node at (axis cs:2,1.8)[
8484
scale=0.7,
@@ -89,7 +89,7 @@
8989
anchor= east,
9090
text=black,
9191
rotate=0.0
92-
]{ $<$-$>$};
92+
]{$<$-$>$};
9393
\draw[latex-,black] (axis cs:2,0.8) -- (axis cs:3.2,0.8);
9494
\node at (axis cs:2,0.8)[
9595
scale=0.7,
@@ -100,7 +100,7 @@
100100
anchor= east,
101101
text=black,
102102
rotate=0.0
103-
]{ $<$$|$-};
103+
]{$<$$|$-};
104104
\draw[latex-latex,black] (axis cs:6,6.8) -- (axis cs:7.2,6.8);
105105
\node at (axis cs:6,6.8)[
106106
scale=0.7,
@@ -111,7 +111,7 @@
111111
anchor= east,
112112
text=black,
113113
rotate=0.0
114-
]{ $<$$|$-$|$$>$};
114+
]{$<$$|$-$|$$>$};
115115
\draw[|-,black] (axis cs:6,5.8) -- (axis cs:7.2,5.8);
116116
\node at (axis cs:6,5.8)[
117117
scale=0.7,
@@ -122,7 +122,7 @@
122122
anchor= east,
123123
text=black,
124124
rotate=0.0
125-
]{ ]-};
125+
]{]-};
126126
\draw[|-|,black] (axis cs:6,4.8) -- (axis cs:7.2,4.8);
127127
\node at (axis cs:6,4.8)[
128128
scale=0.7,
@@ -133,7 +133,7 @@
133133
anchor= east,
134134
text=black,
135135
rotate=0.0
136-
]{ ]-[};
136+
]{]-[};
137137
\draw[-latex,very thick,black] (axis cs:6,3.8) -- (axis cs:7.2,3.8);
138138
\node at (axis cs:6,3.8)[
139139
scale=0.7,
@@ -144,7 +144,7 @@
144144
anchor= east,
145145
text=black,
146146
rotate=0.0
147-
]{ fancy};
147+
]{fancy};
148148
\draw[-latex,very thick,black] (axis cs:6,2.8) -- (axis cs:7.2,2.8);
149149
\node at (axis cs:6,2.8)[
150150
scale=0.7,
@@ -155,7 +155,7 @@
155155
anchor= east,
156156
text=black,
157157
rotate=0.0
158-
]{ simple};
158+
]{simple};
159159
\draw[-latex,very thick,black] (axis cs:6,1.8) -- (axis cs:7.2,1.8);
160160
\node at (axis cs:6,1.8)[
161161
scale=0.7,
@@ -166,7 +166,7 @@
166166
anchor= east,
167167
text=black,
168168
rotate=0.0
169-
]{ wedge};
169+
]{wedge};
170170
\draw[|-|,black] (axis cs:6,0.8) -- (axis cs:7.2,0.8);
171171
\node at (axis cs:6,0.8)[
172172
scale=0.7,
@@ -177,7 +177,7 @@
177177
anchor= east,
178178
text=black,
179179
rotate=0.0
180-
]{ $|$-$|$};
180+
]{$|$-$|$};
181181
\end{axis}
182182

183183
\end{tikzpicture}

test/test_basic_sin.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,9 @@ def plot():
2626
def test():
2727
assert_equality(plot, "test_basic_sin_reference.tex")
2828
return
29+
30+
31+
if __name__ == "__main__":
32+
import helpers
33+
# helpers.compare_mpl_latex(plot)
34+
helpers.print_tree(plot())

test/test_fancybox.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ def box1(ax):
3939

4040
ax.add_patch(p_fancy)
4141

42-
ax.text(0.1, 0.8, " boxstyle='round, pad=0.1'", size=10, transform=ax.transAxes)
42+
ax.text(0.1, 0.8, "boxstyle='round, pad=0.1'", size=10, transform=ax.transAxes)
4343

4444
# # draws control points for the fancy box.
4545
# l = p_fancy.get_path().vertices
@@ -204,5 +204,6 @@ def test():
204204

205205

206206
if __name__ == "__main__":
207-
from helpers import compare_mpl_latex
208-
compare_mpl_latex(plot)
207+
import helpers
208+
# helpers.compare_mpl_latex(plot)
209+
helpers.print_tree(plot())

test/test_fancybox_reference.tex

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
anchor=base west,
3030
text=black,
3131
rotate=0.0
32-
]{ boxstyle='round, pad=0.1'};
32+
]{boxstyle='round, pad=0.1'};
3333
\path [draw=black, fill opacity=0] (axis cs:0.3,0.4)
3434
--(axis cs:0.7,0.4)
3535
--(axis cs:0.7,0.6)
@@ -62,7 +62,7 @@
6262
text=black,
6363
rotate=0.0,
6464
align=left
65-
]{ boxstyle='round,pad=0.1\\rounding\_size=0.2'};
65+
]{boxstyle='round,pad=0.1\\rounding\_size=0.2'};
6666
\path [draw=black, fill opacity=0] (axis cs:0.3,0.4)
6767
--(axis cs:0.7,0.4)
6868
--(axis cs:0.7,0.6)
@@ -95,7 +95,7 @@
9595
text=black,
9696
rotate=0.0,
9797
align=left
98-
]{ boxstyle='round,pad=0.1'\\mutation\_scale=2};
98+
]{boxstyle='round,pad=0.1'\\mutation\_scale=2};
9999
\path [draw=black, fill opacity=0] (axis cs:0.3,0.4)
100100
--(axis cs:0.7,0.4)
101101
--(axis cs:0.7,0.6)
@@ -128,7 +128,7 @@
128128
text=black,
129129
rotate=0.0,
130130
align=left
131-
]{ boxstyle='round, pad=0.3'\\mutation\_aspect=.5};
131+
]{boxstyle='round, pad=0.3'\\mutation\_aspect=.5};
132132
\path [draw=green!50.0!black, fill opacity=0] (axis cs:0.3,0.2)
133133
--(axis cs:0.7,0.2)
134134
.. controls (axis cs:0.833333333333333,0.2) and (axis cs:0.9,0.266666666666667) .. (axis cs:0.9,0.4)

0 commit comments

Comments
 (0)