Skip to content

Commit e1c841f

Browse files
authored
Merge pull request #289 from nschloe/transform
reintroduce data transform
2 parents afa4d36 + 82330ad commit e1c841f

File tree

6 files changed

+107
-36
lines changed

6 files changed

+107
-36
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
[![PyPi Version](https://img.shields.io/pypi/v/matplotlib2tikz.svg)](https://pypi.org/project/matplotlib2tikz)
99
[![DOI](https://zenodo.org/badge/DOI/10.5281/zenodo.1173089.svg)](https://doi.org/10.5281/zenodo.1173089)
1010
[![GitHub stars](https://img.shields.io/github/stars/nschloe/matplotlib2tikz.svg?logo=github&label=Stars&logoColor=white)](https://github.com/nschloe/matplotlib2tikz)
11+
[![PyPi downloads](https://img.shields.io/pypi/dd/matplotlib2tikz.svg)](https://pypistats.org/packages/matplotlib2tikz)
1112

1213
This is matplotlib2tikz, a Python tool for converting matplotlib figures into
1314
[PGFPlots](https://www.ctan.org/pkg/pgfplots) ([PGF/TikZ](https://www.ctan.org/pkg/pgf))

matplotlib2tikz/line2d.py

Lines changed: 4 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
from . import path as mypath
1111
from . import files
1212

13-
from .util import get_legend_text, has_legend
13+
from .util import get_legend_text, has_legend, transform_to_data_coordinates
1414

1515

1616
def draw_line2d(data, obj):
@@ -180,36 +180,6 @@ def _mpl_marker2pgfp_marker(data, mpl_marker, marker_face_color):
180180
return data, None, None
181181

182182

183-
# def _transform_to_data_coordinates(obj, xdata, ydata):
184-
# '''The coordinates might not be in data coordinates, but could be partly
185-
# in axes coordinates. For example, the matplotlib command
186-
# axes.axvline(2)
187-
# will have the y coordinates set to 0 and 1, not to the limits. Therefore,
188-
# a two-stage transform has to be applied:
189-
# 1. first transforming to display coordinates, then
190-
# 2. from display to data.
191-
# In case of problems (non-invertible, or whatever), print a warning and
192-
# continue anyways.
193-
# '''
194-
# try:
195-
# import matplotlib.transforms
196-
# points = numpy.array(zip(xdata, ydata))
197-
# transform = matplotlib.transforms.composite_transform_factory(
198-
# obj.get_transform(),
199-
# obj.axes.transData.inverted()
200-
# )
201-
# points_data = transform.transform(points)
202-
# xdata, ydata = zip(*points_data)
203-
# except Exception as e:
204-
# print(xdata, ydata)
205-
# print(('Problem during transformation:\n' +
206-
# ' %s\n' +
207-
# 'Continuing with original data.')
208-
# % e
209-
# )
210-
# return (xdata, ydata)
211-
212-
213183
def _marker(
214184
obj,
215185
data,
@@ -272,11 +242,9 @@ def _marker(
272242

273243

274244
def _table(obj, data):
275-
# TODO nschloe, Oct 2, 2015:
276-
# The transform call yields warnings and it is unclear why. Perhaps the input data
277-
# is not suitable? Anyhow, this should not happen. Comment out for now.
278-
# xdata, ydata = _transform_to_data_coordinates(obj, *obj.get_data())
279245
xdata, ydata = obj.get_data()
246+
if not isinstance(xdata[0], datetime.datetime):
247+
xdata, ydata = transform_to_data_coordinates(obj, xdata, ydata)
280248

281249
# matplotlib allows plotting of data containing `astropy.units`, but they will break
282250
# the formatted string here. Try to strip the units from the data.
@@ -337,7 +305,7 @@ def _table(obj, data):
337305
else:
338306
plot_table.append((xformat + col_sep + ff + "\n").format(x, y))
339307
else:
340-
for (x, y) in zip(xdata, ydata):
308+
for x, y in zip(xdata, ydata):
341309
plot_table.append((xformat + col_sep + ff + "\n").format(x, y))
342310

343311
if data["externalize tables"]:

matplotlib2tikz/text.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@ def draw_text(data, obj):
2020
# 4: the text
2121
# -------1--------2---3--4--
2222
pos = obj.get_position()
23+
24+
# from .util import transform_to_data_coordinates
25+
# pos = transform_to_data_coordinates(obj, *pos)
26+
2327
text = obj.get_text()
2428

2529
if text in ["", data["current axis title"]]:

matplotlib2tikz/util.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# -*- coding: utf-8 -*-
22
#
3+
import matplotlib.transforms
4+
import numpy
35

46

57
def has_legend(axes):
@@ -22,3 +24,21 @@ def get_legend_text(obj):
2224
return d[label]
2325

2426
return None
27+
28+
29+
def transform_to_data_coordinates(obj, xdata, ydata):
30+
"""The coordinates might not be in data coordinates, but could be sometimes in axes
31+
coordinates. For example, the matplotlib command
32+
axes.axvline(2)
33+
will have the y coordinates set to 0 and 1, not to the limits. Therefore, a
34+
two-stage transform has to be applied:
35+
1. first transforming to display coordinates, then
36+
2. from display to data.
37+
"""
38+
if obj.axes is not None and obj.get_transform() != obj.axes.transData:
39+
points = numpy.array([xdata, ydata]).T
40+
transform = matplotlib.transforms.composite_transform_factory(
41+
obj.get_transform(), obj.axes.transData.inverted()
42+
)
43+
return transform.transform(points).T
44+
return xdata, ydata

test/test_axvline.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# -*- coding: utf-8 -*-
2+
#
3+
import matplotlib.pyplot as plt
4+
import numpy as np
5+
6+
from helpers import assert_equality
7+
8+
9+
def plot():
10+
fig = plt.figure()
11+
np.random.seed(123)
12+
s = np.random.normal(0, 1, 10)
13+
plt.gca().set_ylim(-1.0, +1.0)
14+
plt.hist(s, 30)
15+
plt.axvline(1.96)
16+
return fig
17+
18+
19+
def test():
20+
assert_equality(plot, __file__[:-3] + "_reference.tex")
21+
return
22+
23+
24+
if __name__ == "__main__":
25+
import helpers
26+
27+
helpers.compare_mpl_latex(plot)
28+
# helpers.print_tree(plot())

test/test_axvline_reference.tex

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
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=-2.64601320556273, xmax=2.17933396216965,
10+
y grid style={white!69.01960784313725!black},
11+
ymin=-1, ymax=1
12+
]
13+
\draw[fill=color0,draw opacity=0] (axis cs:-2.42667924339307,0) rectangle (axis cs:-2.29074205071007,1);
14+
\draw[fill=color0,draw opacity=0] (axis cs:-2.29074205071007,0) rectangle (axis cs:-2.15480485802706,0);
15+
\draw[fill=color0,draw opacity=0] (axis cs:-2.15480485802706,0) rectangle (axis cs:-2.01886766534405,0);
16+
\draw[fill=color0,draw opacity=0] (axis cs:-2.01886766534405,0) rectangle (axis cs:-1.88293047266104,0);
17+
\draw[fill=color0,draw opacity=0] (axis cs:-1.88293047266104,0) rectangle (axis cs:-1.74699327997804,0);
18+
\draw[fill=color0,draw opacity=0] (axis cs:-1.74699327997804,0) rectangle (axis cs:-1.61105608729503,0);
19+
\draw[fill=color0,draw opacity=0] (axis cs:-1.61105608729503,0) rectangle (axis cs:-1.47511889461202,1);
20+
\draw[fill=color0,draw opacity=0] (axis cs:-1.47511889461202,0) rectangle (axis cs:-1.33918170192901,0);
21+
\draw[fill=color0,draw opacity=0] (axis cs:-1.33918170192901,0) rectangle (axis cs:-1.20324450924601,0);
22+
\draw[fill=color0,draw opacity=0] (axis cs:-1.20324450924601,0) rectangle (axis cs:-1.067307316563,1);
23+
\draw[fill=color0,draw opacity=0] (axis cs:-1.067307316563,0) rectangle (axis cs:-0.931370123879991,0);
24+
\draw[fill=color0,draw opacity=0] (axis cs:-0.931370123879991,0) rectangle (axis cs:-0.795432931196984,1);
25+
\draw[fill=color0,draw opacity=0] (axis cs:-0.795432931196984,0) rectangle (axis cs:-0.659495738513976,0);
26+
\draw[fill=color0,draw opacity=0] (axis cs:-0.659495738513976,0) rectangle (axis cs:-0.523558545830969,1);
27+
\draw[fill=color0,draw opacity=0] (axis cs:-0.523558545830969,0) rectangle (axis cs:-0.387621353147961,1);
28+
\draw[fill=color0,draw opacity=0] (axis cs:-0.387621353147961,0) rectangle (axis cs:-0.251684160464954,0);
29+
\draw[fill=color0,draw opacity=0] (axis cs:-0.251684160464954,0) rectangle (axis cs:-0.115746967781946,0);
30+
\draw[fill=color0,draw opacity=0] (axis cs:-0.115746967781946,0) rectangle (axis cs:0.0201902249010613,0);
31+
\draw[fill=color0,draw opacity=0] (axis cs:0.0201902249010613,0) rectangle (axis cs:0.156127417584069,0);
32+
\draw[fill=color0,draw opacity=0] (axis cs:0.156127417584069,0) rectangle (axis cs:0.292064610267076,1);
33+
\draw[fill=color0,draw opacity=0] (axis cs:0.292064610267076,0) rectangle (axis cs:0.428001802950084,0);
34+
\draw[fill=color0,draw opacity=0] (axis cs:0.428001802950084,0) rectangle (axis cs:0.563938995633091,0);
35+
\draw[fill=color0,draw opacity=0] (axis cs:0.563938995633091,0) rectangle (axis cs:0.699876188316099,0);
36+
\draw[fill=color0,draw opacity=0] (axis cs:0.699876188316099,0) rectangle (axis cs:0.835813380999106,0);
37+
\draw[fill=color0,draw opacity=0] (axis cs:0.835813380999106,0) rectangle (axis cs:0.971750573682114,0);
38+
\draw[fill=color0,draw opacity=0] (axis cs:0.971750573682114,0) rectangle (axis cs:1.10768776636512,1);
39+
\draw[fill=color0,draw opacity=0] (axis cs:1.10768776636512,0) rectangle (axis cs:1.24362495904813,0);
40+
\draw[fill=color0,draw opacity=0] (axis cs:1.24362495904813,0) rectangle (axis cs:1.37956215173114,1);
41+
\draw[fill=color0,draw opacity=0] (axis cs:1.37956215173114,0) rectangle (axis cs:1.51549934441414,0);
42+
\draw[fill=color0,draw opacity=0] (axis cs:1.51549934441414,0) rectangle (axis cs:1.65143653709715,1);
43+
\addplot [semithick, color0]
44+
table {%
45+
1.96 -1
46+
1.96 1
47+
};
48+
\end{axis}
49+
50+
\end{tikzpicture}

0 commit comments

Comments
 (0)