A Docker and AWS utility package
Project description
Matplotlib2LaTeX
An high-level python library to have your plots in pgf, the perfect format for an awesome LaTeX report/publication. The documentation is available at this link
Sometimes there may be an error about the a matplotlib lockfile. Just restart the kernel.
Installation
To install simply run
pip install mpl2latex
it requires matplotlib to work.
Example of use on jupyter notebook
First, we import numpy, matplotlib and the new library MPL2LATEX. We will then make some examples with different type of implementation
import numpy as np
import matplotlib.pyplot as plt
import matplotlib
from mpl2latex import Matplotlib2LaTeX, latex_figsize
We will plot some simple data, let's say a quadratic function
x = np.linspace(1, 100, 1000)
fig, ax = plt.subplots(figsize=latex_figsize( wf=0.8) ) # width of an entire latex column
ax.plot(x, x**2)
ax.set_xlabel('x')
ax.set_ylabel('$x^2$')
ax.set_title('Trial')
plt.show()
Now, we make the same but with the parameters of Matplotlib2LaTeX, using it from the start
fig, ax = plt.subplots(figsize=latex_figsize( wf=0.8) ) # width of an entire latex column
ltx = Matplotlib2LaTeX(fig)
ltx.ax[0].plot(x, x**2)
ltx.ax[0].set_xlabel('x')
ltx.ax[0].set_ylabel('$x^2$')
ltx.ax[0].set_title('Trial')
ltx.show()
We can easily come back to the default settings for matplolib
ltx.reset()
fig, ax = plt.subplots(figsize=latex_figsize( wf=0.8) ) # width of an entire latex column
ax.plot(x, x**2)
ax.set_xlabel('x')
ax.set_ylabel('$x^2$')
ax.set_title('Trial')
plt.show()
But we can also save the figure in pgf and then we automatically come back to our usual backend to visualize the plots in the jupyter environment!
fig, ax = plt.subplots(figsize=latex_figsize( wf=0.8) ) # width of an entire latex column
ltx = Matplotlib2LaTeX(fig)
ltx.ax[0].plot(x, x**2)
ltx.ax[0].set_xlabel('x')
ltx.ax[0].set_ylabel('$x^2$')
ltx.ax[0].set_title('Trial')
ltx.save_fig('Trial.pgf')
You have your pgf file, but you can continue to work on the notebook!
fig, ax = plt.subplots(figsize=latex_figsize( wf=1) ) # width of an entire latex column
ax.plot(x, x**3)
ax.set_xlabel('x')
ax.set_ylabel('$x^3$')
ax.set_title('Trial')
plt.show()
Using the pgf output in LaTeX
Now that we have finally reproduced the plot in pgf we show how to import it in a LaTeX document. It is really straightforward, using the pgfplots
package. We make an example of the following here:
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
\begin{figure}
\centering
\input{Trial.pgf}
\caption{Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.}
\label{fig:trial}
\end{figure}
which gives the following output. It is a little blurred, but you can look at the original .pdf
in the folder Examples_files
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.