How to change the linewidth of hatch in matplotlib?

后端 未结 4 1168
执笔经年
执笔经年 2021-02-05 08:47

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

4条回答
  •  余生分开走
    2021-02-05 08:56

    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.

提交回复
热议问题