<h3 dir="auto">Bug report</h3>
<p dir="auto"><strong>Bug summary</strong></p>
<p dir="auto">When writing a generical code to generate plots with sphinx documentation, this function cannot be called by other functions from the documentation in the same file</p>
<p dir="auto"><strong>Code for reproduction</strong></p>
<p dir="auto">Repository layout:</p>
<ul dir="auto">
<li>docs
<ul dir="auto">
<li>source
<ul dir="auto">
<li>pyplots
<ul dir="auto">
<li>test.py</li>
</ul>
</li>
<li>conf.py</li>
<li>index.rst</li>
</ul>
</li>
</ul>
</li>
<li>package</li>
</ul>
<p dir="auto">Content of file <code class="notranslate">test.py</code></p>
<div class="highlight highlight-source-python notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="import matplotlib.pyplot as plt


def generate_plot_according_to_step(step):

    plt.figure()

    plt.plot([1, 2], [2, 3])

    if step &gt;= 1:
        plt.title(&quot;Adding a title&quot;)

    if step &gt;= 2:
        plt.suptitle('Adding a suptitle')

    plt.show()


def step0():
    generate_plot_according_to_step(0)


def step1():
    generate_plot_according_to_step(1)


def step2():
    generate_plot_according_to_step(2)"><pre class="notranslate"><span class="pl-k">import</span> <span class="pl-s1">matplotlib</span>.<span class="pl-s1">pyplot</span> <span class="pl-k">as</span> <span class="pl-s1">plt</span>


<span class="pl-k">def</span> <span class="pl-en">generate_plot_according_to_step</span>(<span class="pl-s1">step</span>):

    <span class="pl-s1">plt</span>.<span class="pl-en">figure</span>()

    <span class="pl-s1">plt</span>.<span class="pl-en">plot</span>([<span class="pl-c1">1</span>, <span class="pl-c1">2</span>], [<span class="pl-c1">2</span>, <span class="pl-c1">3</span>])

    <span class="pl-k">if</span> <span class="pl-s1">step</span> <span class="pl-c1">&gt;=</span> <span class="pl-c1">1</span>:
        <span class="pl-s1">plt</span>.<span class="pl-en">title</span>(<span class="pl-s">"Adding a title"</span>)

    <span class="pl-k">if</span> <span class="pl-s1">step</span> <span class="pl-c1">&gt;=</span> <span class="pl-c1">2</span>:
        <span class="pl-s1">plt</span>.<span class="pl-en">suptitle</span>(<span class="pl-s">'Adding a suptitle'</span>)

    <span class="pl-s1">plt</span>.<span class="pl-en">show</span>()


<span class="pl-k">def</span> <span class="pl-en">step0</span>():
    <span class="pl-en">generate_plot_according_to_step</span>(<span class="pl-c1">0</span>)


<span class="pl-k">def</span> <span class="pl-en">step1</span>():
    <span class="pl-en">generate_plot_according_to_step</span>(<span class="pl-c1">1</span>)


<span class="pl-k">def</span> <span class="pl-en">step2</span>():
    <span class="pl-en">generate_plot_according_to_step</span>(<span class="pl-c1">2</span>)</pre></div>
<p dir="auto">Content of <code class="notranslate">conf.py</code> :</p>
<div class="highlight highlight-source-python notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="extensions = [
        'sphinx.ext.autodoc',
        'matplotlib.sphinxext.plot_directive'
]"><pre class="notranslate"><span class="pl-s1">extensions</span> <span class="pl-c1">=</span> [
        <span class="pl-s">'sphinx.ext.autodoc'</span>,
        <span class="pl-s">'matplotlib.sphinxext.plot_directive'</span>
]</pre></div>
<p dir="auto">Then when referencing the steps functions in docstring anywhere in the package, such as:</p>
<div class="highlight highlight-source-python notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="&quot;&quot;&quot;
.. plot:: pyplots/test.py step0

.. plot:: pyplots/test.py step1

.. plot:: pyplots/test.py step2

