nschloe / tikzplotlib

:bar_chart: Save matplotlib figures as TikZ/PGFplots for smooth integration into LaTeX.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

plt.fill_between() causes AttributeError: 'IdentityTransform' object has no attribute 'copy'

MaxGaukler opened this issue · comments

Since Matplotlib 3.3, plt.fill_between() causes errors in tikzplotlib:

from matplotlib import pyplot as plt
import tikzplotlib
plt.fill_between([0, 1], [2, 3])
print(tikzplotlib.get_tikz_code())
Traceback (most recent call last):
  File "bug.py", line 6, in <module>
    print(tikzplotlib.get_tikz_code())
  File "/home/user/.local/lib/python3.6/site-packages/tikzplotlib/_save.py", line 207, in get_tikz_code
    data, content = _recurse(data, figure)
  File "/home/user/.local/lib/python3.6/site-packages/tikzplotlib/_save.py", line 351, in _recurse
    data, children_content = _recurse(data, child)
  File "/home/user/.local/lib/python3.6/site-packages/tikzplotlib/_save.py", line 376, in _recurse
    data, cont = _draw_collection(data, child)
  File "/home/user/.local/lib/python3.6/site-packages/tikzplotlib/_save.py", line 323, in _draw_collection
    return _patch.draw_patchcollection(data, child)
  File "/home/user/.local/lib/python3.6/site-packages/tikzplotlib/_patch.py", line 95, in draw_patchcollection
    path = path.transformed(mpl.transforms.Affine2D(t).translate(*off))
  File "/home/user/.local/lib/python3.6/site-packages/matplotlib/transforms.py", line 1846, in __init__
    self._mtx = matrix.copy()
AttributeError: 'IdentityTransform' object has no attribute 'copy'

The problem is that matplotlib.transforms.Affine2D() expects a numpy.ndarray (or at least the docstring claims so):

https://github.com/matplotlib/matplotlib/blob/019a75231fd8d475c8b6f42a7d0cb1d99e9dcfa5/lib/matplotlib/transforms.py#L1832-L1847

However, we pass it a matplotlib.transforms.IdentityTransform:

if t is None:
t = mpl.transforms.IdentityTransform()
path = path.transformed(mpl.transforms.Affine2D(t).translate(*off))

If my diagnosis is correct, then this code would never work at all, which is strange given the fact that this case is covered by unittests: https://codecov.io/gh/nschloe/tikzplotlib/src/07c0f52bf8cb6808271a00d3199e75dedc7d837b/tikzplotlib/_patch.py#L90...97

I am using matplotlib 3.3.0. The error disappears with matplotlib 3.2.0. This may be related to a change in matplotlib: matplotlib/matplotlib@88e40cb#diff-9d3a2ed9922d92407a1b5e9c4862d1d1L1833-L1835

Maybe Affine2D() was used the wrong way since long ago, but the error only surfaced now?

Your diagnosis seems correct to me. I can confirm that unittests fail right now with matplotlib 3.3 but work well with 3.2.

Fixed now.