Skip to content
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

Matplotlib does not display hatching when rendering to pdf #29

Closed
synapticarbors opened this issue Mar 4, 2011 · 8 comments
Closed
Assignees
Milestone

Comments

@synapticarbors
Copy link

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:

plt.fill(x,np.sin(x),color='blue',alpha=0.5)
plt.fill(x,np.sin(x),color='None',alpha=0.5,edgecolor='blue',hatch='/')

Not sure why this works, but it saves the pdf correctly when using the two commands in combination.

@jkseppan
Copy link
Member

jkseppan commented 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?

@ghost ghost assigned mdboom Jun 15, 2011
@skovhus
Copy link

skovhus commented May 20, 2012

What is the status of this issue?

@mdboom
Copy link
Member

mdboom commented May 23, 2012

Perhaps related, the contourf_hatching.py demo throws an exception when saving to PDF.

@pelson
Copy link
Member

pelson commented Jun 9, 2012

Perhaps related, the contourf_hatching.py demo throws an exception when saving to PDF.

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

pelson commented 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.

@pelson pelson closed this as completed Jun 10, 2012
@mdboom
Copy link
Member

mdboom commented 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.

fariza added a commit to fariza/matplotlib that referenced this issue Sep 22, 2016
Basic info, and simplifying plots
@dirteat
Copy link

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

@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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants