Issue 1:
Bug report

Bug summary

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

Code for reproduction

Repository layout:

docs

source

pyplots

test.py

conf.py

index.rst

package

Content of file test.py

import matplotlib.pyplot as plt

def generate_plot_according_to_step(step):

plt.figure()

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

if step >= 1:
plt.title("Adding a title")

if step >= 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)

Content of conf.py :

extensions = [
'sphinx.ext.autodoc',
'matplotlib.sphinxext.plot_directive'
]

Then when referencing the steps functions in docstring anywhere in the package, such as:

"""
.. plot:: pyplots/test.py step0

.. plot:: pyplots/test.py step1

.. plot:: pyplots/test.py step2

"""

Actual outcome

When building the doc through the following steps:

cd .\docs

sphinx-quickstart

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

.\make.bat html

The following message arise

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 "<string>", line 1, in <module>
NameError: name 'step0' is not defined

Expected outcome

It seems that it is not possible to use a generical function such as the one presented to generate documentation.

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

Matplotlib version

Operating system: Windows 10

Matplotlib version (import matplotlib; print(matplotlib.__version__)): 3.4.2

Matplotlib backend (print(matplotlib.get_backend())): TkAgg

Python version: 3.9.5

Other libraries: Sphinx 4.0.2

Every library has been installed through pip

Issue 2:
Bug summary

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.

Code for reproduction

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={"projection": "3d"})

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()

Actual outcome

Expected outcome

Additional information

No response

Operating system

Windows

Matplotlib Version

3.5.0

Matplotlib Backend

TkAgg

Python version

Python 3.9.9, Python 3.10.1

Jupyter version

No response

Installation

pip