Skip to content

Commit 32cf365

Browse files
committed
support for custom dash pattern
1 parent 0ea7930 commit 32cf365

File tree

3 files changed

+106
-1
lines changed

3 files changed

+106
-1
lines changed

matplotlib2tikz/path.py

+23-1
Original file line numberDiff line numberDiff line change
@@ -293,7 +293,7 @@ def mpl_linewidth2pgfp_linewidth(data, line_width):
293293
return out
294294

295295

296-
def mpl_linestyle2pgfplots_linestyle(line_style):
296+
def mpl_linestyle2pgfplots_linestyle(line_style, line=None):
297297
"""Translates a line style of matplotlib to the corresponding style
298298
in PGFPlots.
299299
"""
@@ -314,6 +314,28 @@ def mpl_linestyle2pgfplots_linestyle(line_style):
314314

315315
assert len(line_style[1]) == 4
316316
return "dash pattern=on {}pt off {}pt on {}pt off {}pt".format(*line_style[1])
317+
318+
if isinstance(line, mpl.lines.Line2D) and line.is_dashed():
319+
# see matplotlib.lines.Line2D.set_dashes
320+
321+
# get defaults
322+
default_dashOffset, default_dashSeq = mpl.lines._get_dash_pattern(line_style)
323+
324+
# get dash format of line under test
325+
dashSeq = line._us_dashSeq
326+
dashOffset = line._us_dashOffset
327+
328+
lst = list()
329+
if dashSeq != default_dashSeq:
330+
# generate own dash sequence
331+
format_string = " ".join(len(dashSeq) // 2 * ["on {}pt off {}pt"])
332+
lst.append("dash pattern=" + format_string.format(*dashSeq))
333+
334+
if dashOffset != default_dashOffset:
335+
lst.append("dash phase={}pt".format(dashOffset))
336+
337+
if len(lst) > 0:
338+
return ", ".join(lst)
317339

318340
return {
319341
"": None,

test/test_line_dashes.py

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# -*- coding: utf-8 -*-
2+
#
3+
import matplotlib.pyplot as plt
4+
5+
from helpers import assert_equality
6+
7+
8+
def plot():
9+
fig = plt.figure()
10+
linestyles = ["-", "--", "-.", ":", (1, (10, 1)), (5, (10, 1)), (0, (1, 2, 3, 4))]
11+
for idx, ls in enumerate(linestyles):
12+
plt.plot([idx, idx + 1], linestyle=ls)
13+
return fig
14+
15+
16+
def test():
17+
assert_equality(plot, __file__[:-3] + "_reference.tex")
18+
return
19+
20+
21+
if __name__ == "__main__":
22+
import helpers
23+
24+
helpers.compare_mpl_latex(plot)
25+
# helpers.print_tree(plot())

test/test_line_dashes_reference.tex

+58
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
\begin{tikzpicture}
2+
3+
\definecolor{color0}{rgb}{0.12156862745098,0.466666666666667,0.705882352941177}
4+
\definecolor{color1}{rgb}{1,0.498039215686275,0.0549019607843137}
5+
\definecolor{color2}{rgb}{0.172549019607843,0.627450980392157,0.172549019607843}
6+
\definecolor{color3}{rgb}{0.83921568627451,0.152941176470588,0.156862745098039}
7+
\definecolor{color4}{rgb}{0.580392156862745,0.403921568627451,0.741176470588235}
8+
\definecolor{color5}{rgb}{0.549019607843137,0.337254901960784,0.294117647058824}
9+
\definecolor{color6}{rgb}{0.890196078431372,0.466666666666667,0.76078431372549}
10+
11+
\begin{axis}[
12+
tick align=outside,
13+
tick pos=left,
14+
x grid style={white!69.01960784313725!black},
15+
xmin=-0.05, xmax=1.05,
16+
xtick style={color=black},
17+
y grid style={white!69.01960784313725!black},
18+
ymin=-0.35, ymax=7.35,
19+
ytick style={color=black}
20+
]
21+
\addplot [semithick, color0]
22+
table {%
23+
0 0
24+
1 1
25+
};
26+
\addplot [semithick, color1, dashed]
27+
table {%
28+
0 1
29+
1 2
30+
};
31+
\addplot [semithick, color2, dash pattern=on 1pt off 3pt on 3pt off 3pt]
32+
table {%
33+
0 2
34+
1 3
35+
};
36+
\addplot [semithick, color3, dotted]
37+
table {%
38+
0 3
39+
1 4
40+
};
41+
\addplot [semithick, color4, dash pattern=on 10pt off 1pt, dash phase=1pt]
42+
table {%
43+
0 4
44+
1 5
45+
};
46+
\addplot [semithick, color5, dash pattern=on 10pt off 1pt, dash phase=5pt]
47+
table {%
48+
0 5
49+
1 6
50+
};
51+
\addplot [semithick, color6, dash pattern=on 1pt off 2pt on 3pt off 4pt]
52+
table {%
53+
0 6
54+
1 7
55+
};
56+
\end{axis}
57+
58+
\end{tikzpicture}

0 commit comments

Comments
 (0)