Skip to content

Commit 82330ad

Browse files
committed
move transform function into utils
1 parent 3045f10 commit 82330ad

File tree

3 files changed

+26
-25
lines changed

3 files changed

+26
-25
lines changed

matplotlib2tikz/line2d.py

Lines changed: 2 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,15 @@
22
#
33
from __future__ import print_function
44

5-
import matplotlib.transforms
65
from matplotlib.dates import num2date
7-
import numpy
86
import datetime
97
import six
108

119
from . import color as mycol
1210
from . import path as mypath
1311
from . import files
1412

15-
from .util import get_legend_text, has_legend
13+
from .util import get_legend_text, has_legend, transform_to_data_coordinates
1614

1715

1816
def draw_line2d(data, obj):
@@ -182,24 +180,6 @@ def _mpl_marker2pgfp_marker(data, mpl_marker, marker_face_color):
182180
return data, None, None
183181

184182

185-
def _transform_to_data_coordinates(obj, xdata, ydata):
186-
"""The coordinates might not be in data coordinates, but could be sometimes in axes
187-
coordinates. For example, the matplotlib command
188-
axes.axvline(2)
189-
will have the y coordinates set to 0 and 1, not to the limits. Therefore, a
190-
two-stage transform has to be applied:
191-
1. first transforming to display coordinates, then
192-
2. from display to data.
193-
"""
194-
if obj.get_transform() != obj.axes.transData:
195-
points = numpy.array([xdata, ydata]).T
196-
transform = matplotlib.transforms.composite_transform_factory(
197-
obj.get_transform(), obj.axes.transData.inverted()
198-
)
199-
return transform.transform(points).T
200-
return xdata, ydata
201-
202-
203183
def _marker(
204184
obj,
205185
data,
@@ -262,12 +242,9 @@ def _marker(
262242

263243

264244
def _table(obj, data):
265-
# TODO nschloe, Oct 2, 2015:
266-
# The transform call yields warnings and it is unclear why. Perhaps the input data
267-
# is not suitable? Anyhow, this should not happen. Comment out for now.
268245
xdata, ydata = obj.get_data()
269246
if not isinstance(xdata[0], datetime.datetime):
270-
xdata, ydata = _transform_to_data_coordinates(obj, xdata, ydata)
247+
xdata, ydata = transform_to_data_coordinates(obj, xdata, ydata)
271248

272249
# matplotlib allows plotting of data containing `astropy.units`, but they will break
273250
# the formatted string here. Try to strip the units from the data.

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

0 commit comments

Comments
 (0)