Skip to content

Commit 647da40

Browse files
authored
Merge pull request #360 from harenbrs/master
Support paths with datetime x-axis (#328)
2 parents d7cfc31 + 8322565 commit 647da40

File tree

3 files changed

+93
-8
lines changed

3 files changed

+93
-8
lines changed

test/test_datetime_paths.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import datetime as date
2+
3+
import matplotlib.pyplot as plt
4+
from matplotlib import dates
5+
6+
from helpers import assert_equality
7+
8+
9+
def plot():
10+
fig = plt.figure()
11+
12+
times = [date.datetime(2020, 1, 1, 12, 00), date.datetime(2020, 1, 2, 12, 00)]
13+
line = [2, 2]
14+
upper = [3, 4]
15+
lower = [1, 0]
16+
17+
plt.plot(times, line)
18+
plt.fill_between(times, lower, upper)
19+
ax = plt.gca()
20+
ax.fmt_xdata = dates.DateFormatter("%d %b %Y %H:%M:%S")
21+
22+
return fig
23+
24+
25+
def test():
26+
assert_equality(plot, __file__[:-3] + "_reference.tex")
27+
return
28+
29+
30+
if __name__ == "__main__":
31+
import helpers
32+
33+
helpers.compare_mpl_latex(plot)
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
\begin{tikzpicture}
2+
3+
\definecolor{color0}{rgb}{0.12156862745098,0.466666666666667,0.705882352941177}
4+
5+
\begin{axis}[
6+
date coordinates in=x,
7+
tick align=outside,
8+
tick pos=left,
9+
x grid style={white!69.01960784313725!black},
10+
xmin=2020-01-01 10:48, xmax=2020-01-02 13:12,
11+
xtick style={color=black},
12+
y grid style={white!69.01960784313725!black},
13+
ymin=-0.2, ymax=4.2,
14+
ytick style={color=black}
15+
]
16+
\path [fill=color0]
17+
(axis cs:2020-01-01 12:00:00+00:00,3)
18+
--(axis cs:2020-01-01 12:00:00+00:00,1)
19+
--(axis cs:2020-01-02 12:00:00+00:00,0)
20+
--(axis cs:2020-01-02 12:00:00+00:00,4)
21+
--(axis cs:2020-01-02 12:00:00+00:00,4)
22+
--(axis cs:2020-01-01 12:00:00+00:00,3)
23+
--cycle;
24+
25+
\addplot [semithick, color0]
26+
table [header=false,col sep=comma] {%
27+
2020-01-01 12:00,2
28+
2020-01-02 12:00,2
29+
};
30+
\end{axis}
31+
32+
\end{tikzpicture}

tikzplotlib/_path.py

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import matplotlib as mpl
22
import numpy
33
from matplotlib.markers import MarkerStyle
4+
from matplotlib.dates import num2date, DateConverter
45

56
from . import _color
67
from ._axes import _mpl_cmap2pgf_cmap
@@ -22,8 +23,10 @@ def draw_path(data, path, draw_options=None, simplify=None):
2223
):
2324
return data, "", None, False
2425

26+
x_is_date = isinstance(data["current mpl axes obj"].xaxis.converter, DateConverter)
2527
nodes = []
2628
ff = data["float format"]
29+
xformat = "{}" if x_is_date else ff
2730
prev = None
2831
for vert, code in path.iter_segments(simplify=simplify):
2932
# nschloe, Oct 2, 2015:
@@ -38,9 +41,13 @@ def draw_path(data, path, draw_options=None, simplify=None):
3841
# if code == mpl.path.Path.STOP: pass
3942
is_area = False
4043
if code == mpl.path.Path.MOVETO:
41-
nodes.append(("(axis cs:" + ff + "," + ff + ")").format(*vert))
44+
if x_is_date:
45+
vert = [num2date(vert[0]), vert[1]]
46+
nodes.append(("(axis cs:" + xformat + "," + ff + ")").format(*vert))
4247
elif code == mpl.path.Path.LINETO:
43-
nodes.append(("--(axis cs:" + ff + "," + ff + ")").format(*vert))
48+
if x_is_date:
49+
vert = [num2date(vert[0]), vert[1]]
50+
nodes.append(("--(axis cs:" + xformat + "," + ff + ")").format(*vert))
4451
elif code == mpl.path.Path.CURVE3:
4552
# Quadratic Bezier curves aren't natively supported in TikZ, but
4653
# can be emulated as cubic Beziers.
@@ -63,41 +70,54 @@ def draw_path(data, path, draw_options=None, simplify=None):
6370
Q1 = 1.0 / 3.0 * prev + 2.0 / 3.0 * vert[0:2]
6471
Q2 = 2.0 / 3.0 * vert[0:2] + 1.0 / 3.0 * vert[2:4]
6572
Q3 = vert[2:4]
73+
if x_is_date:
74+
Q1 = [num2date(Q1[0]), Q1[1]]
75+
Q2 = [num2date(Q2[0]), Q2[1]]
76+
Q3 = [num2date(Q3[0]), Q3[1]]
6677
nodes.append(
6778
(
6879
".. controls (axis cs:"
69-
+ ff
80+
+ xformat
7081
+ ","
7182
+ ff
7283
+ ") "
7384
+ "and (axis cs:"
74-
+ ff
85+
+ xformat
7586
+ ","
7687
+ ff
7788
+ ") "
7889
+ ".. (axis cs:"
79-
+ ff
90+
+ xformat
8091
+ ","
8192
+ ff
8293
+ ")"
8394
).format(Q1[0], Q1[1], Q2[0], Q2[1], Q3[0], Q3[1])
8495
)
8596
elif code == mpl.path.Path.CURVE4:
8697
# Cubic Bezier curves.
98+
if x_is_date:
99+
vert = [
100+
num2date(vert[0]),
101+
vert[1],
102+
num2date(vert[2]),
103+
vert[3],
104+
num2date(vert[4]),
105+
vert[5],
106+
]
87107
nodes.append(
88108
(
89109
".. controls (axis cs:"
90-
+ ff
110+
+ xformat
91111
+ ","
92112
+ ff
93113
+ ") "
94114
+ "and (axis cs:"
95-
+ ff
115+
+ xformat
96116
+ ","
97117
+ ff
98118
+ ") "
99119
+ ".. (axis cs:"
100-
+ ff
120+
+ xformat
101121
+ ","
102122
+ ff
103123
+ ")"

0 commit comments

Comments
 (0)