简体   繁体   English

如何在matplotlib中更改填充的线宽?

[英]How to change the linewidth of hatch in matplotlib?

Is there a way to increase the width of hatch in matplotlib? 有没有办法增加matplotlib中的影线宽度?

For example, the following code by specifying linewidth only changes the width of the edge. 例如,以下代码通过指定linewidth仅更改边的宽度。 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()

As of matplotlib version 2.0, you can directly change the linewidth parameter , as follows: 从matplotlib 2.0版开始,您可以直接更改linewidth参数 ,如下所示:

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: 您可以通过以下方式检查matplotlib版本:

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. 如果您使用pdf并拥有sudo权限,则可以在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 . 通常它位于/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). 还有人写了一个黑客来从你的脚本执行此操作(仍然需要执行sudo权限)。 Solution from here: http://micol.tistory.com/358 解决方案来自: 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)

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: 有一个非常hacky的解决方案,但允许你做你想要的而不改变matplotlib内部文件:你可以修补PdfFile的writeHatches,如下所示:


# 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. 在保存之前你的数字是pdf。

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 阴影线的宽度受dpi设置的影响

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

The following figure is done at 80 DPI 下图是在80 DPI下完成的 在此输入图像描述

Then 然后 在此输入图像描述 again at 400 DPI 再次在400 DPI

I fully understand that this may introduce other issues, but I figured it is worth the mention. 我完全明白这可能会引入其他问题,但我认为值得一提。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM