Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

It is hard to set opacity for polygon plotting #290

Open
antononcube opened this issue Feb 25, 2022 · 3 comments
Open

It is hard to set opacity for polygon plotting #290

antononcube opened this issue Feb 25, 2022 · 3 comments

Comments

@antononcube
Copy link

antononcube commented Feb 25, 2022

I want to change the opacity of the polygon plots made with this package.

Here is the code I tried initially:

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

After posting a question StackOverflow I was able to get the Bezier polygon plots with the desired opacity using code like:

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

See "Changing the opacity of the polygons in the Python Bezier package".

@dhermes
Copy link
Owner

dhermes commented Feb 26, 2022

@antononcube thanks for filing! What do you think would make sense? A single alpha applied to everything? An edge alpha and a patch alpha? Returning the list of all patch / line objects created?

@antononcube
Copy link
Author

antononcube commented Feb 26, 2022

@dhermes My simplistic suggestion is to just have an alpha argument for bezier.CurvedPolygon, for example:

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

(In my RandomMandala package I use the alpha argument in that way in order to control the opacity of the "bezier_fill" mandalas.)


Here is an explanation/suggestion by Mr.T from the StackOverflow discussion linked above:

Well, the problem is that you have to retrieve immediately the Line2D and PathPatch objects representing the Bezier curve because if you plot multiple objects, you wouldn't know which of them belongs to your curves. I think they are prepended, i.e., Line2D[0] and PathPatch[0] would be the last curve generated. Unless, of course, you want to give all Bezier curves the same attributes; then, this approach is directly applicable. Too bad, that they don't allow passing parameters to matplotlib, return the objects, or document their source code

Again, I use the simplistic approach -- alpha is applied to all. If alpha is None, then that is the current behavior (in the bezier package.)

@dhermes
Copy link
Owner

dhermes commented Feb 26, 2022

I am totally fine with that approach. If you would you like to send a PR I'm happy to review it and approve it. (You probably won't have to deal with unit tests if you use a keyword argument with a nice default for alpha; I think it's either 1.0 or None.) Otherwise, I'll try to get to it soon.

Worth noting also, I am currently (mostly) blocked on cutting a release due to my lack of M1 access.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants