Skip to content

Commit 34c456c

Browse files
committed
add helper function for comparing plots
1 parent 1347b10 commit 34c456c

File tree

3 files changed

+35
-2
lines changed

3 files changed

+35
-2
lines changed

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_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)