如何在Matplotlib中设置补丁的edgecolor和facecolor的不同不透明度

smi*_*key 9 python matplotlib

我试图绘制一组具有不同方向和大小的三角形.内部重叠的形状是我想要的,这是最黑暗的区域.但是当我在mpatches.RegularPolygon中设置不透明度(alpha)时,边缘也变得透明.我怎么解决这个问题?谢谢!

xnx*_*xnx 13

您应该发布一些代码以明确您的意思,但根据我的理解,您可以将facecoloredgecolor单独设置为(R,G,B,alpha)元组并设置alphaedgecolor等于1以使其不透明,如果这就是你想.例如,

import matplotlib.pyplot as plt
from matplotlib.patches import Polygon

fig = plt.figure()
ax = fig.add_subplot(111, aspect='equal')

triangle1 = Polygon(((0.05,0.1), (0.396,0.1), (0.223, 0.38)),
                    fc=(1,0,0,0.5), ec=(0,0,0,1), lw=2)
triangle2 = Polygon(((0.2,0.2), (0.5,0.4), (0.3, 0.6)),
                    fc=(1,0,0,0.5), ec=(0,0,0,1), lw=2)

ax.add_artist(triangle1)
ax.add_artist(triangle2)

plt.show()
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

  • 作为额外的提示,如果打算使用命名颜色,技巧是使用 [to_rgba](https://matplotlib.org/3.1.1/api/_as_gen/matplotlib.colors.to_rgba.html) 函数,该函数允许做一些诸如`fc=to_rgba('red', 0.5)`之类的事情 (3认同)
  • 如果有人在使补丁尊重边缘颜色和面部颜色 alpha 方面遇到困难,请确保补丁没有设置“整体”alpha。如果存在,将使用该值,并且不会考虑面部颜色的 RGBA 的 A 部分。“整体”alpha 可以通过“patch.set_alpha(None)”取消设置。 (2认同)