Introductory example - Plotting sin#
This is a general example demonstrating a Matplotlib plot output, embedded
reST, the use of math notation and cross-linking to other examples. It would be
useful to compare the
source Python file with the
Source files for gallery examples should start with a triple-quoted header docstring. Anything before the docstring is ignored by Sphinx-Gallery and will not appear in the rendered output, nor will it be executed. This docstring requires a reST header, which is used as the title of the example and to correctly build cross-referencing links.
Code and embedded reST text blocks follow the docstring. The first block
immediately after the docstring is deemed a code block, by default, unless you
specify it to be a text block using a line of
#%% (see below).
All code blocks get executed by Sphinx-Gallery and any output, including plots
will be captured. Typically, code and text blocks are interspersed to provide
narrative explanations of what the code is doing or interpretations of code
Mathematical expressions can be included as LaTeX, and will be rendered with
MathJax. To include displayed math notation, use the directive
To include inline math notation use the
:math: role. For example, we are
about to plot the following function:
Here the function \(\sin\) is evaluated at each point the variable \(x\) is defined. When including LaTeX in a Python string, ensure that you escape the backslashes or use a raw docstring. You do not need to do this in text blocks (see below).
To include embedded reST, use a line of >= 20
#%% between your
reST and your code (see Embed reST in your example Python files). This separates your example
into distinct text and code blocks. You can continue writing code below the
embedded reST text block:
print("This example shows a sin plot!")
This example shows a sin plot!
LaTeX syntax in the text blocks does not require backslashes to be escaped:
You can refer to an example from any part of the documentation,
including from other examples. Sphinx-Gallery automatically creates reference
labels for each example. The label consists of the
.py file name,
sphx_glr_ and the name of the
folder(s) the example is in. Below, the example we want to
cross-reference is in
Configure and use Sphinx-Gallery), then the subdirectory
(since the example is within a sub-gallery). The file name of the example is
plot_syntaxerror.py. We can thus cross-link to the example ‘SyntaxError’
See SyntaxError for an example with an error.
Total running time of the script: (0 minutes 0.856 seconds)
Estimated memory usage: 19 MB