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

IndexError when saving

LDAP opened this issue · comments

Using tikzplotlib for contourf sometimes leads to:

  File "D:\Local_Repositories\Studium\Semester_7\bachelorarbeit\code\env\lib\site-packages\tikzplotlib\_save.py", line 260, in save
    code = get_tikz_code(*args, filepath=filepath, **kwargs)
  File "D:\Local_Repositories\Studium\Semester_7\bachelorarbeit\code\env\lib\site-packages\tikzplotlib\_save.py", line 209, in get_tikz_code
    data, content = _recurse(data, figure)
  File "D:\Local_Repositories\Studium\Semester_7\bachelorarbeit\code\env\lib\site-packages\tikzplotlib\_save.py", line 353, in _recurse
    data, children_content = _recurse(data, child)
  File "D:\Local_Repositories\Studium\Semester_7\bachelorarbeit\code\env\lib\site-packages\tikzplotlib\_save.py", line 378, in _recurse
    data, cont = _draw_collection(data, child)
  File "D:\Local_Repositories\Studium\Semester_7\bachelorarbeit\code\env\lib\site-packages\tikzplotlib\_save.py", line 319, in _draw_collection
    return _path.draw_pathcollection(data, child)
  File "D:\Local_Repositories\Studium\Semester_7\bachelorarbeit\code\env\lib\site-packages\tikzplotlib\_path.py", line 214, in draw_pathcollection
    p = obj.get_paths()[0]
IndexError: list index out of range

A solution is to check here:

# "solution" from
# <https://github.com/matplotlib/matplotlib/issues/4672#issuecomment-378702670>
p = obj.get_paths()[0]
ms = {style: MarkerStyle(style) for style in MarkerStyle.markers}
paths = {
style: marker.get_path().transformed(marker.get_transform())
for style, marker in ms.items()
}
marker0 = None
for marker, path in paths.items():
if (
np.array_equal(path.codes, p.codes)
and (path.vertices.shape == p.vertices.shape)
and np.max(np.abs(path.vertices - p.vertices)) < 1.0e-10
):
marker0 = marker
break

if get_paths() returns an empty list and in this case setting marker0 = None.