Skip to content
Advertisement

Is it possible to export 3D charts from MatPlotLib to TikZ?

I am trying to export a 3D diagram from Matplotlib to TikZ, using import tikzplotlib. This is the code executed in Jupyter NoteBook:

from mpl_toolkits import mplot3d
import numpy as np
import matplotlib.pyplot as plt
fig = plt.figure()
ax = plt.axes(projection='3d')
z = np.linspace(0, 1, 100)
x = z * np.sin(20 * z)
y = z * np.cos(20 * z)
ax.plot3D(x, y, z, 'gray')
ax.set_title('Intentando con 3D')
plt.show()

import tikzplotlib

tikzplotlib.save("papon.tex")
tikzplotlib.Flavors.latex.preamble()

The resulting papon.tex file shows the following when compiled:

% This file was created by tikzplotlib v0.9.8.
begin{tikzpicture}

end{tikzpicture}

Missing any configuration or code modification? Or, simply, is it not possible to correctly export a 3D chart -with these characteristics- to TikZ?

Advertisement

Answer

The answer is yes to both questions.

The first problem is that you save the figure after show, which consumes the plot, and you save an empty new plot thereafter. The plot gets exported when you remove the following line:

plt.show()

However, the second problem is that 3d plots are indeed not supported. See the tikzplotlib documentation:

Of course, not all figures produced by matplotlib can be converted without error. Notably, 3D plots don’t work.

The output of your example above looks like the following, and the tikz output is a 2d plot. So there is no quick solution to turn that into the desired plot:

Example output

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement