-
-
Notifications
You must be signed in to change notification settings - Fork 7.9k
hatch_demo.py does not correctly save all the hatches in pdf #12367
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
Comments
This is strange because matplotlib/lib/matplotlib/tests/test_simplification.py Lines 221 to 226 in 7bcf618
The only difference is the example sets hatching after the object is created. But modifying the test to do the same still passes. Perhaps something to do with the |
Hi, I have just realized that the embedded pdf visualizer in my web browser show me the pdf correctly.
I have done another test, I noticed that the generated pdf file is a "PDF-1.4" file. For convenience I attach the "PDF-1.5" version here: hatch_demo-PDF-1.5.pdf |
Adobe Acrobat: No hatching at all SumatraPDF: No hatching + some white lines Strikingly, the shared hatch_demo-PDF-1.5.pdf looks perfect in all tested pdf viewers and browsers (Firefox, Opera, Vivaldi, Foxit Reader, Acrobat, pdfSam, SumatraPDF). However, I cannot reproduce the conversion. Trying to convert via ghostscript9.16 as Also using some online tool (https://docupub.de/pdfconvert) produces undesired output. @ftesser Can you share how exactly you did the conversion? |
Curiously, I get similar result with mplcairo: chromium shows the hatches, mupdf and zathura don't, and okular only shows the round ones (second from the left). |
@ImportanceOfBeingErnest I did the conversion like this: |
I see, that means that it's not enough to just change the version, but to convert it to svg first and then save to pdf again. (Did you try with "restrict to version 1.4"? It might just work as well.) |
@ImportanceOfBeingErnest: I confirm, the same inkscape procedure but with "restrict to PDF version" 1.4 works as well. |
Good to know that it doesn't depend on the pdf version. It means that this is purely a problem of the pdf backend. For a workaround one can indeed use inkscape as
which requires inkscape to live in the Path. |
use inkscape workaround: matplotlib/matplotlib#12367
Bump. I'm having a similar issue here: hatching is not exported to PDF. |
Here is what I see when I open the following files in Chrome (105.0.5195.125 on arm64 macOS):
|
I dig into this a bit more and found that the alpha value is indeed the problem. The following code snippet generates two images import matplotlib.pyplot as plt
from matplotlib.patches import Rectangle
rectangles = {
"default" : Rectangle((0, 0), 1, 1, hatch="x"),
"alpha1.0": Rectangle((0, 0), 1, 1, hatch="x", alpha=1.0),
# "alpha0.5":Rectangle((0, 0), 1, 1, hatch="x", alpha=0.5),
# "no-fill": Rectangle((0, 0), 1, 1, hatch="x", fill=False),
}
for name, rect in rectangles.items():
fig, ax = plt.subplots()
ax.set_axis_off()
fig.set_tight_layout(True)
ax.add_patch(rect)
fig.savefig(name + ".pdf", bbox_inches='tight', pad_inches=0)
fig.savefig(name + ".png", bbox_inches='tight', pad_inches=0) By diffing the generated PDF files. There is only a single line of difference: diff <(xxd alpha1.0.pdf) <(xxd default.pdf)
41c41
< 00000280: 7874 4753 7461 7465 202f 4341 2031 202f xtGState /CA 1 /
---
> 00000280: 7874 4753 7461 7465 202f 4341 2030 202f xtGState /CA 0 / One can also see this using a PDF parser: from PyPDF2 import PdfReader
for name in rectangles.keys():
reader = PdfReader(name + ".pdf")
print(reader.pages[0]["/Resources"]["/ExtGState"])
# output
{'/A1': {'/Type': '/ExtGState', '/CA': 0, '/ca': 1}}
{'/A1': {'/Type': '/ExtGState', '/CA': 1, '/ca': 1}} The reference from http://res.icesoft.org/docs/icepdf/v4_4_0/core/org/icepdf/core/pobjects/graphics/ExtGState.html shows:
In terms of the code from matplotlib, it has something to do with the following lines: matplotlib/lib/matplotlib/backends/backend_pdf.py Lines 1991 to 1996 in 1d5221b
|
This issue has been marked "inactive" because it has been 365 days since the last comment. If this issue is still present in recent Matplotlib releases, or the feature request is still wanted, please leave a comment and this label will be removed. If there are no updates in another 30 days, this issue will be automatically closed, but you are free to re-open or create a new issue if needed. We value issue reports, and this procedure is meant to help us resurface and prioritize issues that have not been addressed yet, not make them disappear. Thanks for your help! |
Thanks for these insights @ShawnZhong ! It seems like matplotlib/lib/matplotlib/backends/backend_pdf.py Lines 1455 to 1466 in bd8198c
Which in turn is called from: This is only called in: matplotlib/lib/matplotlib/backends/backend_pdf.py Lines 2589 to 2625 in bd8198c
(as part of There is a bit related to hatches where maybe alpha should be dealt with in some way... Now, checking #17049, I may be somewhat correct, but the easiest thing is probably to build on that. |
I still have this issue. An alternative is to generate svg files and then convert to pdf. |
I can confirm some of the reports in this thread on ~current main:
|
Uh oh!
There was an error while loading. Please reload this page.
Bug report
Bug summary
Code for reproduction
The same code available in
https://matplotlib.org/2.0.2/examples/pylab_examples/hatch_demo.html
with just two addition:
matplotlib.use('Agg')
andplt.savefig
in different formats.Actual outcome
The pdf saved image does not show all the hatch.
hatch_demo.pdf
Instead, the png and the eps ones show all the hatch correctly.
Expected outcome
The same shown in
https://matplotlib.org/2.0.2/mpl_examples/pylab_examples/hatch_demo.pdf
Matplotlib version
print(matplotlib.get_backend())
):Agg
(but the same happen also whitTkAgg
)Matplotlib installed with pip
The text was updated successfully, but these errors were encountered: