Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I set different opacity of edgecolor and facecolor of a patch in Matplotlib

I am trying to plot a set of triangles with different orientations and sizes. The inner overlapped shape is what i wanted, which is the darkest area. But when I set opacity (alpha) in mpatches.RegularPolygon, the edges become transparent too. How can I solve this problem? Thank you!

like image 542
smirkymonkey Avatar asked Jan 05 '16 07:01

smirkymonkey


People also ask

How to set different opacity of edgecolor and facecolor of a patch?

How to set different opacity of edgecolor and facecolor of a patch in Matplotlib? To set different opacity of edge and face color, we can use a color tuple and the 4th index of the tuple could set the opacity value of the colors.

How to set the edge color of a patch in Matplotlib?

Show activity on this post. per the documentation, edgecolors is default to None, which got override by matplotlib.rcParams default settings, which is transparent. So either do: p = PatchCollection (patches, edgecolor='r', alpha=0.4) ax.add_collection (p)

What are the colors in Matplotlib colorspace?

Matplotlib indexes color at draw time and defaults to black if cycle does not include color. "Red", "Green", and "Blue" are the intensities of those colors. In combination, they represent the colorspace.

How to use edgecolor for bar edges?

The color (s) for bar edges. The parameter takes a color or list of colors. If a color value is given for edgecolor, then all the bars will get the same color for their edges. Note: If we are providing a list of colors for edgecolor parameter, the length of this list of colors should match the length of x and height.


1 Answers

You should perhaps post some code to make your meaning clear, but from what I understand, you could set the facecolor and edgecolor separately as (R,G,B,alpha) tuples and set alpha for the edgecolor equal to 1 to make it opaque if that's what you want. For example,

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

enter image description here

like image 70
xnx Avatar answered Sep 18 '22 06:09

xnx