Skip to content

PDF backend can now do alpha on hatches. #17049

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

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

mnmelo
Copy link
Contributor

@mnmelo mnmelo commented Apr 6, 2020

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
hatching_ 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
hatching_ 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

@QuLogic
Copy link
Member

QuLogic commented Apr 7, 2020

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

@mnmelo
Copy link
Contributor Author

mnmelo commented Apr 7, 2020

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
Copy link
Contributor Author

mnmelo commented Apr 8, 2020

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
fill_black_with_alpha-expected_svg
Now
fill_black_with_alpha_svg

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 mnmelo force-pushed the pdf_hatch_alpha branch from 7311274 to 1b7fca1 Compare April 8, 2020 02:08
@tacaswell tacaswell requested a review from jkseppan April 8, 2020 03:04
@tacaswell
Copy link
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
Copy link
Contributor Author

mnmelo commented Apr 8, 2020

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
Copy link
Member

QuLogic commented Apr 8, 2020

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
Copy link
Contributor Author

mnmelo commented Apr 8, 2020

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 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
Member

Choose a reason for hiding this comment

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

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
Contributor Author

Choose a reason for hiding this comment

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

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.

Copy link
Member

@jkseppan jkseppan left a comment

Choose a reason for hiding this comment

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

Looks good to me!

@mnmelo
Copy link
Contributor Author

mnmelo commented Apr 8, 2020

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

@QuLogic
Copy link
Member

QuLogic commented Apr 18, 2020

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
Copy link
Contributor Author

mnmelo commented Apr 20, 2020

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 QuLogic modified the milestones: v3.3.0, v3.4.0 Apr 30, 2020
@jklymak jklymak marked this pull request as draft July 23, 2020 03:19
@QuLogic QuLogic modified the milestones: v3.4.0, v3.5.0 Jan 21, 2021
@tacaswell tacaswell modified the milestones: v3.5.0, v3.6.0 Aug 5, 2021
@timhoffm timhoffm modified the milestones: v3.6.0, unassigned Apr 30, 2022
@story645 story645 modified the milestones: unassigned, needs sorting Oct 6, 2022
@oscargus
Copy link
Member

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

@github-actions
Copy link

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 github-actions bot added the status: inactive Marked by the “Stale” Github Action label Jul 21, 2023
@github-actions github-actions bot removed the status: inactive Marked by the “Stale” Github Action label Jan 9, 2024
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