Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
97 commits
Select commit Hold shift + click to select a range
50cad2a
Backport PR #29584: DOC: Recommend constrained_layout over tight_layout
timhoffm Feb 28, 2025
c38e0f7
Merge pull request #29691 from meeseeksmachine/auto-backport-of-pr-29…
rcomer Feb 28, 2025
1bdc36a
Backport PR #29689: Fix alt and caption handling in Sphinx directives
timhoffm Feb 28, 2025
bf64054
Backport PR #29590: Blocked set_clim() callbacks to prevent inconsist…
prafulgulani Feb 28, 2025
365dd59
Merge pull request #29693 from meeseeksmachine/auto-backport-of-pr-29…
QuLogic Feb 28, 2025
b6f6e27
Merge pull request #29692 from meeseeksmachine/auto-backport-of-pr-29…
timhoffm Mar 1, 2025
7bbf261
Backport PR #29708: MNT: correct version in plotting method deprecati…
timhoffm Mar 6, 2025
a2ed2da
Merge pull request #29709 from meeseeksmachine/auto-backport-of-pr-29…
oscargus Mar 6, 2025
6843f81
Backport PR #29726: Add reference tag to Hatch style reference
story645 Mar 10, 2025
f51d428
Merge pull request #29727 from meeseeksmachine/auto-backport-of-pr-29…
rcomer Mar 10, 2025
f098eb2
Backport PR #29724: Fix SubplotSpec.get_gridspec type hint
timhoffm Mar 10, 2025
01d66d1
Merge pull request #29730 from meeseeksmachine/auto-backport-of-pr-29…
oscargus Mar 10, 2025
6bc61bb
Backport PR #29719: Fix passing singleton sequence-type styles to hist
story645 Mar 12, 2025
519b3e5
Merge pull request #29735 from meeseeksmachine/auto-backport-of-pr-29…
story645 Mar 12, 2025
3ac78ff
Backport PR #29734: ci: MacOS 14: temporarily upper-bound the 'PyGObj…
timhoffm Mar 12, 2025
5d62811
Merge pull request #29737 from meeseeksmachine/auto-backport-of-pr-29…
dstansby Mar 13, 2025
d0ab468
Backport PR #29748: Fix PyGObject version pinning in macOS tests
tacaswell Mar 13, 2025
5090b26
Backport PR #29721: FIX: pyplot auto-backend detection case-sensitivi…
ianthomas23 Mar 14, 2025
f86947c
Backport PR #29767: Add description to logit_demo.py script
QianZhang19 Mar 18, 2025
90a5aa9
Merge pull request #29768 from meeseeksmachine/auto-backport-of-pr-29…
dstansby Mar 18, 2025
8946085
Backport PR #29752: DOC: Add install instructions for pixi and uv
timhoffm Mar 18, 2025
d4820f3
Merge pull request #29771 from meeseeksmachine/auto-backport-of-pr-29…
timhoffm Mar 18, 2025
2ddb364
Backport PR #29781: Fix escaping of nulls and "0" in default filenames.
timhoffm Mar 20, 2025
2736c12
Merge pull request #29784 from meeseeksmachine/auto-backport-of-pr-29…
QuLogic Mar 20, 2025
ac6495f
Backport PR #29755: DOC: Simplify annotation arrow style reference
timhoffm Mar 21, 2025
005677f
Merge pull request #29786 from meeseeksmachine/auto-backport-of-pr-29…
timhoffm Mar 21, 2025
ba55b7c
Backport PR #29770: MNT: Move test for old ipython behavior to minver…
dstansby Mar 21, 2025
a56911e
Merge pull request #29754 from meeseeksmachine/auto-backport-of-pr-29…
dstansby Mar 21, 2025
57878e9
Merge pull request #29750 from meeseeksmachine/auto-backport-of-pr-29…
dstansby Mar 21, 2025
1e89088
Merge pull request #29792 from meeseeksmachine/auto-backport-of-pr-29…
QuLogic Mar 21, 2025
64ff907
Backport PR #29552: Bug Fix: Normalize kwargs for Histogram
TheSkyentist Feb 14, 2025
f5d6737
Merge pull request #29814 from meeseeksmachine/auto-backport-of-pr-29…
QuLogic Mar 27, 2025
0359016
Backport PR #29801: DOC: Slightly further improve arrowstyle demo
timhoffm Mar 27, 2025
30817f9
Merge pull request #29818 from meeseeksmachine/auto-backport-of-pr-29…
QuLogic Mar 28, 2025
5fde149
Backport PR #29839: Improve docs regarding plt.close().
timhoffm Apr 1, 2025
ba588a5
Merge pull request #29848 from meeseeksmachine/auto-backport-of-pr-29…
timhoffm Apr 1, 2025
b29bd15
Backport PR #29842: Don't drag draggables on scroll events
timhoffm Apr 2, 2025
282ef47
Merge pull request #29858 from meeseeksmachine/auto-backport-of-pr-29…
timhoffm Apr 2, 2025
00b4df2
Backport PR #29545: DOC: correctly specify return type of `figaspect`
QuLogic Apr 3, 2025
465d984
Merge pull request #29859 from meeseeksmachine/auto-backport-of-pr-29…
QuLogic Apr 3, 2025
a499643
Backport PR #29773: DOC: Improve interactive figures guide / Blocking…
timhoffm Apr 3, 2025
2230a0e
Merge pull request #29861 from meeseeksmachine/auto-backport-of-pr-29…
timhoffm Apr 3, 2025
479ed1e
Backport PR #29834: TST: pin flake8
jklymak Mar 30, 2025
4bf746b
Merge pull request #29868 from meeseeksmachine/auto-backport-of-pr-29…
QuLogic Apr 5, 2025
d8654f5
Backport PR #29853: Update lib/matplotlib/stackplot.py
timhoffm Apr 7, 2025
6cab025
Merge pull request #29880 from meeseeksmachine/auto-backport-of-pr-29…
timhoffm Apr 7, 2025
877ec29
Backport PR #29803: DOC: Improve FancyArrowPatch docstring
QuLogic Apr 9, 2025
6c394db
Merge pull request #29888 from meeseeksmachine/auto-backport-of-pr-29…
timhoffm Apr 9, 2025
7470a81
Backport PR #29896: Change `.T` to `.transpose()` in `_reshape_2D`
story645 Apr 10, 2025
02c0548
Backport PR #29899: [doc] minimally document what basic units is doing
timhoffm Apr 10, 2025
fe0140e
Merge pull request #29900 from meeseeksmachine/auto-backport-of-pr-29…
timhoffm Apr 10, 2025
34a11ab
Merge pull request #29902 from meeseeksmachine/auto-backport-of-pr-29…
QuLogic Apr 11, 2025
25b9377
Backport PR #29897: BUG: ensure that errorbar does not error on maske…
QuLogic Apr 16, 2025
88bd99c
Backport PR #29929: Correct rightparen typo
oscargus Apr 16, 2025
2673b93
Merge pull request #29930 from meeseeksmachine/auto-backport-of-pr-29…
QuLogic Apr 17, 2025
f72525a
Merge pull request #29927 from meeseeksmachine/auto-backport-of-pr-29…
QuLogic Apr 17, 2025
54d6615
Backport PR #29920: Allow `None` in set_prop_cycle (in type hints)
timhoffm Apr 17, 2025
a595375
Merge pull request #29932 from meeseeksmachine/auto-backport-of-pr-29…
timhoffm Apr 17, 2025
9a57241
Backport PR #29931: Allow Python native sequences in Matplotlib `imsa…
ksunden Apr 17, 2025
bdba952
Backport PR #29919: Handle MOVETO's, CLOSEPOLY's and empty paths in P…
timhoffm Apr 18, 2025
a1440fe
Fix broken links in interactive guide
dstansby Apr 18, 2025
6a6e8d8
Merge pull request #29940 from meeseeksmachine/auto-backport-of-pr-29…
oscargus Apr 18, 2025
09bea95
Merge pull request #29943 from dstansby/310-broken-links
tacaswell Apr 18, 2025
4cd3b06
STY: placate lint
tacaswell Apr 18, 2025
7dfb4e4
Merge pull request #29933 from meeseeksmachine/auto-backport-of-pr-29…
timhoffm Apr 20, 2025
d1c20ea
Backport PR #29872: TST: Use placeholders for text in layout tests
timhoffm Apr 10, 2025
110b6af
Merge pull request #29901 from meeseeksmachine/auto-backport-of-pr-29…
rcomer Apr 20, 2025
704c239
Backport PR #29796: ci: rotate soon-to-be-unsupported GitHub Actions …
timhoffm Apr 1, 2025
9a80304
Merge pull request #29949 from rcomer/auto-backport-of-pr-29796-on-v3…
QuLogic Apr 23, 2025
59a9774
Backport PR #29827: TST: Remove unnecessary test images
QuLogic Mar 29, 2025
a7f5ce6
Backport PR #29965: Document Axes.spines
jklymak Apr 25, 2025
8a1cea5
Merge pull request #29969 from meeseeksmachine/auto-backport-of-pr-29…
QuLogic Apr 26, 2025
0753e77
Backport PR #29970: TST: Make refcount tests more resilient to Python…
tacaswell Apr 28, 2025
4ee1e05
Backport PR #29979: Fix typos: horizonatal -> horizontal
rcomer Apr 28, 2025
9846a6f
Merge pull request #29980 from meeseeksmachine/auto-backport-of-pr-29…
timhoffm Apr 28, 2025
9e42559
Backport PR #29975: DOC: correct signature for animation update funct…
QuLogic Apr 28, 2025
950f2bc
Merge pull request #29974 from meeseeksmachine/auto-backport-of-pr-29…
dstansby Apr 28, 2025
495fb64
Merge pull request #29983 from meeseeksmachine/auto-backport-of-pr-29…
dstansby Apr 28, 2025
7dab28b
Backport PR #29370: DOC: Improve NonUniformImage docs
timhoffm Apr 29, 2025
2322a8f
Merge pull request #29987 from meeseeksmachine/auto-backport-of-pr-29…
rcomer Apr 29, 2025
2c0e150
Backport PR #29789: Improve layout of cheatsheets in sidebar
dstansby Apr 30, 2025
be0d1aa
Merge pull request #29990 from meeseeksmachine/auto-backport-of-pr-29…
dstansby Apr 30, 2025
f7b8d2d
Backport PR #29995: Fix typo: missing singlequote in unrecognized bac…
dstansby May 1, 2025
cd67b8e
Merge pull request #29996 from meeseeksmachine/auto-backport-of-pr-29…
rcomer May 1, 2025
05d104e
Backport PR #29997: BLD: Ensure meson.build has the right version of …
timhoffm May 2, 2025
a64d453
Merge pull request #29999 from QuLogic/auto-backport-of-pr-29997-on-v…
timhoffm May 2, 2025
95c87f2
Backport PR #29673: DOC: document the issues with overlaying new mpl …
ksunden May 2, 2025
9f40c83
Merge pull request #30002 from meeseeksmachine/auto-backport-of-pr-29…
rcomer May 2, 2025
0595366
Merge pull request #29867 from QuLogic/auto-backport-of-pr-29827-on-v…
ksunden May 2, 2025
0ef1165
Backport PR #29992 on v3.10.x: Update pinned oldest win image on azure
dstansby May 5, 2025
acb7361
Merge pull request #30010 from rcomer/auto-backport-of-pr-29992-on-v3…
dstansby May 5, 2025
3b50d5c
Backport PR #29907: Ensure text metric calculation always uses the te…
tacaswell Apr 13, 2025
0ef15b6
Merge pull request #30018 from meeseeksmachine/auto-backport-of-pr-29…
oscargus May 6, 2025
71e6946
REL prep 3.10.3
ksunden May 8, 2025
8b82729
REL: v3.10.3
ksunden May 8, 2025
e82b493
Bump from v3.10.3 tag
ksunden May 8, 2025
0b02f2d
Merge branch 'v3.10.x' into merge_up_3.10.3
ksunden May 9, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Backport PR #29689: Fix alt and caption handling in Sphinx directives
  • Loading branch information
