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

标签 python matplotlib plot bezier

我想改变多边形图的不透明度 这个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()

代码生成以下图:

enter image description here

enter image description here

最佳答案

这个库没有很好的文档记录,除了轴以及线和面积的一般颜色之外,似乎没有什么可以传递给绘图的。但是我们可以检索绘制的对象(在本例中,绘制的贝塞尔曲线由 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()

示例输出: enter image description here

关于python - 更改 Python Bezier 包中多边形的不透明度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71267653/

相关文章:

python - 如何使用python获取所有没有特定标签的mysql实例?

python - 测试二维 numpy 数组中的成员资格

生产环境中的 python 和 SQLite

python - 使用 matplotlib 在 Pandas groupby 上绘制直方图

matplotlib - Matlab 在 Julia 中的 "hold on"

python - 运行 sudo 命令以在脚本中执行可执行文件

python - 将 DatetimeIndex 转换为日期时间

python -/usr/bin/python 与 OS X 上的/opt/local/bin/python2.7

python/matplotlib 绘图相当于 R 的符号图?

python - 使用 matplotlib 和 pandas 创建连续图