Reputation: 31
In Inkscape, there is the possibility of saving pdf files along with generating pdf_tex files for automatic formatting into a Latex file.
This can be specified by checking a box after clicking on save and specifying the extension as .pdf
.
I'm interested in directly saving, from my python script, matplotlib figures in that specific format.
Does this possibility exist or can it be done with some tricks?
Upvotes: 3
Views: 3613
Reputation: 51
I created a matplotlib backend a few years ago specifically to support this feature. However, it is only compatible with older matplotlib versions and must be updated (I would appreciate any contributions).
@ImportanceOfBeingErnest has already mentioned this. However, if that solution fails, try it with --export-filename
or -o
instead of --export-pdf
.
Upvotes: 1
Reputation: 339600
An option is to export a pdf from matplotlib and then use inkscape programmatically, i.e. through the command line interface to let it create the desired format.
import matplotlib.pyplot as plt
plt.bar(x=[1,2], height=[3,4], label="Bar")
plt.legend()
plt.xlabel("Label")
plt.title("title")
def savepdf_tex(fig, name, **kwargs):
import subprocess, os
fig.savefig("temp.pdf", format="pdf", **kwargs)
incmd = ["inkscape", "temp.pdf", "--export-pdf={}.pdf".format(name),
"--export-latex"] #"--export-ignore-filters",
subprocess.check_output(incmd)
os.remove("temp.pdf")
savepdf_tex(plt.gcf(), "latest")
plt.show()
Also note that matplotlib can save in the pgf
format. It's definitely worth trying as an alternative to the above. See this example, and append plt.savefig("filename.pgf")
to it. In the latex code use \input{filename.pgf}
.
Upvotes: 1