timhoffm authored and meeseeksmachine committed Feb 28, 2025
commit 1bdc36aea428696118679f3d1d1a86180cddef5f
78 changes: 38 additions & 40 deletions lib/matplotlib/sphinxext/figmpl_directive.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,14 @@
See the *FigureMpl* documentation below.

"""
from docutils import nodes

from docutils.parsers.rst import directives
from docutils.parsers.rst.directives.images import Figure, Image

import os
from os.path import relpath
from pathlib import PurePath, Path
import shutil

from docutils import nodes
from docutils.parsers.rst import directives
from docutils.parsers.rst.directives.images import Figure, Image
from sphinx.errors import ExtensionError

import matplotlib
Expand Down Expand Up @@ -193,12 +191,13 @@ def visit_figmpl_html(self, node):
# make uri also be relative...
nm = PurePath(node['uri'][1:]).name
uri = f'{imagerel}/{rel}{nm}'
img_attrs = {'src': uri, 'alt': node['alt']}

# make srcset str. Need to change all the prefixes!
maxsrc = uri
srcsetst = ''
if srcset:
maxmult = -1
srcsetst = ''
for mult, src in srcset.items():
nm = PurePath(src[1:]).name
# ../../_images/plot_1_2_0x.png
Expand All @@ -214,44 +213,43 @@ def visit_figmpl_html(self, node):
maxsrc = path

# trim trailing comma and space...
srcsetst = srcsetst[:-2]
img_attrs['srcset'] = srcsetst[:-2]

alt = node['alt']
if node['class'] is not None:
classst = ' '.join(node['class'])
classst = f'class="{classst}"'

else:
classst = ''

stylers = ['width', 'height', 'scale']
stylest = ''
for style in stylers:
img_attrs['class'] = ' '.join(node['class'])
for style in ['width', 'height', 'scale']:
if node[style]:
stylest += f'{style}: {node[style]};'

figalign = node['align'] if node['align'] else 'center'

# <figure class="align-default" id="id1">
# <a class="reference internal image-reference" href="_images/index-1.2x.png">
# <img alt="_images/index-1.2x.png" src="_images/index-1.2x.png" style="width: 53%;" />
# </a>
# <figcaption>
# <p><span class="caption-text">Figure caption is here....</span>
# <a class="headerlink" href="#id1" title="Permalink to this image">#</a></p>
# </figcaption>
# </figure>
img_block = (f'<img src="{uri}" style="{stylest}" srcset="{srcsetst}" '
f'alt="{alt}" {classst}/>')
html_block = f'<figure class="align-{figalign}">\n'
html_block += f' <a class="reference internal image-reference" href="{maxsrc}">\n'
html_block += f' {img_block}\n </a>\n'
if 'style' not in img_attrs:
img_attrs['style'] = f'{style}: {node[style]};'
else:
img_attrs['style'] += f'{style}: {node[style]};'

# <figure class="align-default" id="id1">
# <a class="reference internal image-reference" href="_images/index-1.2x.png">
# <img alt="_images/index-1.2x.png"
# src="_images/index-1.2x.png" style="width: 53%;" />
# </a>
# <figcaption>
# <p><span class="caption-text">Figure caption is here....</span>
# <a class="headerlink" href="#id1" title="Permalink to this image">#</a></p>
# </figcaption>
# </figure>
self.body.append(
self.starttag(
node, 'figure',
CLASS=f'align-{node["align"]}' if node['align'] else 'align-center'))
self.body.append(
self.starttag(node, 'a', CLASS='reference internal image-reference',
href=maxsrc) +
self.emptytag(node, 'img', **img_attrs) +
'</a>\n')
if node['caption']:
html_block += ' <figcaption>\n'
html_block += f' <p><span class="caption-text">{node["caption"]}</span></p>\n'
html_block += ' </figcaption>\n'
html_block += '</figure>\n'
self.body.append(html_block)
self.body.append(self.starttag(node, 'figcaption'))
self.body.append(self.starttag(node, 'p'))
self.body.append(self.starttag(node, 'span', CLASS='caption-text'))
self.body.append(node['caption'])
self.body.append('</span></p></figcaption>\n')
self.body.append('</figure>\n')


def visit_figmpl_latex(self, node):
Expand Down
5 changes: 4 additions & 1 deletion lib/matplotlib/sphinxext/plot_directive.py
Original file line number Diff line number Diff line change
Expand Up @@ -876,7 +876,7 @@ def run(arguments, content, options, state_machine, state, lineno):

# Properly indent the caption
if caption and config.plot_srcset:
caption = f':caption: {caption}'
caption = ':caption: ' + caption.replace('\n', ' ')
elif caption:
caption = '\n' + '\n'.join(' ' + line.strip()
for line in caption.split('\n'))
Expand All @@ -896,6 +896,9 @@ def run(arguments, content, options, state_machine, state, lineno):
if nofigs:
images = []

if 'alt' in options:
options['alt'] = options['alt'].replace('\n', ' ')

opts = [
f':{key}: {val}' for key, val in options.items()
if key in ('alt', 'height', 'width', 'scale', 'align', 'class')]
Expand Down
19 changes: 11 additions & 8 deletions lib/matplotlib/tests/test_sphinxext.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,22 +75,25 @@ def plot_directive_file(num):
# Plot 13 shows close-figs in action
assert filecmp.cmp(range_4, plot_file(13))
# Plot 14 has included source
html_contents = (html_dir / 'some_plots.html').read_bytes()
html_contents = (html_dir / 'some_plots.html').read_text(encoding='utf-8')

assert b'# Only a comment' in html_contents
assert '# Only a comment' in html_contents
# check plot defined in external file.
assert filecmp.cmp(range_4, img_dir / 'range4.png')
assert filecmp.cmp(range_6, img_dir / 'range6_range6.png')
# check if figure caption made it into html file
assert b'This is the caption for plot 15.' in html_contents
# check if figure caption using :caption: made it into html file
assert b'Plot 17 uses the caption option.' in html_contents
assert 'This is the caption for plot 15.' in html_contents
# check if figure caption using :caption: made it into html file (because this plot
# doesn't use srcset, the caption preserves newlines in the output.)
assert 'Plot 17 uses the caption option,\nwith multi-line input.' in html_contents
# check if figure alt text using :alt: made it into html file
assert 'Plot 17 uses the alt option, with multi-line input.' in html_contents
# check if figure caption made it into html file
assert b'This is the caption for plot 18.' in html_contents
assert 'This is the caption for plot 18.' in html_contents
# check if the custom classes made it into the html file
assert b'plot-directive my-class my-other-class' in html_contents
assert 'plot-directive my-class my-other-class' in html_contents
# check that the multi-image caption is applied twice
assert html_contents.count(b'This caption applies to both plots.') == 2
assert html_contents.count('This caption applies to both plots.') == 2
# Plot 21 is range(6) plot via an include directive. But because some of
# the previous plots are repeated, the argument to plot_file() is only 17.
assert filecmp.cmp(range_6, plot_file(17))
Expand Down
7 changes: 6 additions & 1 deletion lib/matplotlib/tests/tinypages/some_plots.rst
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,12 @@ Plot 16 uses a specific function in a file with plot commands:
Plot 17 gets a caption specified by the :caption: option:

.. plot::
:caption: Plot 17 uses the caption option.
:caption:
Plot 17 uses the caption option,
with multi-line input.
:alt:
Plot 17 uses the alt option,
with multi-line input.

plt.figure()
plt.plot(range(6))
Expand Down