Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Matplotlib does not display hatching when rendering to pdf

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.

like image 502
JoshAdel Avatar asked Mar 04 '11 15:03

JoshAdel


2 Answers

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.

like image 84
David Guyon Avatar answered Nov 15 '22 14:11

David Guyon


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='/')
like image 39
Jouni K. Seppänen Avatar answered Nov 15 '22 16:11

Jouni K. Seppänen