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

未關閉
#32,699 0 則留言 0 個 reaction 已指派 0 人 在 GitHub 檢視

維護者通常 1 天內回覆

還沒有人認領這個 Issue。

評估

難度
3/5
預估耗時
1-2 天
新手友好度
58/100
Issue 類型
缺陷
描述清晰度
基本清楚
活躍度
活躍
技術堆疊
matplotlib, python

研究方向

Start by reading matplotlib.sphinxext.plot_directive and trace how plot_formats DPI is applied when saving figures, especially for plots with and without a directive width. Reproduce the two examples in a Sphinx build using plot_formats = ['png'] and [('png', 200)]; done means changing DPI preserves the first figure's physical size while producing the expected higher pixel resolution.

由索引模型根據 Issue 內容生成。

描述

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.

主要語言
Python
星號
23.6k
分支
8.6k
平均合併
1 天 19 小時
30 天內合併 PR
64

環境準備

在 Codespaces 中開啟

在瀏覽器裡用你自己的 GitHub 帳號啟動這個專案的開發容器。

從這裡開始

  1. 先讀完整個 Issue,再讀專案的貢獻指南。
  2. 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
  3. Fork 儲存庫,在一個分支上完成修改。
  4. 送出 Pull Request,並在描述裡引用這個 Issue 編號。

matplotlib/matplotlib 的其他 Issue

查看 matplotlib/matplotlib 的全部 Issue

相似的 Issue

更多 Python Issue

把新 issue 寄到你的電子郵件信箱

精選適合新手參與的 GitHub issue 摘要。