Skip to content

Commit 8daef84

Browse files
tests
* Test_plottypes: plot, scatter, bar, hist * Test_lineplot: line without markers, line with markers, only markers, sinusoid instead of line
1 parent a2256d0 commit 8daef84

File tree

1 file changed

+189
-27
lines changed

1 file changed

+189
-27
lines changed

test/test_cleanfigure.py

Lines changed: 189 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -318,35 +318,172 @@ def test_opheimSimplify():
318318
assert np.allclose(mask * 1, np.array([1,] + [0,] * 10 + [1,]))
319319

320320

321-
# TODO: test for getVisualData
322-
323-
324-
def test_cleanfigure():
325-
"""test high-level usage for simple example.
326-
Test is successfull if generated tikz code saves correct amount of lines
321+
class Test_plottypes:
322+
"""Testing plot types found here https://matplotlib.org/3.1.1/tutorials/introductory/sample_plots.html
323+
324+
TODO: implement 3D tests
327325
"""
328-
from tikzplotlib import get_tikz_code
329-
330-
x = np.linspace(1, 100, 20)
331-
y = np.linspace(1, 100, 20)
332326

333-
with plt.rc_context(rc=RC_PARAMS):
334-
fig, ax = plt.subplots(1, 1, figsize=(5, 5))
335-
ax.plot(x, y)
336-
ax.set_ylim([20, 80])
337-
ax.set_xlim([20, 80])
338-
raw = get_tikz_code()
339-
340-
cleanfigure.cleanfigure(fig, ax)
341-
clean = get_tikz_code()
342-
343-
# Use number of lines to test if it worked.
344-
# the baseline (raw) should have 20 points
345-
# the clean version (clean) should have 2 points
346-
# the difference in line numbers should therefore be 2
347-
numLinesRaw = raw.count("\n")
348-
numLinesClean = clean.count("\n")
349-
assert numLinesRaw - numLinesClean == 18
327+
def test_plot(self):
328+
from tikzplotlib import get_tikz_code
329+
330+
x = np.linspace(1, 100, 20)
331+
y = np.linspace(1, 100, 20)
332+
333+
with plt.rc_context(rc=RC_PARAMS):
334+
fig, ax = plt.subplots(1, 1, figsize=(5, 5))
335+
ax.plot(x, y)
336+
ax.set_ylim([20, 80])
337+
ax.set_xlim([20, 80])
338+
cleanfigure.cleanfigure(fig, ax)
339+
assert True
340+
341+
def test_scatter(self):
342+
from tikzplotlib import get_tikz_code
343+
344+
x = np.linspace(1, 100, 20)
345+
y = np.linspace(1, 100, 20)
346+
with plt.rc_context(rc=RC_PARAMS):
347+
fig, ax = plt.subplots(1, 1, figsize=(5, 5))
348+
ax.scatter(x, y)
349+
ax.set_ylim([20, 80])
350+
ax.set_xlim([20, 80])
351+
cleanfigure.cleanfigure(fig, ax)
352+
assert True
353+
354+
def test_bar(self):
355+
from tikzplotlib import get_tikz_code
356+
357+
x = np.linspace(1, 100, 20)
358+
y = np.linspace(1, 100, 20)
359+
with plt.rc_context(rc=RC_PARAMS):
360+
fig, ax = plt.subplots(1, 1, figsize=(5, 5))
361+
ax.bar(x, y)
362+
ax.set_ylim([20, 80])
363+
ax.set_xlim([20, 80])
364+
cleanfigure.cleanfigure(fig, ax)
365+
assert True
366+
367+
def test_hist(self):
368+
"""creates same test case as bar"""
369+
from tikzplotlib import get_tikz_code
370+
371+
x = np.linspace(1, 100, 20)
372+
y = np.linspace(1, 100, 20)
373+
with plt.rc_context(rc=RC_PARAMS):
374+
fig, ax = plt.subplots(1, 1, figsize=(5, 5))
375+
ax.hist(x, y)
376+
ax.set_ylim([20, 80])
377+
ax.set_xlim([20, 80])
378+
cleanfigure.cleanfigure(fig, ax)
379+
assert True
380+
381+
382+
class Test_lineplot:
383+
def test_line_no_markers(self):
384+
"""test high-level usage for simple example.
385+
Test is successfull if generated tikz code saves correct amount of lines
386+
"""
387+
from tikzplotlib import get_tikz_code
388+
389+
x = np.linspace(1, 100, 20)
390+
y = np.linspace(1, 100, 20)
391+
392+
with plt.rc_context(rc=RC_PARAMS):
393+
fig, ax = plt.subplots(1, 1, figsize=(5, 5))
394+
ax.plot(x, y, linestyle="-", marker="None")
395+
ax.set_ylim([20, 80])
396+
ax.set_xlim([20, 80])
397+
raw = get_tikz_code()
398+
399+
cleanfigure.cleanfigure(fig, ax)
400+
clean = get_tikz_code()
401+
402+
# Use number of lines to test if it worked.
403+
# the baseline (raw) should have 20 points
404+
# the clean version (clean) should have 2 points
405+
# the difference in line numbers should therefore be 2
406+
numLinesRaw = raw.count("\n")
407+
numLinesClean = clean.count("\n")
408+
assert numLinesRaw - numLinesClean == 18
409+
410+
def test_no_line_markers(self):
411+
"""test high-level usage for simple example.
412+
Test is successfull if generated tikz code saves correct amount of lines
413+
"""
414+
from tikzplotlib import get_tikz_code
415+
416+
x = np.linspace(1, 100, 20)
417+
y = np.linspace(1, 100, 20)
418+
419+
with plt.rc_context(rc=RC_PARAMS):
420+
fig, ax = plt.subplots(1, 1, figsize=(5, 5))
421+
ax.plot(x, y, linestyle="None", marker="*")
422+
ax.set_ylim([20, 80])
423+
ax.set_xlim([20, 80])
424+
raw = get_tikz_code()
425+
426+
cleanfigure.cleanfigure(fig, ax)
427+
clean = get_tikz_code()
428+
429+
# Use number of lines to test if it worked.
430+
# the baseline (raw) should have 20 points
431+
# the clean version (clean) should have 2 points
432+
# the difference in line numbers should therefore be 2
433+
numLinesRaw = raw.count("\n")
434+
numLinesClean = clean.count("\n")
435+
assert numLinesRaw - numLinesClean == 6
436+
437+
def test_line_markers(self):
438+
"""test high-level usage for simple example.
439+
Test is successfull if generated tikz code saves correct amount of lines
440+
"""
441+
from tikzplotlib import get_tikz_code
442+
443+
x = np.linspace(1, 100, 20)
444+
y = np.linspace(1, 100, 20)
445+
446+
with plt.rc_context(rc=RC_PARAMS):
447+
fig, ax = plt.subplots(1, 1, figsize=(5, 5))
448+
ax.plot(x, y, linestyle="-", marker="*")
449+
ax.set_ylim([20, 80])
450+
ax.set_xlim([20, 80])
451+
raw = get_tikz_code()
452+
453+
cleanfigure.cleanfigure(fig, ax)
454+
clean = get_tikz_code()
455+
456+
# Use number of lines to test if it worked.
457+
# the baseline (raw) should have 20 points
458+
# the clean version (clean) should have 2 points
459+
# the difference in line numbers should therefore be 2
460+
numLinesRaw = raw.count("\n")
461+
numLinesClean = clean.count("\n")
462+
assert numLinesRaw - numLinesClean == 6
463+
464+
def test_sine(self):
465+
from tikzplotlib import get_tikz_code
466+
467+
x = np.linspace(1, 2 * np.pi, 100)
468+
y = np.sin(8 * x)
469+
470+
with plt.rc_context(rc=RC_PARAMS):
471+
fig, ax = plt.subplots(1, 1, figsize=(5, 5))
472+
ax.plot(x, y, linestyle="-", marker="*")
473+
ax.set_xlim([0.5 * np.pi, 1.5 * np.pi])
474+
ax.set_ylim([-1, 1])
475+
raw = get_tikz_code()
476+
477+
cleanfigure.cleanfigure(fig, ax)
478+
clean = get_tikz_code()
479+
480+
# Use number of lines to test if it worked.
481+
# the baseline (raw) should have 20 points
482+
# the clean version (clean) should have 2 points
483+
# the difference in line numbers should therefore be 2
484+
numLinesRaw = raw.count("\n")
485+
numLinesClean = clean.count("\n")
486+
assert numLinesRaw - numLinesClean == 39
350487

351488

352489
def test_segmentVisible():
@@ -426,3 +563,28 @@ def test_segmentsIntersect():
426563
mask = cleanfigure.segmentsIntersect(X1, X2, X3, X4)
427564
assert np.allclose(mask * 1, np.zeros_like(mask))
428565

566+
567+
def test_pixelate():
568+
xToPix = 49.952
569+
yToPix = 49.952
570+
xData = np.array(
571+
[
572+
21.842,
573+
27.053,
574+
32.263,
575+
37.474,
576+
42.684,
577+
47.895,
578+
53.105,
579+
58.316,
580+
63.526,
581+
68.737,
582+
73.947,
583+
79.158,
584+
]
585+
)
586+
yData = xData.copy()
587+
mask = cleanfigure.pixelate(xData, yData, xToPix, yToPix)
588+
assert mask.shape == (12,)
589+
assert np.all(mask)
590+

0 commit comments

Comments
 (0)