在 Python Bezier 包中更改多边形的不透明度

Changing the opacity of the polygons in the Python Bezier package

我想更改多边形图的不透明度 这个Python Bezier package.

这是我试过的代码:

import bezier
import matplotlib.pyplot as plt
import numpy

plt.clf()

nodes0 = numpy.asfortranarray([[0.0, 1.0, 2.0], [0.0, -1.0, 0.0]])
edge0 = bezier.Curve(nodes0, degree=2)

nodes1 = numpy.asfortranarray([[2.0, 2.0], [0.0, 1.0]])
edge1 = bezier.Curve(nodes1, degree=1)

nodes2 = numpy.asfortranarray([[2.0, 1.0, 0.0], [1.0, 2.0, 1.0]])
edge2 = bezier.Curve(nodes2, degree=2)

nodes3 = numpy.asfortranarray([[0.0, 0.0], [1.0, 0.0]])
edge3 = bezier.Curve(nodes3, degree=1)

curved_poly = bezier.CurvedPolygon(edge0, edge1, edge2, edge3)

# ax.set_alpha(1.0)                    # <-- I tried this, does not produce any effect.
ax = curved_poly.plot(pts_per_edge=12) # <-- Does not take alpha argument.

# plt.plot(alpha=1)                    # <-- I tried this, does not produce any effect.
plt.show()

代码生成以下图:

这个库不是well-documented,除了线和区域的轴和一般颜色之外,似乎没有什么可以传递给绘图的。但是我们可以检索绘制的对象(在这种情况下,绘制的贝塞尔曲线由 Line2D 和 PathPatch 对象组成)并修改它们:

import bezier
import matplotlib.pyplot as plt
import numpy
from matplotlib.lines import Line2D
from matplotlib.patches import PathPatch


fig, (ax1, ax2) = plt.subplots(2)

nodes0 = numpy.asfortranarray([[0.0, 1.0, 2.0], [0.0, -1.0, 0.0]])
edge0 = bezier.Curve(nodes0, degree=2)

nodes1 = numpy.asfortranarray([[2.0, 2.0], [0.0, 1.0]])
edge1 = bezier.Curve(nodes1, degree=1)

nodes2 = numpy.asfortranarray([[2.0, 1.0, 0.0], [1.0, 2.0, 1.0]])
edge2 = bezier.Curve(nodes2, degree=2)

nodes3 = numpy.asfortranarray([[0.0, 0.0], [1.0, 0.0]])
edge3 = bezier.Curve(nodes3, degree=1)

curved_poly = bezier.CurvedPolygon(edge0, edge1, edge2, edge3)


curved_poly.plot(pts_per_edge=12, color="green", ax=ax1) 

for item in ax1.get_children():
    if isinstance(item, Line2D):
        item.set_color("red")
        item.set_alpha(0.7)
    if isinstance(item, PathPatch):
        item.set_alpha(0.1)
plt.show()

示例输出: