Skip to content

[Doc] Added images of hatches to hatch API page #29644

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

Merged
merged 1 commit into from
Mar 4, 2025

Conversation

story645
Copy link
Member

@story645 story645 commented Feb 18, 2025

PR summary

Adds an image of the hatches to the hatch docs. closes #27196

  • 1st commit cleans up/implements requested changes from [Doc] Updated Hatches API page  #27370
  • 2nd commit adds a hatch mpl_type that points to the API page (down the line, this should probably be its own page like colors)

PR checklist

@story645 story645 mentioned this pull request Feb 18, 2025
2 tasks
@story645 story645 changed the title [Doc] Updated Hatches API page [Doc] Added images of hatches to hatch API page Feb 18, 2025
@timhoffm
Copy link
Member

timhoffm commented Feb 18, 2025

I believe the mpl_type link should point to the hatch style reference.

Amending the matplotlib.hatch API page is okish. I'd like to point out though that the hatch classes defined there cannot be used by the user. AFAICS the only public API to create hatches is through hatch patterns. In that sense matplotlib.hatch is an implementation detail, and we could even make it private. IMHO documentation on the API page should rather connect the visual to the classes, e.g.

grafik

import matplotlib.pyplot as plt
from matplotlib.patches import Rectangle

fig, ax = plt.subplots()

pattern_to_class = {
    '/': 'NorthEastHatch',
    '\\': 'SouthEastHatch',
    '|': 'VerticalHatch',
    '-': 'HorizontalHatch',
    '+': 'VerticalHatch + HorizontalHatch',
    'x': 'NorthEastHatch + SouthEastHatch',
    'o': 'SmallCircles',
    'O': 'LargeCircles',
    '.': 'SmallFilledCircles',
    '*': 'Stars',
}

for i, (hatch, classes) in enumerate(pattern_to_class.items()):
    ax.add_patch(Rectangle((0.1, i+0.5), 0.8, 0.8, fill=False, hatch=hatch*2))
    ax.text(1.3, i+0.9, f"'{hatch}'", family='monospace', va='center')
    ax.text(2, i+0.9, pattern_to_class[hatch], family='monospace', va='center', color='tab:blue')
ax.set(xlim=(0, 5), ylim=(0, i+1.5), yinverted=True)
ax.set_axis_off()

@story645
Copy link
Member Author

So like your table, gonna adopt that.

In that sense matplotlib.hatch is an implementation detail, and we could even make it private.

I think the reason not to is for helper classes for custom hatches #20690

@timhoffm
Copy link
Member

FYI: I've added a minor position tweak to the above code.

@story645
Copy link
Member Author

story645 commented Feb 18, 2025

Um, I just used annotate

 for i, (hatch, classes) in enumerate(pattern_to_class.items()):
        r = Rectangle((0.1, i+0.5), 0.8, 0.8, fill=False, hatch=hatch*2)
        ax.add_patch(r)
        h = ax.annotate(f"'{hatch}'", xy=(1.2, .5), xycoords=r,
                        family='monospace', va='center', ha='left')
        ax.annotate(pattern_to_class[hatch], xy=(1.5, .5), xycoords=h,
                    family='monospace', va='center', ha='left', color='tab:blue')

  ax.set(xlim=(0, 5), ylim=(0, i+1.5), yinverted=True)
  ax.set_axis_off()

image

@story645
Copy link
Member Author

Ok, I have a it's possibly documented somewhere question, but does the plot directive not work in the API docs? I pushed changes in the latest commit just in case I'm doing something silly.

@github-actions github-actions bot added the Documentation: examples files in galleries/examples label Feb 19, 2025
@timhoffm
Copy link
Member

Interesting. I don't think we have the plot directive in any other module docstring, but we have it in method docstrings and there it works, e.g. https://output.circle-artifacts.com/output/job/4778f46c-549d-46ab-8d66-29dd2131e7e8/artifacts/0/doc/build/html/api/_as_gen/matplotlib.axes.Axes.margins.html#matplotlib.axes.Axes.margins.

Just as a note Sphinx 8.2 was released yesterday. It could be that there is a regression, but again, the method docstring works (I've intentionally picked your doc build here in the link above.

@story645
Copy link
Member Author

So the answer is I was doing something silly - plot_formats is a config variable and not an option parameter 🤦‍♀️

@timhoffm
Copy link
Member

And that doesn't lead to an error or at least warning? AFAIR Sphinx built clearly.

@story645
Copy link
Member Author

And that doesn't lead to an error or at least warning? AFAIR Sphinx built clearly.

Nope 😭


.. plot:: _embedded_plots/hatch_classes.py
:include-source: false
:alt: 8 squares, each showing the pattern corresponding to the hatch symbol: symbol '/' makes right leaning diagonals, '\\' makes left leaning diagonals, '|' makes vertical lines, '-' makes horizontal lines, '+' makes a grid, 'X' makes a grid rotated 90 degrees, 'o' makes small unfilled circles, 'O' makes large unfilled circles, '.' makes small filled circles, and '*' makes a start with 5 points
Copy link
Member Author

Choose a reason for hiding this comment

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

Linting won't pass cause of the alt text but alt txt can't span multiple lines so the fix for this is probably the sdg that lets me stash this in the file

Copy link
Member

Choose a reason for hiding this comment

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

Are you sure? Docutils defines directive options as field lists and there they write


:Indentation: Since the field marker may be quite long, the second
   and subsequent lines of the field body do not have to line up
   with the first line, but they must be indented relative to the
   field name marker, and they must line up with each other.

I'd, assume this should be automatically handled by the docutils parser. If not we may check whether we can modify our plot directive.

Copy link
Member Author

Choose a reason for hiding this comment

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

I've tried every variant of indentation I could think of. Now I'm wondering if it's maybe getting regestered as code (it gets formatted weird, so let me try putting the alt text before other options

Copy link
Member Author

Choose a reason for hiding this comment

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

That also didn't work :/

Copy link
Member

Choose a reason for hiding this comment

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

We don't have any multiline plot-alt option. Though there is one figure-alt at

.. figure:: /_static/FigureNotebook.png
:alt: Image of figure generated in Jupyter Notebook with notebook
backend, including a toolbar.
:width: 400

You could try to break lines on existing plot directives in docstrings and rst and see how that goes.

Or just shorten the text to "A visualization of the 8 builtin hatch patterns", which is of course less helpful but IMHO not the end of the world. 🤷

Copy link
Member Author

Choose a reason for hiding this comment

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

At that point, I was thinking maybe put each hatch in its class docstring, but at that point I'm tilting into repr on hatchclasses which doesn't make sense unless we go the full registry route

Copy link
Member

@timhoffm timhoffm left a comment

Choose a reason for hiding this comment

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

Content is good to go. You can self-merge after finding a way to deal with the line length.

Co-authored-by: Md Ashhar <mdashhar01@gmail.com>
Co-authored-by: hannah <story645@gmail.com>
Co-authored-by: Tim Hoffmann <2836374+timhoffm@users.noreply.github.com>

added a hatch mpl_type that links to the hatch reference docs
@story645 story645 marked this pull request as ready for review March 4, 2025 04:09
@story645 story645 merged commit 52f659f into matplotlib:main Mar 4, 2025
39 of 41 checks passed
@QuLogic QuLogic added this to the v3.11.0 milestone Mar 5, 2025
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.

[Doc]: List supported hatches and link to/embed hatch reference on hatches API page
4 participants