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.
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
.
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='/')
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With