I am attempting to use the hatching feature in matplotlib, which works fine when displaying to screen. However when I save the figure to pdf format, the hatch marks are not rendered:
import matplotlib
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(0,2*np.pi,100)
plt.figure()
plt.fill(x,np.sin(x),color='blue',alpha=0.5,hatch='/')
plt.show()
plt.savefig('./test.pdf',format='pdf')
I am using matplotlib 1.0.1 on OS X 10.6.6 (Specifically the Enthought Python Distribution v7).
Upon posting the issue initially to Stackoverflow, I received a workaround for the issue by using:
Among the arguments accepted by the fill function are color, edgecolor, facecolor, hatch, and alpha, and it's not obvious to me what the semantics should be, although it is clearly a bug that the agg and pdf backends create different output.
My guess is that the agg backend fills the area with "color", then draws hatches with "color", and applies alpha compositing to the two operations separately so that the hatches show up as a darker shade of blue. The pdf backend creates a tiling pattern that includes both the hatch lines and the fill color and paints the area with that pattern, with alpha set to 0.5. The effect is a uniform blue color, since the pattern is just full of blue, and compositing it on the canvas just makes it lighter. The workaround works because there are two separate objects being drawn, so they are composited on top of each other.
Should the pdf backend emulate the agg backend by drawing the hatch lines separately? Any other ideas?
Relatedly, is the same "alpha" supposed to apply to both facecolor and edgecolor? What should happen if one of these is an RGBA tuple?
I have just checked, and this is definitely fixed in v1.1.x branch (and hence 1.1.1rc2), although the hatching is made a little uglier by not having the hatch gap fix from #853 backported.
Yeah -- unfortunately #853 also required changing the hatch spacing, so it's a fairly visible change and in my mind went beyond a bug fix -- but it's certainly in a grey area in that regard.
This is a "feature" of MAC OSX which is called "anti-aliasing".
I've encountered the same problem recently (with recent version of mpl), the hatches are actually quite thin, and the MAC OSX PDF anti-aliasing filters just smooth them to vanish.
Opening the same figure with Xpdf just works fine ;)
It would be helpful to have some control on hatches thickness (mpl.rcParams[] seems to be ignored for hatches within a contourplot).
@dirteat, could you open a new issue describing this problem in more detail? The original bug here is different, and we don't want to confuse the two (similar) problems.
Activity
jkseppan commented on Mar 4, 2011
Among the arguments accepted by the fill function are color, edgecolor, facecolor, hatch, and alpha, and it's not obvious to me what the semantics should be, although it is clearly a bug that the agg and pdf backends create different output.
My guess is that the agg backend fills the area with "color", then draws hatches with "color", and applies alpha compositing to the two operations separately so that the hatches show up as a darker shade of blue. The pdf backend creates a tiling pattern that includes both the hatch lines and the fill color and paints the area with that pattern, with alpha set to 0.5. The effect is a uniform blue color, since the pattern is just full of blue, and compositing it on the canvas just makes it lighter. The workaround works because there are two separate objects being drawn, so they are composited on top of each other.
Should the pdf backend emulate the agg backend by drawing the hatch lines separately? Any other ideas?
Relatedly, is the same "alpha" supposed to apply to both facecolor and edgecolor? What should happen if one of these is an RGBA tuple?
skovhus commented on May 20, 2012
What is the status of this issue?
mdboom commented on May 23, 2012
Perhaps related, the contourf_hatching.py demo throws an exception when saving to PDF.
pelson commented on Jun 9, 2012
I think that was a redherring which we managed to solve in #891.
None the less, the example given in this issue now produces a pdf on master which, when viewed in evince, has the desired hatch pattern.
@skovhus: Did you come across this issue because you are seeing the same problem?
pelson commented on Jun 10, 2012
I have just checked, and this is definitely fixed in v1.1.x branch (and hence 1.1.1rc2), although the hatching is made a little uglier by not having the hatch gap fix from #853 backported.
mdboom commented on Jun 11, 2012
Yeah -- unfortunately #853 also required changing the hatch spacing, so it's a fairly visible change and in my mind went beyond a bug fix -- but it's certainly in a grey area in that regard.
Merge pull request matplotlib#29 from fariza/basic-info
dirteat commented on Jan 27, 2021
This is a "feature" of MAC OSX which is called "anti-aliasing".
I've encountered the same problem recently (with recent version of mpl), the hatches are actually quite thin, and the MAC OSX PDF anti-aliasing filters just smooth them to vanish.
Opening the same figure with Xpdf just works fine ;)
It would be helpful to have some control on hatches thickness (mpl.rcParams[] seems to be ignored for hatches within a contourplot).
WeatherGod commented on Jan 27, 2021
@dirteat, could you open a new issue describing this problem in more detail? The original bug here is different, and we don't want to confuse the two (similar) problems.