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

Hatch alpha not preserved in SVG or PDF · Issue #16883 · matplotlib/matplotlib · GitHub

Repository navigation

Hatch alpha not preserved in SVG or PDF #16883

Open

Description

Bug report

Bug summary

A hatched patch, with hatch transparency, renders fine with the inline or PNG backends, but alpha is ignored by the SVG or PDF backends. Did not test other backends.

A possible fix for SVG is simple, and included below. I can open a PR with it.

This is separate from #8431 or #9894.

Code for reproduction

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

plt.rcParams['hatch.linewidth'] = 9
patch = Rectangle(xy=(.1,.1), width=.5, height=.3,
                  facecolor="red",
                  edgecolor=(0.,0.,0.,.4),
                  hatch='/',
                  lw=4)
fig, ax = plt.subplots()
ax.add_patch(patch)
ax.set_title("SVG")
plt.savefig('test.svg')
ax.set_title("PDF")
plt.savefig('test.pdf')
ax.set_title("PNG")
plt.savefig('test.png')

# for uploading svg and pdf output was converted to png with inkscape:
# inkscape -z -e testsvg.png test.svg -d 72
# inkscape -z -e testpdf.png test.pdf -d 72
# and concatenated with imagemagick:
# montage testsvg.png testpdf.png test.png -mode concatenate -tile 1x3 out.png

Actual outcome

In addition, rendering of the PDF output is inconsistent across viewers (possibly due to #3841 and further inconsistency described in #14639). Inkscape views/converts nicely (as shown) but imagemagick doesn't even render the edge opacity. Firefox also views correctly. Adobe Reader DC behaves similar to imagemagick.

Expected outcome

SVG and PDF output should look like the PNG one.

FIX

I took a look at the generated SVG. The hatch description has the following style (note the lack of stroke-opacity):

style="fill:#000000;stroke:#000000;stroke-linecap:butt;stroke-linejoin:miter;stroke-width:9.0;"

The problem lies in the _write_hatches method, in backend_svg.py, which never sets any stroke-opacity. A fix for SVG is as simple as adding 'stroke-opacity': str(stroke[3]) to the dict passed to the generate_css call.

Matplotlib version

  • Operating system: Ubuntu 18.04
  • Matplotlib version: 3.1.2
  • Matplotlib backend: SVG/PDF/PNG
  • Python version: 3.6.8

Installed via pip.

Activity

  1. added this to the v3.3.0 milestone on Mar 23, 2020
  2. tacaswell commented on Mar 23, 2020

    Member

    Thank you for tracking this down!

    Could you please open a PR with that change to the svg backend?

  3. added a commit that references this issue on Mar 23, 2020
  4. mnmelo commented on Mar 23, 2020

    ContributorAuthor

    Done. Let's see what CI says.

    For the PDF backend, however, I can't really contribute. Took a look at the code and it seems to be solvable in the writeHatches method of backend_pdf.py. But I know nothing of PDF and couldn't find an obvious point to include the alpha.

    Further checking other backends: cairo doesn't even draw the hatching. Agg draws it but also without alpha.

  5. modified the milestones: v3.3.0, v3.4.0 on May 7, 2020
  6. modified the milestones: v3.4.0, v3.5.0 on Jan 27, 2021
  7. modified the milestones: v3.5.0, v3.6.0 on Sep 25, 2021
  8. modified the milestones: v3.6.0, v3.7.0 on Aug 24, 2022
  9. cvanelteren commented on Jan 8, 2026

    Contributor

    Any movement on this -- it's been close to 6 years now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions


      Back | FazBrowse Home | New Git URL