I need to monkey patch a third party application which uses tkinter
for testing purposes. Unfortunately, the third party app also uses matplotlib
, which also depends on tkinter
by default. It turns out that mocking tkinter
class in the way I’m currently doing it is breaking matplotlib
as evidenced by my recent question on stackexchange.
I don’t think there is a way around this without resulting in very ugly code, since it basically hijacks python’s import
mechanism. So an alternative solution is to force my test configuration to use a different backend such as Agg
, and let users use the default tkinter
based backend.
So, here’s my question: can I rely on matplotlib to always generate identical PNG files, regardless of backend? My test suite will be comparing image equality using PIL
and np.array_equal
, so they would always have to be identical. I’ve tested it for a couple of simple cases, and this seems to hold true:
# agg.py
import matplotlib
import matplotlib.pyplot as plt
matplotlib.use("Agg")
fig = plt.figure()
ax = plt.gca()
ax.plot([1,2,2], label='test')
ax.plot([1,1,1])
ax.legend()
ax.grid()
plt.savefig('agg.png')
# tk.py
import matplotlib
import matplotlib.pyplot as plt
fig = plt.figure()
ax = plt.gca()
ax.plot([1,2,2], label='test')
ax.plot([1,1,1])
ax.legend()
ax.grid()
plt.savefig('tk.png')
# compare.py
from PIL import Image
import pdb
import numpy as np
tk = np.array(Image.open('tk.png'))
agg = np.array(Image.open('agg.png'))
assert np.array_equal(tk, agg)
Running the above in succession from a powershell prompt python .\agg.py; python .\tk.py; python .\compare.py
results in no assertion error, indicating that the images are identical.
So, it seems to be independent of backend for this simple example, but can I always rely on this to be true?