Skip to content

Commit 5990477

Browse files
authored
Merge pull request #415 from theRealSuperMario/logscale
Logscale
2 parents adcf150 + 1f0e8f7 commit 5990477

File tree

2 files changed

+59
-8
lines changed

2 files changed

+59
-8
lines changed

test/test_cleanfigure.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,45 @@ def test_plot(self):
3333
assert numLinesRaw - numLinesClean == 18
3434
plt.close("all")
3535

36+
def test_logplot(self):
37+
x = np.logspace(-3, 3, 20)
38+
y = np.logspace(-3, 3, 20)
39+
40+
with plt.rc_context(rc=RC_PARAMS):
41+
fig, ax = plt.subplots(1, 1, figsize=(5, 5))
42+
ax.plot(x, y)
43+
ax.set_xscale("log")
44+
ax.set_yscale("log")
45+
ax.set_ylim([10 ** (-2), 10 ** (2)])
46+
ax.set_xlim([10 ** (-2), 10 ** (2)])
47+
raw = get_tikz_code()
48+
49+
clean_figure(fig)
50+
clean = get_tikz_code()
51+
numLinesRaw = raw.count("\n")
52+
numLinesClean = clean.count("\n")
53+
assert numLinesRaw - numLinesClean == 11
54+
plt.close("all")
55+
56+
def test_semilogplot(self):
57+
x = np.logspace(-3, 3, 20)
58+
y = np.linspace(1, 100, 20)
59+
60+
with plt.rc_context(rc=RC_PARAMS):
61+
fig, ax = plt.subplots(1, 1, figsize=(5, 5))
62+
ax.plot(x, y)
63+
ax.set_xscale("log")
64+
ax.set_xlim([10 ** (-2), 10 ** (2)])
65+
ax.set_ylim([20, 80])
66+
raw = get_tikz_code()
67+
68+
clean_figure(fig)
69+
clean = get_tikz_code()
70+
numLinesRaw = raw.count("\n")
71+
numLinesClean = clean.count("\n")
72+
assert numLinesRaw - numLinesClean == 6
73+
plt.close("all")
74+
3675
def test_step(self):
3776
x = np.linspace(1, 100, 20)
3877
y = np.linspace(1, 100, 20)

tikzplotlib/_cleanfigure.py

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -229,8 +229,8 @@ def _cleanline(fighandle, axhandle, linehandle, target_resolution, scale_precisi
229229
visual_data = _get_visual_data(axhandle, data, is3D)
230230

231231
if not is3D:
232-
data = _move_points_closer(xLim, yLim, data)
233-
visual_data = _get_visual_data(axhandle, data, is3D)
232+
visual_data = _move_points_closer(xLim, yLim, visual_data)
233+
visual_data = _get_visual_data(axhandle, visual_data, is3D)
234234

235235
hasMarkers = not linehandle.get_marker() == "None"
236236
hasLines = not linehandle.get_linestyle() == "None"
@@ -276,8 +276,8 @@ def _clean_collections(
276276
visual_data = _get_visual_data(axhandle, data, is3D)
277277

278278
if not is3D:
279-
data = _move_points_closer(xLim, yLim, data)
280-
visual_data = _get_visual_data(axhandle, data, is3D)
279+
visual_data = _move_points_closer(xLim, yLim, visual_data)
280+
visual_data = _get_visual_data(axhandle, visual_data, is3D)
281281

282282
hasMarkers = True
283283
hasLines = False
@@ -325,17 +325,17 @@ def _get_visual_limits(fighandle, axhandle):
325325
xLim = np.array(axhandle.get_xlim())
326326
yLim = np.array(axhandle.get_ylim())
327327
if is3D:
328-
zLim = np.array(axhandle.get_ylim())
328+
zLim = np.array(axhandle.get_zlim())
329329

330330
# Check for logarithmic scales
331-
isXlog = axhandle.get_xscale() == "log"
331+
isXlog = _axIsXLog(axhandle)
332332
if isXlog:
333333
xLim = np.log10(xLim)
334-
isYLog = axhandle.get_yscale() == "log"
334+
isYLog = _axIsYLog(axhandle)
335335
if isYLog:
336336
yLim = np.log10(yLim)
337337
if is3D:
338-
isZLog = axhandle.get_zscale() == "log"
338+
isZLog = _axIsZLog(axhandle)
339339
if isZLog:
340340
zLim = np.log10(zLim)
341341

@@ -542,6 +542,18 @@ def _axIs3D(axhandle):
542542
return hasattr(axhandle, "get_zlim")
543543

544544

545+
def _axIsXLog(axhandle):
546+
return axhandle.get_xscale() == "log"
547+
548+
549+
def _axIsYLog(axhandle):
550+
return axhandle.get_yscale() == "log"
551+
552+
553+
def _axIsZLog(axhandle3D):
554+
return axhandle3D.get_zscale() == "log"
555+
556+
545557
def _get_line_data(linehandle):
546558
"""Retrieve 2D or 3D data from line object.
547559

0 commit comments

Comments
 (0)