&quot;&quot;&quot;"><pre class="notranslate"><span class="pl-s">"""</span>
<span class="pl-s">.. plot:: pyplots/test.py step0</span>
<span class="pl-s"></span>
<span class="pl-s">.. plot:: pyplots/test.py step1</span>
<span class="pl-s"></span>
<span class="pl-s">.. plot:: pyplots/test.py step2</span>
<span class="pl-s"></span>
<span class="pl-s">"""</span></pre></div>
<p dir="auto"><strong>Actual outcome</strong></p>
<p dir="auto">When building the doc through the following steps:</p>
<div class="highlight highlight-source-powershell notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="

cd .\docs

sphinx-quickstart

sphinx-apidoc -f -o source/ ../

.\make.bat html"><pre class="notranslate">cd .\docs

sphinx<span class="pl-k">-</span>quickstart

sphinx<span class="pl-k">-</span>apidoc <span class="pl-k">-f</span> <span class="pl-k">-</span>o source<span class="pl-k">/</span> ..<span class="pl-k">/</span>

.\<span class="pl-c1">make.bat</span> html</pre></div>
<p dir="auto">The following message arise</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content=" WARNING: Exception occurred in plotting test
 from path\to\repository\docs\source\pyplots/test.py:
Traceback (most recent call last):
  File &quot;path\to\repository\.venv\lib\site-packages\matplotlib\sphinxext\plot_directive.py&quot;, line 485, in run_code
    exec(function_name + &quot;()&quot;, ns)
  File &quot;&lt;string&gt;&quot;, line 1, in &lt;module&gt;
NameError: name 'step0' is not defined"><pre class="notranslate"><code class="notranslate"> WARNING: Exception occurred in plotting test
 from path\to\repository\docs\source\pyplots/test.py:
Traceback (most recent call last):
  File "path\to\repository\.venv\lib\site-packages\matplotlib\sphinxext\plot_directive.py", line 485, in run_code
    exec(function_name + "()", ns)
  File "&lt;string&gt;", line 1, in &lt;module&gt;
NameError: name 'step0' is not defined
</code></pre></div>
<p dir="auto"><strong>Expected outcome</strong></p>
<p dir="auto">It seems that it is not possible to use a generical function such as the one presented to generate documentation.<br>
Either it is considered a bug and should be fixed or the error message should be more explicit, as it is currently inaccurate, as the step functions exists</p>
<p dir="auto"><strong>Matplotlib version</strong></p>
<ul dir="auto">
<li>Operating system: Windows 10</li>
<li>Matplotlib version (<code class="notranslate">import matplotlib; print(matplotlib.__version__)</code>):  3.4.2</li>
<li>Matplotlib backend (<code class="notranslate">print(matplotlib.get_backend())</code>): TkAgg</li>
<li>Python version: 3.9.5</li>
<li>Other libraries: Sphinx 4.0.2</li>
</ul>
<p dir="auto">Every library has been installed through pip</p>
<h3 dir="auto">Bug summary</h3>
<p dir="auto">Matplotlib is unable to apply the boundaries in the colorbar after updating to 3.5.0. Please compare the results in the screenshots between version 3.5.0 and the previous version 3.4.3.</p>
<h3 dir="auto">Code for reproduction</h3>
<div class="highlight highlight-source-python notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="import matplotlib.pyplot as plt
import numpy as np
from matplotlib import cm

n_radii = 8
n_angles = 36

# Make radii and angles spaces (radius r=0 omitted to eliminate duplication).
radii = np.linspace(0.125, 1.0, n_radii)
angles = np.linspace(0, 2*np.pi, n_angles, endpoint=False)[..., np.newaxis]

# Convert polar (radii, angles) coords to cartesian (x, y) coords.
# (0, 0) is manually added at this stage,  so there will be no duplicate
# points in the (x, y) plane.
x = np.append(0, (radii*np.cos(angles)).flatten())
y = np.append(0, (radii*np.sin(angles)).flatten())

# Compute z to make the pringle surface.
z = np.sin(-x*y)

fig, ax = plt.subplots(subplot_kw={&quot;projection&quot;: &quot;3d&quot;})

surf = ax.plot_trisurf(x, y, z, linewidth=0.2, antialiased=True, cmap=cm.coolwarm)

boundaries = np.linspace(np.min(z), np.max(z), 10, endpoint=True)
fig.colorbar(surf, shrink=0.5, boundaries=boundaries, pad=0.1)

plt.show()"><pre class="notranslate"><span class="pl-k">import</span> <span class="pl-s1">matplotlib</span>.<span class="pl-s1">pyplot</span> <span class="pl-k">as</span> <span class="pl-s1">plt</span>
<span class="pl-k">import</span> <span class="pl-s1">numpy</span> <span class="pl-k">as</span> <span class="pl-s1">np</span>
<span class="pl-k">from</span> <span class="pl-s1">matplotlib</span> <span class="pl-k">import</span> <span class="pl-s1">cm</span>

<span class="pl-s1">n_radii</span> <span class="pl-c1">=</span> <span class="pl-c1">8</span>
<span class="pl-s1">n_angles</span> <span class="pl-c1">=</span> <span class="pl-c1">36</span>

<span class="pl-c"># Make radii and angles spaces (radius r=0 omitted to eliminate duplication).</span>
<span class="pl-s1">radii</span> <span class="pl-c1">=</span> <span class="pl-s1">np</span>.<span class="pl-en">linspace</span>(<span class="pl-c1">0.125</span>, <span class="pl-c1">1.0</span>, <span class="pl-s1">n_radii</span>)
<span class="pl-s1">angles</span> <span class="pl-c1">=</span> <span class="pl-s1">np</span>.<span class="pl-en">linspace</span>(<span class="pl-c1">0</span>, <span class="pl-c1">2</span><span class="pl-c1">*</span><span class="pl-s1">np</span>.<span class="pl-s1">pi</span>, <span class="pl-s1">n_angles</span>, <span class="pl-s1">endpoint</span><span class="pl-c1">=</span><span class="pl-c1">False</span>)[..., <span class="pl-s1">np</span>.<span class="pl-s1">newaxis</span>]

<span class="pl-c"># Convert polar (radii, angles) coords to cartesian (x, y) coords.</span>
<span class="pl-c"># (0, 0) is manually added at this stage,  so there will be no duplicate</span>
<span class="pl-c"># points in the (x, y) plane.</span>
<span class="pl-s1">x</span> <span class="pl-c1">=</span> <span class="pl-s1">np</span>.<span class="pl-en">append</span>(<span class="pl-c1">0</span>, (<span class="pl-s1">radii</span><span class="pl-c1">*</span><span class="pl-s1">np</span>.<span class="pl-en">cos</span>(<span class="pl-s1">angles</span>)).<span class="pl-en">flatten</span>())
<span class="pl-s1">y</span> <span class="pl-c1">=</span> <span class="pl-s1">np</span>.<span class="pl-en">append</span>(<span class="pl-c1">0</span>, (<span class="pl-s1">radii</span><span class="pl-c1">*</span><span class="pl-s1">np</span>.<span class="pl-en">sin</span>(<span class="pl-s1">angles</span>)).<span class="pl-en">flatten</span>())

<span class="pl-c"># Compute z to make the pringle surface.</span>
<span class="pl-s1">z</span> <span class="pl-c1">=</span> <span class="pl-s1">np</span>.<span class="pl-en">sin</span>(<span class="pl-c1">-</span><span class="pl-s1">x</span><span class="pl-c1">*</span><span class="pl-s1">y</span>)

<span class="pl-s1">fig</span>, <span class="pl-s1">ax</span> <span class="pl-c1">=</span> <span class="pl-s1">plt</span>.<span class="pl-en">subplots</span>(<span class="pl-s1">subplot_kw</span><span class="pl-c1">=</span>{<span class="pl-s">"projection"</span>: <span class="pl-s">"3d"</span>})

<span class="pl-s1">surf</span> <span class="pl-c1">=</span> <span class="pl-s1">ax</span>.<span class="pl-en">plot_trisurf</span>(<span class="pl-s1">x</span>, <span class="pl-s1">y</span>, <span class="pl-s1">z</span>, <span class="pl-s1">linewidth</span><span class="pl-c1">=</span><span class="pl-c1">0.2</span>, <span class="pl-s1">antialiased</span><span class="pl-c1">=</span><span class="pl-c1">True</span>, <span class="pl-s1">cmap</span><span class="pl-c1">=</span><span class="pl-s1">cm</span>.<span class="pl-s1">coolwarm</span>)

<span class="pl-s1">boundaries</span> <span class="pl-c1">=</span> <span class="pl-s1">np</span>.<span class="pl-en">linspace</span>(<span class="pl-s1">np</span>.<span class="pl-en">min</span>(<span class="pl-s1">z</span>), <span class="pl-s1">np</span>.<span class="pl-en">max</span>(<span class="pl-s1">z</span>), <span class="pl-c1">10</span>, <span class="pl-s1">endpoint</span><span class="pl-c1">=</span><span class="pl-c1">True</span>)
<span class="pl-s1">fig</span>.<span class="pl-en">colorbar</span>(<span class="pl-s1">surf</span>, <span class="pl-s1">shrink</span><span class="pl-c1">=</span><span class="pl-c1">0.5</span>, <span class="pl-s1">boundaries</span><span class="pl-c1">=</span><span class="pl-s1">boundaries</span>, <span class="pl-s1">pad</span><span class="pl-c1">=</span><span class="pl-c1">0.1</span>)

<span class="pl-s1">plt</span>.<span class="pl-en">show</span>()</pre></div>
<h3 dir="auto">Actual outcome</h3>
<p dir="auto"><a target="_blank" rel="noopener noreferrer nofollow" href="https://user-images.githubusercontent.com/5299999/145533380-64fb0801-c30f-413e-8709-3bfe8ff202f5.png"><img src="https://user-images.githubusercontent.com/5299999/145533380-64fb0801-c30f-413e-8709-3bfe8ff202f5.png" alt="Figure_matplotlib==3 5 0" style="max-width: 100%;"></a></p>
<h3 dir="auto">Expected outcome</h3>
<p dir="auto"><a target="_blank" rel="noopener noreferrer nofollow" href="https://user-images.githubusercontent.com/5299999/145533365-2c0b8053-9789-46e8-a014-3da2d8ff3ddd.png"><img src="https://user-images.githubusercontent.com/5299999/145533365-2c0b8053-9789-46e8-a014-3da2d8ff3ddd.png" alt="Figure_matplotlib==3 4 3" style="max-width: 100%;"></a></p>
<h3 dir="auto">Additional information</h3>
<p dir="auto"><em>No response</em></p>
<h3 dir="auto">Operating system</h3>
<p dir="auto">Windows</p>
<h3 dir="auto">Matplotlib Version</h3>
<p dir="auto">3.5.0</p>
<h3 dir="auto">Matplotlib Backend</h3>
<p dir="auto">TkAgg</p>
<h3 dir="auto">Python version</h3>
<p dir="auto">Python 3.9.9, Python 3.10.1</p>
<h3 dir="auto">Jupyter version</h3>
<p dir="auto"><em>No response</em></p>
<h3 dir="auto">Installation</h3>
<p dir="auto">pip</p>