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

Tikzplotlib error on 3D surface plot

peterdsharpe opened this issue · comments

Problem Description

Trying to plot a simple 3D surface plot and then save the results with tikzplotlib throws an error.

Minimum working example:

from matplotlib import pyplot as plt
import tikzplotlib
import numpy as np

X, Y = np.meshgrid(np.linspace(0, 1), np.linspace(0, 1))

fig, ax = plt.subplots(subplot_kw={"projection": "3d"})
ax.plot_surface(X, Y, X + Y)

tikzplotlib.save("C:/Users/User/Downloads/rosen.tex")

plt.show()

Results in an AttributeError thown by tikzplotlib:

Traceback (most recent call last):
  File "C:\Users\User\anaconda3\lib\site-packages\IPython\core\interactiveshell.py", line 3437, in run_code
    exec(code_obj, self.user_global_ns, self.user_ns)
  File "<ipython-input-2-c3ca481ad9a6>", line 1, in <module>
    runfile('C:/Users/User/AppData/Roaming/JetBrains/PyCharm2021.1/scratches/scratch_35.py', wdir='C:/Users/User/AppData/Roaming/JetBrains/PyCharm2021.1/scratches')
  File "C:\Program Files\JetBrains\PyCharm 2020.1\plugins\python\helpers\pydev\_pydev_bundle\pydev_umd.py", line 197, in runfile
    pydev_imports.execfile(filename, global_vars, local_vars)  # execute the script
  File "C:\Program Files\JetBrains\PyCharm 2020.1\plugins\python\helpers\pydev\_pydev_imps\_pydev_execfile.py", line 18, in execfile
    exec(compile(contents+"\n", file, 'exec'), glob, loc)
  File "C:/Users/User/AppData/Roaming/JetBrains/PyCharm2021.1/scratches/scratch_35.py", line 11, in <module>
    tikzplotlib.save("C:/Users/User/Downloads/rosen.tex")
  File "C:\Users\User\anaconda3\lib\site-packages\tikzplotlib\_save.py", line 260, in save
    code = get_tikz_code(*args, filepath=filepath, **kwargs)
  File "C:\Users\User\anaconda3\lib\site-packages\tikzplotlib\_save.py", line 209, in get_tikz_code
    data, content = _recurse(data, figure)
  File "C:\Users\User\anaconda3\lib\site-packages\tikzplotlib\_save.py", line 353, in _recurse
    data, children_content = _recurse(data, child)
  File "C:\Users\User\anaconda3\lib\site-packages\tikzplotlib\_save.py", line 378, in _recurse
    data, cont = _draw_collection(data, child)
  File "C:\Users\User\anaconda3\lib\site-packages\tikzplotlib\_save.py", line 325, in _draw_collection
    return _patch.draw_patchcollection(data, child)
  File "C:\Users\User\anaconda3\lib\site-packages\tikzplotlib\_patch.py", line 81, in draw_patchcollection
    ecs = ensure_list(obj.get_edgecolor())
  File "C:\Users\User\AppData\Roaming\Python\Python38\site-packages\mpl_toolkits\mplot3d\art3d.py", line 883, in get_edgecolor
    return self._edgecolors2d
AttributeError: 'Poly3DCollection' object has no attribute '_edgecolors2d'

The associated figure:
image

Version Info

  • Windows 10
  • Python 3.8.8, Anaconda install
  • Tikzplotlib 0.9.8 (latest)
  • Matplotlib 3.4.1 (latest)

Duplicate of #119.

UPDATE:

Following another issue here #416, I tried adding a plt.savefig() before the tikzplotlib.save(). Now, when I run it, no AttributeError is produced and the .tex file is created.

Specifically, the input looks like:

from matplotlib import pyplot as plt
import tikzplotlib
import numpy as np

X, Y = np.meshgrid(np.linspace(0, 1), np.linspace(0, 1))

fig, ax = plt.subplots(subplot_kw={"projection": "3d"})
ax.plot_surface(X, Y, X + Y)

plt.savefig("C:/Users/User/Downloads/rosen.png")  # THIS IS THE NEW LINE ADDED
tikzplotlib.save("C:/Users/User/Downloads/rosen.tex")

plt.show()

Happy to upload the resulting *.tex file if it's helpful.

@nschloe Are you sure that this is a matplotlib issue and that this issue should be closed? Seems strange that a plt.savefig would change tikzplotlib behavior.

Even if it is a matplotlib issue, internally adding a plt.savefig() to tikzplotlib.save could save tikzplotlib users a lot of head-scratching while the matplotlib team works on the "real" fix.

Wait, it works and produces the correct result for you? A 3D plot? I want to believe, so show me a screenshot of your LaTeX output.