Skip to content

referencing a plot_directive plot does not work #9346

Closed as not planned
Closed as not planned

Description

@quantenschaum

Bug report

Bug summary

A figure created with plot_directive and preceeding label cannot be referenced with :ref: or :numref:.

Code for reproduction

.. _bar:

Testplot
--------

.. _foo:

.. plot::

   import matplotlib.pyplot as plt
   import numpy as np
   plt.hist(np.random.randn(1000), 20)

reference to plot: :ref:`foo` / :numref:`foo`
	
reference to section: :ref:`bar` / :numref:`bar`

Actual outcome

rendered HTML

bildschirmfoto_2017-10-10_18-42-44

LaTeX code

\subsection{Testplot}
\label{\detokenize{M01/index:testplot}}\label{\detokenize{M01/index:bar}}\phantomsection\label{\detokenize{M01/index:foo}}
\begin{figure}[H]
\centering

\noindent\sphinxincludegraphics{{index-1}.pdf}
\end{figure}

reference to plot: \DUrole{xref,std,std-ref}{foo} / \sphinxcode{foo}

reference to section: {\hyperref[\detokenize{M01/index:bar}]{\sphinxcrossref{\DUrole{std,std-ref}{Testplot}}}} / \hyperref[\detokenize{M01/index:bar}]{Section \ref{\detokenize{M01/index:bar}}}

Expected outcome

The foo references should be links like the bar references.

Matplotlib version

  • Operating system: Linux 4.10.0-35-generic 39~16.04.1-Ubuntu 16.04.3 LTS SMP x86_64 GNU/Linux
  • Matplotlib version: 2.1.0 via pip
  • Python version: 3.5.2
  • Other libraries: Sphinx (sphinx-build) 1.6.4

Activity

  1. quantenschaum commented on Oct 10, 2017

    @quantenschaum
    Author
    1. The plot directive should support the :name: option to add a named label.
    2. Especially for inline plot code there should be a :caption: option
  2. quantenschaum commented on Oct 10, 2017

    @quantenschaum
    Author

    :numref: works with the following template in conf.py where the only directives have been removed.

    plot_template = """
    {{ source_code }}
    
    {% for img in images %}
    .. figure:: {{ build_dir }}/{{ img.basename }}.*
       {% for option in options -%}
       {{ option }}
       {% endfor %}
    
       {{ caption }}
    {% endfor %}
    """
  3. quantenschaum commented on Oct 10, 2017

    @quantenschaum
    Author

    Adding :caption: is easy.
    In setup() add 'caption': directives.unchanged to the options. And in run() add if 'caption' in options: caption=options['caption'] before format_template().

  4. anntzer commented on Oct 10, 2017

    @anntzer
    Contributor

    Feel free to open a PR (preferably with a test -- sphinx has been having many backcompat issues in recent versions so we should try to make sure things stay in a working state...) :-)

  5. quantenschaum commented on Oct 10, 2017

    @quantenschaum
    Author

    When I try to add 'name' to the options in setup(), I get

    TypeError: add_directive() got multiple values for argument 'name'
    

    So it seems, as if there was already a name option, but supplying :name: does not work either.

    (ERROR/3) Error in "plot" directive: unknown option: "name".
    
  6. added this to the milestone on Oct 11, 2017
  7. tacaswell commented on Oct 11, 2017

    @tacaswell
    Member

    Is this documented to work and does not or would you like this to work as described?

  8. quantenschaum commented on Oct 11, 2017

    @quantenschaum
    Author

    Referencing plots is not explicitly mentioned in the docs, but since the plot_directive generates a figure, which can be labelled by putting a label in front of it, I would expect the plot to catch the label and the ref to be working. There is even code for moving

    the reference from the "htmlonly" (or "latexonly") code to the actual figure node itself

    but it seems to fail.

  9. quantenschaum commented on Oct 11, 2017

    @quantenschaum
    Author

    The culprit is mark_plot_labels. It looks for tagnames html_only and latex_only, but they are actually named only. So it was never actually doing anything.

    After fixing this, it fails, because there is no document.settings.env.labels. Where is it now?

  10. quantenschaum commented on Oct 11, 2017

    @quantenschaum
    Author

    I modified the code such that it adds the :name: option to the figure in the template. I printed the result to console. The :name: is there as expected, but the ref does not work. If I paste the code, generated by the template engine, into the actual source, the ref is working.

    Why does a label inside the only directive generated by the template engine not work?

  11. tushuhei commented on Apr 28, 2021

    @tushuhei

    I believe the root causation is how the only directive handles references, as pointed out at sphinx-doc/sphinx#2150.
    If we add :name: along with other options, it will be rendered twice (one within only:: html and the other within only:: not html), which will cause the Duplicate explicit target name warning eventually.

  12. eudoxos commented on May 21, 2022

    @eudoxos

    Anyone has a workaround for this, besides the hack in sfstoolbox/theory#5 ?

  13. github-actions commented on May 23, 2023

    @github-actions

    This issue has been marked "inactive" because it has been 365 days since the last comment. If this issue is still present in recent Matplotlib releases, or the feature request is still wanted, please leave a comment and this label will be removed. If there are no updates in another 30 days, this issue will be automatically closed, but you are free to re-open or create a new issue if needed. We value issue reports, and this procedure is meant to help us resurface and prioritize issues that have not been addressed yet, not make them disappear. Thanks for your help!

  14. added
    status: closed as inactiveIssues closed by the "Stale" Github Action. Please comment on any you think should still be open.
    on Jun 23, 2023
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

    status: closed as inactiveIssues closed by the "Stale" Github Action. Please comment on any you think should still be open.status: inactiveMarked by the “Stale” Github Actiontopic: sphinx extension

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions