25

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()
monodera
  • 283
  • 1
  • 3
  • 8
  • [This answer](http://stackoverflow.com/a/6552115/4403123) might help. I'll see if I can understand it and write an answer for what you're doing. – KSFT Apr 09 '15 at 21:54
  • In the comments [here](http://stackoverflow.com/questions/14325773/how-to-change-marker-border-width-and-hatch-width?rq=1) @tcaswell says it would be very very hard to make hatch line widths adjustable. – cphlewis Apr 10 '15 at 00:10
  • I see it's complicated... Thanks for the comments. – monodera Apr 10 '15 at 09:37
  • Yeah, it looks really hard. In case someone else wants to try to figure it out, I'll leave these here: http://matplotlib.org/api/path_api.html#matplotlib.path.Path.hatch, http://fossies.org/dox/matplotlib-1.4.3/hatch_8py_source.html – KSFT Apr 10 '15 at 20:31
  • 3
    Alternatively, you can increase the number of hatch, like this: `hatch='/'*20`, it may give the impression of thickness. – jrjc Nov 09 '15 at 09:10

4 Answers4

38

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
heisenBug
  • 865
  • 9
  • 19
  • 1
    This should be the accepted answer. Odd how this setting is so restricted/not configurable with API commands. – Luke Davis Nov 20 '18 at 05:14
4

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)
Nicor Lengert
  • 188
  • 1
  • 12
  • I used your first suggestion to modify the line in `backend_pdf.py` temporarily. This is a nice work-around until an option becomes available to modify it with a keyword argument. – Matt Hancock Mar 10 '16 at 17:18
2

There is a solution which is very hacky, but allows you to do what you want without changing matplotlib internal files: you can monkey patch the writeHatches of PdfFile like so:


# make sure you have the correct imports,
# they may differ depending on the matplotlib version
import matplotlib.backends.backend_pdf
from matplotlib.externals import six
from matplotlib.backends.backend_pdf import Name, Op
from matplotlib.transforms import Affine2D

def setCustomHatchWidth(customWidth):

     def _writeHatches(self):
        COPY CODE FROM matplotlib.__path__[0] + "/backends/backend_pdf.py" HERE
        change the line 
            self.output(0.1, Op.setlinewidth)
        to 
            self.output(customWidth, Op.setlinewidth)

    matplotlib.backends.backend_pdf.PdfFile.writeHatches = _writeHatches

You can then do

setCustomWidth(2)

before saving you figure as pdf.

explorerDude
  • 323
  • 3
  • 8
1

I have a workaround that may help some. I use this when making my final plots for reports. The width of the hatch is affected by the dpi setting in

plt.savefig('pic',dpi=300)

The following figure is done at 80 DPI enter image description here

Thenenter image description here again at 400 DPI

I fully understand that this may introduce other issues, but I figured it is worth the mention.

Austin Downey
  • 943
  • 2
  • 11
  • 28