Skip to content

Commit d110c32

Browse files
authored
Merge pull request #175 from nschloe/easy-compare
Easy compare
2 parents 1347b10 + 486695a commit d110c32

File tree

5 files changed

+69
-2
lines changed

5 files changed

+69
-2
lines changed

test/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
tex/
2+
zzz_readme_test.py

test/helpers.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
import imagehash
1313
import matplotlib
14+
import matplotlib.image as mpimg
1415
import matplotlib.pyplot as plt
1516
from PIL import Image
1617

@@ -151,6 +152,30 @@ def get_details(self):
151152
return
152153

153154

155+
def compare_with_latex(fig):
156+
157+
# Store original as PNG
158+
_, prefix = tempfile.mkstemp()
159+
filename = prefix + '.png'
160+
plt.savefig(filename, bbox_inches='tight')
161+
162+
# Get PNG of LaTeX conversion
163+
obj = Phash(fig)
164+
165+
# Display both
166+
plt.figure()
167+
plt.subplot(121)
168+
img0 = mpimg.imread(filename)
169+
plt.imshow(img0)
170+
#
171+
plt.subplot(122)
172+
img1 = mpimg.imread(obj.png_file)
173+
plt.imshow(img1)
174+
175+
plt.show()
176+
return
177+
178+
154179
def print_tree(obj, indent=''):
155180
'''Recursively prints the tree structure of the matplotlib object.
156181
'''

test/test_basic_sin.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# -*- coding: utf-8 -*-
22
#
3-
from helpers import Phash
3+
import helpers
44

55

66
def plot():
@@ -24,5 +24,9 @@ def plot():
2424

2525

2626
def test():
27-
phash = Phash(plot())
27+
phash = helpers.Phash(plot())
2828
assert phash.phash == '1f36e5c621c1e7c1', phash.get_details()
29+
30+
31+
if __name__ == '__main__':
32+
helpers.compare_with_latex(plot())

test/test_legend_labels.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# -*- coding: utf-8 -*-
2+
#
3+
import helpers
4+
5+
6+
def plot():
7+
from matplotlib import pyplot as plt
8+
import numpy as np
9+
10+
fig = plt.figure()
11+
12+
x = np.ma.arange(0, 2*np.pi, 0.02)
13+
y1 = np.sin(1*x)
14+
y2 = np.sin(2*x)
15+
y3 = np.sin(3*x)
16+
17+
plt.plot(x, y1, label='y1')
18+
plt.plot(x, y2, label=None)
19+
plt.plot(x, y3, label='y4')
20+
plt.legend()
21+
22+
return fig
23+
24+
25+
def test():
26+
phash = helpers.Phash(plot())
27+
assert phash.phash == 'fb7c5e0aaed60094', phash.get_details()
28+
return
29+
30+
31+
if __name__ == '__main__':
32+
# print(helpers.Phash(plot()).phash)
33+
helpers.compare_with_latex(plot())

test/test_legends.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,3 +33,7 @@ def test():
3333
phash = helpers.Phash(plot())
3434
assert phash.phash == '7f7ca18386d10eaa', phash.get_details()
3535
return
36+
37+
38+
if __name__ == '__main__':
39+
helpers.compare_with_latex(plot())

0 commit comments

Comments
 (0)