Skip to content

Commit 5326b2b

Browse files
authored
Merge pull request #308 from edmundus/master
Added functionality for plotting step plots
2 parents a2344c1 + 34a9840 commit 5326b2b

File tree

3 files changed

+111
-0
lines changed

3 files changed

+111
-0
lines changed

test/test_steps.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
from helpers import assert_equality
2+
3+
4+
def plot():
5+
import matplotlib.pyplot as plt
6+
import numpy as np
7+
8+
fig = plt.figure()
9+
x = np.arange(5)
10+
y1 = np.array([1, 2, 1, 4, 2])
11+
y2 = np.array([1, 2, 1, 4, 2])
12+
y3 = np.array([1, 2, 1, 4, 2])
13+
y4 = np.array([1, 2, 1, 4, 2])
14+
y5 = np.array([2, 3, 2, 5, 3])
15+
16+
plt.step(x, y1, "r-")
17+
plt.step(x, y2, "b--", where="pre")
18+
plt.step(x, y3, "g-.", where="post")
19+
plt.step(x, y4, "y:", where="mid")
20+
plt.plot(x, y5, "c.")
21+
plt.legend(["default", "pre", "post", "mid", "default"])
22+
23+
return fig
24+
25+
26+
def test():
27+
assert_equality(plot, __file__[:-3] + "_reference.tex")
28+
29+
30+
if __name__ == "__main__":
31+
import helpers
32+
33+
helpers.compare_mpl_latex(plot)

test/test_steps_reference.tex

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
\begin{tikzpicture}
2+
3+
\definecolor{color0}{rgb}{0.75,0.75,0}
4+
\definecolor{color1}{rgb}{0,0.75,0.75}
5+
6+
\begin{axis}[
7+
legend cell align={left},
8+
legend style={at={(0.03,0.97)}, anchor=north west, draw=white!80.0!black},
9+
tick align=outside,
10+
tick pos=left,
11+
x grid style={white!69.01960784313725!black},
12+
xmin=-0.2, xmax=4.2,
13+
xtick style={color=black},
14+
y grid style={white!69.01960784313725!black},
15+
ymin=0.8, ymax=5.2,
16+
ytick style={color=black}
17+
]
18+
\addplot [semithick, red, const plot mark right]
19+
table {%
20+
0 1
21+
1 2
22+
2 1
23+
3 4
24+
4 2
25+
};
26+
\addlegendentry{default}
27+
\addplot [semithick, blue, const plot mark right, dashed]
28+
table {%
29+
0 1
30+
1 2
31+
2 1
32+
3 4
33+
4 2
34+
};
35+
\addlegendentry{pre}
36+
\addplot [semithick, green!50.0!black, const plot mark left, dash pattern=on 1pt off 3pt on 3pt off 3pt]
37+
table {%
38+
0 1
39+
1 2
40+
2 1
41+
3 4
42+
4 2
43+
};
44+
\addlegendentry{post}
45+
\addplot [semithick, color0, const plot mark mid, dotted]
46+
table {%
47+
0 1
48+
1 2
49+
2 1
50+
3 4
51+
4 2
52+
};
53+
\addlegendentry{mid}
54+
\addplot [semithick, color1, mark=*, mark size=3, mark options={solid}, only marks]
55+
table {%
56+
0 2
57+
1 3
58+
2 2
59+
3 5
60+
4 3
61+
};
62+
\addlegendentry{default}
63+
\end{axis}
64+
65+
\end{tikzpicture}

tikzplotlib/line2d.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,19 @@ def draw_line2d(data, obj):
3131
data, line_xcolor, _ = mycol.mpl_color2xcolor(data, color)
3232
addplot_options.append(line_xcolor)
3333

34+
# get draw style
35+
drawstyle = obj.get_drawstyle()
36+
if drawstyle in [None, "default"]:
37+
pass
38+
else:
39+
if drawstyle == "steps-mid":
40+
style = "const plot mark mid"
41+
elif drawstyle in ["steps-pre", "steps"]:
42+
style = "const plot mark right"
43+
elif drawstyle == "steps-post":
44+
style = "const plot mark left"
45+
addplot_options.append("{}".format(style))
46+
3447
alpha = obj.get_alpha()
3548
if alpha is not None:
3649
addplot_options.append("opacity={}".format(alpha))

0 commit comments

Comments
 (0)