Skip to content

DOC: Add more explanatory comments to gallery examples #11654

Closed

Description

@story645

@erinleeryan raised the concern that many of the matplotlib gallery examples have very little to no prose documentation, which can be frustrating for users unfamiliar with matplotlib conventions.
For example, in the stem plot example below it is unclear what markerline, stemlines, and baseline are relative the plot, and why plt.stem returns those three objects and what the plt.setp function is doing.

import matplotlib.pyplot as plt
import numpy as np

x = np.linspace(0.1, 2 * np.pi, 10)
markerline, stemlines, baseline = plt.stem(x, np.cos(x), '-.')
plt.setp(baseline, color='r', linewidth=2)

plt.show()

As suggested by @erinleeryan, it would be a welcome improvement to the docs to add short one line comments explaining what each line is doing, as shown in the pie chart example below:

# Pie chart, where the slices will be ordered and plotted counter-clockwise:
labels = 'Frogs', 'Hogs', 'Dogs', 'Logs'
sizes = [15, 30, 45, 10]
explode = (0, 0.1, 0, 0)  # only "explode" the 2nd slice (i.e. 'Hogs')

fig1, ax1 = plt.subplots()
ax1.pie(sizes, explode=explode, labels=labels, autopct='%1.1f%%',
        shadow=True, startangle=90)
ax1.axis('equal')  # Equal aspect ratio ensures that pie is drawn as a circle.

plt.show()

As an aside, we probably should also remove "simple" and "basic" from the titles of examples, as the gallery image will indicate the level of complexity of the example to the user.

Activity

  1. gabru-md commented on Jul 15, 2018

    @gabru-md
    Contributor

    Hello @story645,
    I would like to work on this issue. I can write the comments for the example files incase no one else is working on it. 🐱

  2. story645 commented on Jul 17, 2018

    @story645
    MemberAuthor

    Go for it! This issue is fairly broad, so supports multiple people working on it.

  3. timhoffm commented on Jul 17, 2018

    @timhoffm
    Member

    I think we should establish some guidelines what should be in examples and what not. Some rules that come to my mind (not necessarily polished or complete):

    • Describe the purpose of the example in 1-2 sentences at the top.
    • If suitable, add a section to describe what is done in more detail.
    • Don't describe functionality that's not the main topic of the example (e.g. don't describe the creation of the legend, if it's only in the example to make the generated plot more understandable).
    • Do not describe what's already in docstrings unless it's essential for the understanding of the example. You may link to the docstring.
    • The code itself should be kept as readable as possible.
    • Do not add long explanations as code comments. Instead, put the explanation before the code, or if really necessary, use multiple code blocks with text in between.
    • You may use some short explanatory comments within the code (a good example is the aspect comment in the pie chart example above). But do not clutter the code with too many comments.

    It would be helpful to give some reference examples, which follow the guidelines.

    Also, we should define the difference between an example and a tutorial. In some cases that's not quite clear to me.

  4. jklymak commented on Jul 17, 2018

    @jklymak
    Member

    Agree - note that we are also working w/ sphinx-gallery on links to the methods/functions sphinx-gallery/sphinx-gallery#347 which just got merged, so will take a while to release? Adding these back links puts the example in a list under the method (i.e.
    https://matplotlib.org/api/_as_gen/matplotlib.axes.Axes.plot.html#examples-using-matplotlib-axes-axes-plot) i.e. see the suggested syntax:
    #10974 (comment)

  5. story645 commented on Jul 17, 2018

    @story645
    MemberAuthor

    Don't describe functionality that's not the main topic of the example (e.g. don't describe the creation of the legend, if it's only in the example to make the generated plot more understandable).

    I go back and forth on this 'cause I do this a lot and get lots of complaints that it's confusing. I think a one liner comment can be very useful here.

  6. added this to the v3.0-doc milestone on Feb 20, 2019
  7. modified the milestones: v3.0-doc, v3.1-doc on Jul 3, 2019
  8. chrissshe commented on Feb 18, 2020

    @chrissshe
    Contributor

    I just created a PR to improve examples/widgets/textbox.py #16549

    This is my first contribution to matplotlib. Any feedback are welcome!

  9. tacaswell commented on Feb 18, 2020

    @tacaswell
    Member

    Thanks @chrissshe !

  10. modified the milestones: v3.1-doc, 3.2-doc on Mar 15, 2020
  11. removed this from the 3.2-doc milestone on Jul 17, 2020
  12. 4 remaining items

  13. added this to the v3.6.0 milestone on Sep 25, 2021
  14. modified the milestones: v3.6.0, v3.7.0 on Jul 8, 2022
  15. modified the milestones: v3.7.0, v3.7.1 on Feb 14, 2023
  16. modified the milestones: v3.7.1, v3.7-doc on Mar 4, 2023
  17. ggold7046 commented on Apr 7, 2023

    @ggold7046

    Hello,

    I would love to work on this issue if this issue is still open. Could someone guide me; from where to start ?

  18. ggold7046 commented on Apr 8, 2023

    @ggold7046

    @melissawm , can you help me with this please ? How to find some problems to work on ?

  19. rcomer commented on Apr 8, 2023

    @rcomer
    Member
  20. ggold7046 commented on Apr 9, 2023

    @ggold7046

    Hi @rcomer , thanks for the reply. I was not able to find any #11654 PR related issue which needs more explanatory comments to gallery examples. I thought this issue will be a good starting point for me.

  21. jklymak commented on Apr 9, 2023

    @jklymak
    Member

    @ggold7046 its not clear what you are asking for here. If you have a contribution, code it up and submit as a PR following the guide given above. We don't assign issues.

  22. ggold7046 commented on Apr 9, 2023

    @ggold7046
  23. story645 commented on Apr 9, 2023

    @story645
    MemberAuthor

    Hi @ggold7046, the aim of this issue was that the contributor looks through the gallery and finds which examples they feel need explanation. But, given how open ended this issue is, I'm going to close it since it will overlap with gsod work and hopefully a byproduct of that work will be more specific and targeted issues outlining what isn't working or how to improve specific gallery examples.

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

    Documentation🌱 Good first issueOpen a pull request against these issues if there are no active ones!

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions