Home > other >  Changing the opacity of the polygons in the Python Bezier package
Changing the opacity of the polygons in the Python Bezier package

Time:02-26

I want to change the opacity the polygon plots made with this enter image description here

enter image description here

CodePudding user response:

This library is not well-documented, and apart from the axis and the general color for both line and area, there seems to be nothing that you can pass on to the plot. But we can retrieve the plotted objects (in this case, the plotted Bezier curve consists of a Line2D and a PathPatch object) and modify them:

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()

Sample output: enter image description here

  • Related