Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Give plot_directive output a max-width: 100% #19063

Merged
merged 1 commit into from Jan 4, 2021

Conversation

brunobeltran
Copy link
Contributor

@brunobeltran brunobeltran commented Dec 4, 2020

PR Summary

Currently, non-sphinx-gallery plots in the documentation (for example, from a .. plot:: directive in a docstring) produce plots that do not scale with screen size. This means that on e.g. phones, the plots get chopped off, as seen here:

overfull-example

Other Sphinx extensions (gallery, the inheritance diagram extension, etc) add their own custom CSS to make the images fit in the page. I copied the approach taken by sphinx.ext.graphviz to get the following result:

fixed-overfull-example

This became increasingly important for my GSOD work (e.g. #18544, where the API docs center around the inclusion of a "demo" figure showing the various options).

Edit: additionally, I had to fix an existing bug in plot_directive.TEMPLATE that caused Sphinx to warn (failing our tests) whenever there is both a caption and a custom class assigned to a figure.

PR Checklist

  • Has pytest style unit tests (and pytest passes).
  • Is Flake 8 compliant (run flake8 on changed files to check).
  • [N/A] New features are documented, with examples if plot related.
  • Documentation is sphinx and numpydoc compliant (the docs should build without error).
  • Conforms to Matplotlib style conventions (install flake8-docstrings and run flake8 --docstring-convention=all).
  • [N/A] New features have an entry in doc/users/next_whats_new/ (follow instructions in README.rst there).
  • [N/A] API changes documented in doc/api/next_api_changes/ (follow instructions in README.rst there).

@@ -254,6 +254,13 @@ def run(self):
raise self.error(str(e))


def _copy_css_file(app, exc):
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.


{{ caption }}
{%- endif %}
{{ caption }} {# appropriate leading whitespace added beforehand #}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Stripping trailing whitespace here causes the extra whitespace from before the {% if it matches to be effectively prepended to the caption, causing a bug.

Stripping that earlier whitespace using {% - if doesn't work because then Jinja strips the newline separating the options from the content.

So I settled for this ugly solution which puts "{{ caption }}" up against the side.

The other option would be to remove the artificially-inserted leading whitespace from the first line of the "caption" (L758), but this would technically be changing API for users who already have made custom templates for use the with plot_directive.

*
* Stylesheet controlling images created using the `plot` directive within
* Sphinx.
*
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sphinx includes some copyright/license stuff here, presumably since this file will be redistributed independently of the repository. I figure I don't mind my whole three lines of very advanced CSS becoming public domain upon distribution, but wanted to flag this in case there are legal ramifications I don't understand.

https://github.com/sphinx-doc/sphinx/blob/3.x/sphinx/templates/graphviz/graphviz.css#L7

scenario:

.. plot::
:caption: This caption applies to both plots.
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This caption is repeated verbatim after each plot with my changes. This may not be what I would have expected to happen, but it is the existing behavior.

@tacaswell tacaswell added this to the v3.4.0 milestone Jan 4, 2021
@timhoffm timhoffm merged commit d45dcef into matplotlib:master Jan 4, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

7 participants