FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

PDF backend can now do alpha on hatches. by mnmelo · Pull Request #17049 · matplotlib/matplotlib · GitHub

Repository navigation

PDF backend can now do alpha on hatches. - #17049

Draft
mnmelo wants to merge 1 commit into
matplotlib:mainfrom
mnmelo:pdf_hatch_alpha
Draft

mnmelo wants to merge 1 commit into
matplotlib:mainfrom
mnmelo:pdf_hatch_alpha

Conversation

mnmelo commented Apr 6, 2020

Copy link
Copy Markdown
Contributor

PR Summary

Studied a bit of .PDF and I got this working. Turns out the transparency graphics states (ExtGState) weren't being taught to the hatch pattern writing routine.

This completes the fix of hatch alpha on SVG and PDF (#16883).

Furthermore, I noticed, after the previous PR (#16890), that there was a bit of mixed behavior between 2 cases: 1. when a Patch edgecolor (and therefore its hatch_color) has alpha; or 2. when the entire Patch has alpha. Case 1 is what I had tried to fix in #16890; however, the test I was relying on was of case 2.

Case 2 is more logically and elegantly rendered by doing edge+hatch full opacity and then within an object with alpha. Digging a bit deeper I found I could implement this case 2 behavior by extending the forced_alpha concept to the SVG and PDF renderers: if the parent Patch or Collection has a global alpha, ignore the ones of the elements.

I updated two test_artist.py tests to better cover these cases (no added baselines images). The following images showcase the results of the SVG and PDF hatching test, after conversion to PNG. The rightmost, orange-tinted shapes are the ones where alpha was set; on the top diamond only on the edgecolor, on the bottom one on the entire Rectangle:

SVG

Notice in the top-right shapes how the edge and face/hatch overlap. This no longer occurs if the alpha is set on the entire object (but then the fill is also affected).

PDF

Notice again in the top-right shape that PDF is cleaner in handling the edge overlap with the face and hatch of the same Patch. It's rendered as a group that is internally opaque (the edge completely occludes the fill and hatch) but still transparent at the edges (I guess this is called a knockout group? I didn't add anything specific to this end). I'm not sure SVG could allow for this but would be cool/cleaner if it did.

Feedback appreciated.

PR Checklist

  • Has Pytest style unit tests
  • Code is Flake 8 compliant
  • New features are documented, with examples if plot related
  • Documentation is sphinx and numpydoc compliant
  • Added an entry to doc/users/next_whats_new/ if major new feature (follow instructions in README.rst there)
  • Documented in doc/api/api_changes.rst if API changed in a backward-incompatible way

Comment thread lib/matplotlib/tests/test_artist.py Outdated

QuLogic commented Apr 7, 2020

Copy link
Copy Markdown
Member

If the .png changed too (and is intended!), then that needs a what's new as well.

mnmelo commented Apr 7, 2020

Copy link
Copy Markdown
Contributor Author

The .png was already behaving as intended (see #16883).

I'll update the PR soon with a fix of all the images that got affected. As I can tell at a glance, they're the ones involving PDF edge opacity, which apparently wasn't being handled properly (but perhaps I overlooked something).

mnmelo commented Apr 8, 2020

Copy link
Copy Markdown
Contributor Author

Tests should now pass.

Fixed a bug with forced_alpha I had introduced in the PDF backend. Besides that, checked and updated the four remaining failing images. These were:

test_axes.py::test_contour_hatching, test_backend_svg.py::fill_black_with_alpha and test_backend_svg.py::patch_alpha_override: These are all SVG where the previously wrong whole-patch alpha behavior is evident: by being applied to edge and fill separately their overlap becomes more opaque and yields a visible outline. This is now corrected as an outline-less patch. Example of fill_black_with_alpha:

Before

Now

And finally a PNG test_mplot3d.py::poly3dcollection_alpha now reflects the consolidated application of alpha to collections (more correctly, in my opinion, as edges were being rendered fully opaque).

mnmelo force-pushed the pdf_hatch_alpha branch from 7311274 to 1b7fca1 Compare April 8, 2020 02:08
tacaswell requested a review from jkseppan April 8, 2020 03:04

Copy link
Copy Markdown
Member

Thanks for this, it is really good work! I am nominally 👍 on it going in, but would like to give @jkseppan a chance to weigh in (as the primary author of the pdf backend).

I am not sure that I understand why you added the extra box to the clipping though.

mnmelo commented Apr 8, 2020

Copy link
Copy Markdown
Contributor Author

Why I added a backdrop rectangle to the clipping test? I first did it debug-wise to be sure there was alpha on the fill. Then thought it was also good for the clipping test itself, to check that it really is a clip, not a white overlay, so I let it stay. But I have no strong feeling either way; shall I remove it?

I still have to fix some linting, and apparently the Travis build is failing on macOS at the bbox_inches_tight_raster test... Any hints as to why? The macOS Azure builds succeeded.

The Windows Azure builds are also failing, because of PIL.UnidentifiedImageError: cannot identify image file 'D:\\a\\1\\s\\result_images\\test_compare_images\\basn3p02-expected.png'. This doesn't seem related to my changes.

Finally, what is your policy on squashing these final small-fix commits?

QuLogic commented Apr 8, 2020

Copy link
Copy Markdown
Member

The macOS failure is a known issue.

Squashing is up to the contributor, though it's preferred if you've changed test images multiple times.

mnmelo commented Apr 8, 2020

Copy link
Copy Markdown
Contributor Author

Good to know about the Mac failures. As to the Windows failures, I don't suppose there's a way of pulling the failing images out of the CI VM. I don't work on WIndows...

More consistent behavior for edge/fill/hatch alpha vs. entire patch alpha,
for both PDF and SVG.

PathCollections now respect whole object alpha.
mnmelo force-pushed the pdf_hatch_alpha branch from eb1427b to 83d2017 Compare April 8, 2020 05:08

The SVG and PDF backends now respect the hatch stroke alpha. Useful applications
are, among others, semi-transparent hatches as a subtle way to differentiate
columns in bar plots.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

This could be a nice demo in the gallery, or perhaps another subplot in https://matplotlib.org/3.2.0/gallery/lines_bars_and_markers/filled_step.html

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

Cool idea. As a side note, because hatch color/alpha is linked to the Patch's edge's, a simple plot with hatch alpha will usually not look so good because the translucent edge will half-overlap with the fill. One can plot edge-less with lw=0, but to have an opaque outline one must replot a full opacity, edge-only plot on top of the first.
It's a bit hackish. An independent hatch style API (#7059) would go a long way to simplifying this.

jkseppan left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

Looks good to me!

mnmelo commented Apr 8, 2020

Copy link
Copy Markdown
Contributor Author

Ok, I'll be splitting the collection alpha into a separate PR, and try to add/update a gallery example as suggested.

QuLogic commented Apr 18, 2020

Copy link
Copy Markdown
Member

Is this waiting for the separate PR, or vice versa?

I wonder if #10035 is a similar sort of bug for markers, as the Patch edgecolor handling.

mnmelo commented Apr 20, 2020

Copy link
Copy Markdown
Contributor Author

Both; this should not be merged just yet. I haven't had the time to spin-off a collections-targeted PR.

As to #10035, this might very well be connected. I'll take a look. After a quick read it seems SVG supports 'knock-out' or is going to support it (not sure about the stability of that). This would further homogeneize SVG and PDF behavior. A discussion then would be on what is the intended way of displaying non-opaque edges over their fills (I vote for the cleaner-looking knockout compositing).

QuLogic modified the milestones: v3.3.0, v3.4.0 Apr 30, 2020
jklymak marked this pull request as draft July 23, 2020 03:19
QuLogic modified the milestones: v3.4.0, v3.5.0 Jan 21, 2021
tacaswell modified the milestones: v3.5.0, v3.6.0 Aug 5, 2021
timhoffm modified the milestones: v3.6.0, unassigned Apr 30, 2022
story645 modified the milestones: unassigned, needs sorting Oct 6, 2022

Copy link
Copy Markdown
Member

When this is continued, maybe this issue can be considered as well? #12367 (Seems like it is most likely fixed by this PR.)

Copy link
Copy Markdown

Since this Pull Request has not been updated in 60 days, it has been marked "inactive." This does not mean that it will be closed, though it may be moved to a "Draft" state. This helps maintainers prioritize their reviewing efforts. You can pick the PR back up anytime - please ping us if you need a review or guidance to move the PR forward! If you do not plan on continuing the work, please let us know so that we can either find someone to take the PR over, or close it.

github-actions Bot added the status: inactive Marked by the “Stale” Github Action label Jul 21, 2023
github-actions Bot removed the status: inactive Marked by the “Stale” Github Action label Jan 9, 2024
r3kste mentioned this pull request Jan 3, 2025
9 tasks done

This branch has not been deployed

No deployments
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
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.

8 participants


Back | FazBrowse Home | New Git URL