Skip to content

[Bug]: matplotlib.sphinxext.plot_directive: Changing DPI value in plot_formats leads to unexpected figure sizes #32699

Backlog

Description

@DietBru

When using the plot:: directive in Sphinx (i.e., using matplotlib.sphinxext.plot_directive): Modifying the resolution via the configuration variable plot_formats in Sphinx's conf.py leads to unexpected sizes of the resulting figures:

Setting plot_formats = ['png'] produces:

image

The first image is made up 160x160 pixels and the second image is made up of 320x320 pixel.

Setting plot_formats = plot_formats = [('png', 200)] produces:

image

The first image is made up 400x400 pixels and the second image is made up of 800x800 pixel.

I expected that just the resolution and not the size of the first figure would change (analogous to the second figure).

The ReST source:

*************************
SphinxPlots documentation
*************************

A default matplotlib plot followed by a two inch wide SVG image:


.. plot::

     import matplotlib.pyplot as plt

     fig, ax = plt.subplots(facecolor='gray', figsize=(2, 2))
     fig.suptitle(f"Size: {fig.get_size_inches()} in")
     ax.plot([0, 1, 2, 3], [0, 1, -2, .5])
     plt.show()

.. image:: _static/2inBox.svg
   :alt: SphinxPlots logo
   :align: center
   :width: 2in


A 4 inch wide plot, which is parametrized by Sphinx to width of two inch width:

.. plot::
     :width: 2in

     import matplotlib.pyplot as plt

     fig, ax = plt.subplots(facecolor='gray', figsize=(4, 4))
     fig.suptitle(f"Size: {fig.get_size_inches()} in")
     ax.plot([0, 1, 2, 3], [0, 1, -2, .5])
     plt.show()

.. image:: _static/2inBox.svg
   :alt: SphinxPlots logo
   :align: center
   :width: 2in

Sphinx's configurtion conf.py:

extensions = [
    'matplotlib.sphinxext.plot_directive',
    'matplotlib.sphinxext.figmpl_directive',
]

templates_path = ['_templates']
exclude_patterns = ['_build', 'Thumbs.db', '.DS_Store']



# -- Options for HTML output -------------------------------------------------
# https://www.sphinx-doc.org/en/master/usage/configuration.html#options-for-html-output

html_theme = 'alabaster'
html_static_path = ['_static']

plot_include_source = False
plot_html_show_formats = True
plot_html_show_source_link = False

# plot_srcset = ['4x']
plot_formats = ['png']  # first case
# plot_formats = [('png', 200)]  # second case

Doing plot_formats = [('webp', 200)] leads to comparable results.

Activity

  1. susmita9878 commented on Dec 4, 2026

    @susmita9878

    Hi, I'd like to investigate this issue and work on a fix with a regression test. Is anyone currently working on it?

  2. susmita9878 commented on Dec 4, 2026

    @susmita9878

    I investigated this locally. The generated PNG dimensions scale with DPI, while the first plot has no explicit HTML width. I also tested Sphinx's standard figure directive and observed the same default behavior. An explicit :width: keeps the displayed size fixed. Is automatic physical-size preservation the intended behavior for plot_directive?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions