21

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 in pylab on OS X 10.6.6. This may be a platform specific issue having to do with the backend renderer, but I'm not sure. Any suggestions would be most appreciated.

2 Answers 2

19

TL;DR: use alpha=.99 to render hatches when exporting in PDF

It's nearly 2020 and the bug still exists when using plt.bar(). When rendering in PNG, everything is rendered properly. However, PDF export has a glitch when rendering hatches. Hatches are not visible, sometimes visible when zooming-in/out (sometimes not when tested on different computers), it is not clear where the bug comes from.

We realized it's linked with the alpha option. When using alpha=.5, the color is 50% visible, as well as hatches (50% visible as well). Good step, we have almost visible hatches. Therefore, let's just try with alpha=.99 so that everything is nearly 100% visible.

It works! Houray!

In our workaround, no need to duplicate lines like in previous answer. Keep the color option as it is and just set alpha=.99.

8

Looks like a bug. Please file it in the github issue tracker.

In the meantime, here's a workaround:

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='/')
1
  • Fantastic. Thanks for the workaround and I'll post the issue.
    – JoshAdel
    Commented Mar 4, 2011 at 19:34

Not the answer you're looking for? Browse other questions tagged or ask your own question.