PGF/TikZ format for the plots#
A nice format in which you can work with your plots in latex is PGF/TikZ. With it you can either create your plot directly in latex or export the plot from python using tikzplotlib library.
How to save a figure in python in PGF/TikZ format#
First, you will have to import the corresponding library
import tikzplotlib
.
And then, after you made your figure you save it as
tikzplotlib.save("figure.tex")
.
How to embed it in the main latex file#
Depending on how big the script for your plot is, you can choose between two possibilities:
Embed it directly as a piece of latex code using the environment
\begin{tikzpicture} ... \end{tikzpicture}
Have it as a separate latex file \verb!figure.tex! that you import in the main file using
\scalebox{1.}{\input{figure.tex}}
What is next?#
Now you can take a look at the script that you have just embedded into your file. You may leave it as it is or you may want to play around with it changing/adding stuff in/to your plot. You can look up what different parts of the code do, for example, here.
Pros#
Figures have fonts and colors from your latex file which makes the figure look more naturally embedded in the text.
All the charts and plots are vectorized and therefore are infinitely scalable.
The whole plot is a latex code, which gives you a full control over it. You can edit it directly in the main latex file.
This allows for a very precise control of line weights and the positioning of labels on charts, as well as exact control of the fonts.
It is also easy to add additional symbols, shapes or texts in the figure. You have the whole plethora of toll available in tikz.
All the data from the plot is preserved. It allows to fully manipulate the data inside the latex file such as to change the scale, axes, apply some transformation to the data, etc.
It is very easy to plot basic curves and shapes that are defined by a function or any mathematical expression. You can do it directly in your latex file and do not need any additional software for that.
Cons#
The main file becomes heavier.
If you have plots with a lot of data it might rise a problem of compilation (overleaf is even more sensitive to that).
Generally makes the compiling time longer.
Has bugs when you want to make a 2D surface log-scaled plot.
References#
Main manual on how to make PGF/TikZ plots in latex.
One more manual on PGFplot.
A very minimal introduction to TikZ.
More examples of what you can do in TikZ.