Is there a way to increase the width of hatch in matplotlib?
For example, the following code by specifying linewidth
only changes the width of the edge. I want to change the linewidth of the line used for hatch.
import matplotlib.pyplot as plt
import numpy as np
x = np.random.randn(100)
fig = plt.figure()
ax = fig.add_subplot(111)
ax.hist(x, fill=False, hatch='/', linewidth=2)
plt.show()
Linewidth: By default the linewidth is 1. For graphs with multiple lines it becomes difficult to trace the lines with lighter colors.
linewidth"] (default: 1.0 ), which defaults to 1 point.
The scatter plot in matplotlib has a hatch keyword argument that specifies a pattern on the marker. Below, is an example that runs through a handful of hatch patterns, on randomly selected symbols. Curiously, hatch is not a kwarg of the scatter function, but of collections .
As of matplotlib version 2.0, you can directly change the linewidth parameter, as follows:
import matplotlib as mpl
mpl.rcParams['hatch.linewidth'] = 0.1 # previous pdf hatch linewidth
mpl.rcParams['hatch.linewidth'] = 1.0 # previous svg hatch linewidth
This seems a better workaround than what folks have above. You can check the matplotlib version by:
import matplotlib as mpl
print(mpl.__version__) # should be 2.x.y
If you use pdf and have sudo rights you can change it in backend_pdf.py. There is a line
self.output(0.1, Op.setlinewidth)
Usually it is located in /usr/lib/pymodules/python2.7/matplotlib/backends/backend_pdf.py .
Also someone wrote a hack to do this from your script (still need sudo rights to execute). Solution from here: http://micol.tistory.com/358
import os
import re
import matplotlib
def setHatchThickness(value):
libpath = matplotlib.__path__[0]
backend_pdf = libpath + "/backends/backend_pdf.py"
with open(backend_pdf, "r") as r:
code = r.read()
code = re.sub(r'self\.output\((\d+\.\d+|\d+)\,\ Op\.setlinewidth\)',
"self.output(%s, Op.setlinewidth)" % str(value), code)
with open('/tmp/hatch.tmp', "w") as w:
w.write(code)
print backend_pdf
os.system('sudo mv /tmp/hatch.tmp %s' % backend_pdf)
setHatchThickness(1.0)
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