Debian Codegraph @

texmf-dist/doc/lualatex/pyluatex/example/matplotlib-external.tex

raw
1 %% Copyright 2021-2026 Tobias Enderle
2 %%
3 %% This work may be distributed and/or modified under the
4 %% conditions of the LaTeX Project Public License, either version 1.3c
5 %% of this license or (at your option) any later version.
6 %% The latest version of this license is in
7 %% http://www.latex-project.org/lppl.txt
8 %% and version 1.3c or later is part of all distributions of LaTeX
9 %% version 2005/12/01 or later.
10
11 % DESCRIPTION
12 % Demonstrates how *matplotlib* plots can be generated and included in a
13 % document
14
15 \documentclass{article}
16
17 \usepackage{pyluatex}
18 \usepackage{graphicx}
19
20 \begin{document}
21
22 In this document, we demonstrate how \emph{matplotlib} plots can be generated
23 and included in a document. In the Python code in this document, the plot is
24 configured and saved to an external PDF file. This PDF file is then
25 included as a picture using \verb|\includegraphics|.
26
27 \begin{center}
28 \begin{python}
29 # example based on
30 # https://matplotlib.org/stable/gallery/lines_bars_and_markers/simple_plot.html
31 import matplotlib.pyplot as plt
32 import numpy as np
33
34 t = np.arange(0.0, 2.0, 0.01)
35 s = 1 + np.sin(2 * np.pi * t)
36
37 fig, ax = plt.subplots()
38 ax.plot(t, s)
39
40 ax.set(xlabel='time (s)', ylabel='voltage (mV)',
41 title='About as simple as it gets, folks')
42 ax.grid()
43
44 fig.savefig('matplotlib-plot.pdf', bbox_inches='tight', pad_inches=0.1)
45 \end{python}
46 \includegraphics[width=0.8\textwidth]{matplotlib-plot.pdf}
47 \end{center}
48
49 For an alternative approach using the PGF backend, consider the example
50 \verb|matplotlib-pgf.tex|.
51
52 \end{document}