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

MatplotlibPythonData Visualization

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.


  • Set the figure size and adjust the padding between and around the subplots.
  • Create a figure and a set of subplots using subplots() method.
  • Set different values for edge and face color opacity.
  • Add a rectangel patch using add_patch() method.
  • To display the figure, use show() method.


from matplotlib import pyplot as plt, patches

plt.rcParams["figure.figsize"] = [7.50, 3.50]
plt.rcParams["figure.autolayout"] = True

figure, ax = plt.subplots()
edge_color_opacity = 1 # 0<val<1
face_color_opacity = 0.75 # 0<val<1

ax.add_patch(patches.Rectangle((.25, .25), .50, .50,
                              edgecolor=(1, 0, 0, edge_color_opacity),
                              facecolor=(0, 1, 0, face_color_opacity),


Published on 09-Jun-2021 12:13:25