Skip to content

Commit 41a4d4e

Browse files
authored
Merge pull request #290 from nschloe/pandas-dataframe
Pandas dataframe
2 parents e1c841f + 58902d7 commit 41a4d4e

File tree

5 files changed

+78
-8
lines changed

5 files changed

+78
-8
lines changed

.circleci/config.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ jobs:
2424
- checkout
2525
- run: cd ~/work
2626
- run: excode README.md test/zzz_readme_test.py --filter python,test
27+
- run: pip3 install -r test_requirements.txt
2728
- run: pip3 install .[all]
2829
- run: pip3 check
2930
# The actual test

matplotlib2tikz/line2d.py

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,12 @@
22
#
33
from __future__ import print_function
44

5-
from matplotlib.dates import num2date
65
import datetime
76
import six
87

8+
from matplotlib.dates import num2date
9+
import numpy
10+
911
from . import color as mycol
1012
from . import path as mypath
1113
from . import files
@@ -242,8 +244,23 @@ def _marker(
242244

243245

244246
def _table(obj, data):
245-
xdata, ydata = obj.get_data()
246-
if not isinstance(xdata[0], datetime.datetime):
247+
# get_xydata() always gives float data, no matter what
248+
xdata, ydata = obj.get_xydata().T
249+
250+
# get_{x,y}data gives datetime or string objects if so specified in the plotter
251+
xdata_alt = obj.get_xdata()
252+
253+
ff = data["float format"]
254+
255+
if isinstance(xdata_alt[0], datetime.datetime):
256+
xdata = xdata_alt
257+
elif isinstance(xdata_alt[0], str):
258+
data["current axes"].axis_options += [
259+
"xtick={{{}}}".format(",".join([ff.format(x) for x in xdata])),
260+
"xticklabels={{{}}}".format(",".join(xdata_alt)),
261+
]
262+
xdata, ydata = transform_to_data_coordinates(obj, xdata, ydata)
263+
else:
247264
xdata, ydata = transform_to_data_coordinates(obj, xdata, ydata)
248265

249266
# matplotlib allows plotting of data containing `astropy.units`, but they will break
@@ -258,15 +275,18 @@ def _table(obj, data):
258275
pass
259276

260277
try:
261-
has_mask = ydata.mask.any()
278+
_, ydata_alt = obj.get_data()
279+
ydata_mask = ydata_alt.mask
262280
except AttributeError:
263-
has_mask = False
281+
ydata_mask = []
282+
else:
283+
if isinstance(ydata_mask, numpy.bool_) and not ydata_mask:
284+
ydata_mask = []
264285

265286
axis_options = []
266287

267288
content = []
268289

269-
ff = data["float format"]
270290
if isinstance(xdata[0], datetime.datetime):
271291
xdata = [date.strftime("%Y-%m-%d %H:%M") for date in xdata]
272292
xformat = "{}"
@@ -293,13 +313,13 @@ def _table(obj, data):
293313
content.append("table {%\n")
294314

295315
plot_table = []
296-
if has_mask:
316+
if any(ydata_mask):
297317
# matplotlib jumps at masked images, while PGFPlots by default interpolates.
298318
# Hence, if we have a masked plot, make sure that PGFPlots jumps as well.
299319
if "unbounded coords=jump" not in data["current axes"].axis_options:
300320
data["current axes"].axis_options.append("unbounded coords=jump")
301321

302-
for (x, y, is_masked) in zip(xdata, ydata, ydata.mask):
322+
for (x, y, is_masked) in zip(xdata, ydata, ydata_mask):
303323
if is_masked:
304324
plot_table.append((xformat + col_sep + "nan\n").format(x))
305325
else:

test/test_pandas_dataframe.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# -*- coding: utf-8 -*-
2+
#
3+
import matplotlib.pyplot as plt
4+
import pandas as pd
5+
6+
from helpers import assert_equality
7+
8+
9+
def plot():
10+
fig = plt.figure(1, figsize=(8, 5))
11+
df = pd.DataFrame(index=["one", "two", "three"], data={"data": [1, 2, 3]})
12+
plt.plot(df, "o")
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())
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
\begin{tikzpicture}
2+
3+
\definecolor{color0}{rgb}{0.12156862745098,0.466666666666667,0.705882352941177}
4+
5+
\begin{axis}[
6+
tick align=outside,
7+
tick pos=left,
8+
x grid style={white!69.01960784313725!black},
9+
xmin=-0.1, xmax=2.1,
10+
xtick={0,1,2},
11+
xticklabels={one,two,three},
12+
y grid style={white!69.01960784313725!black},
13+
ymin=0.9, ymax=3.1
14+
]
15+
\addplot [semithick, color0, mark=*, mark size=3, mark options={solid}, only marks]
16+
table {%
17+
0 1
18+
1 2
19+
2 3
20+
};
21+
\end{axis}
22+
23+
\end{tikzpicture}

test_requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
pandas

0 commit comments

Comments
 (0)