Open In App

How to Change the Transparency of a Graph Plot in Matplotlib with Python?

Last Updated : 28 May, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

Changing transparency in Matplotlib plots enhances visual clarity, especially when data overlaps. Transparency is controlled using a value between 0 (fully transparent) and 1 (fully opaque). This setting can be applied to elements like lines, bars, scatter points and filled areas either during plot creation or afterward by modifying the plot object. The following examples demonstrate various ways to adjust transparency in Matplotlib using Python.

Using alpha parameter

alpha parameter is the most direct way to control transparency when creating plots in Matplotlib. It is supported by almost all plotting functions, including plot(), scatter(), bar(), hist() and others. By specifying alpha in the plotting function, you can control how transparent each element should appear.

Example 1: Line Plot with Low Alpha

Python
import matplotlib.pyplot as plt

x = [0, 5, 10, 15]
y = [0, 1, 2, 3]

plt.plot(x, y, color='blue', alpha=0.3)
plt.title("Line plot with alpha=0.3")
plt.show()

Output

Output
Using alpha parameter

Explanation: The blue line appears faint due to alpha=0.3.

Example 2: Histogram with Transparent Bars

Python
import matplotlib.pyplot as plt
import numpy as np

d = np.random.randn(1000)

plt.hist(d, bins=30, color='orange', alpha=0.4)
plt.title("Histogram with transparent bars")
plt.show()

Output

Output
Using alpha parameter

Explanation: The orange bars are slightly transparent to let overlapping bins be visible.

Example 3: Bar Chart with Varying Alpha

Python
import matplotlib.pyplot as plt
x = ['A', 'B', 'C']
y = [5, 8, 6]

for i in range(len(x)):
    plt.bar(x[i], y[i], color='green', alpha=(i + 1)/4)

plt.title("Bar chart with varying alpha")
plt.show()

Output

Output
Using alpha parameter

Explanation: Each bar has different transparency based on its index (0.25, 0.5, 0.75).

Using set_alpha()

set_alpha() method is useful when you want to adjust the transparency of plot elements dynamically after they have already been created. Instead of setting transparency during the initial plotting, you can access the returned artist object (like a line, scatter point, or filled area) and then apply .set_alpha(value) to it.

Example 1 : Set Alpha After Plotting Line

Python
import matplotlib.pyplot as plt

line, = plt.plot([1, 2, 3], [4, 5, 6])
line.set_alpha(0.5)
plt.title("Alpha set using set_alpha()")
plt.show()

Output

Output
Using set_alpha

Explanation: The plotted line becomes semi-transparent using set_alpha().

Example 2: Set Alpha on Scatter Plot

Python
import matplotlib.pyplot as plt

sc = plt.scatter([1, 2, 3], [4, 5, 6], c='blue')
sc.set_alpha(0.3)
plt.title("set_alpha on scatter")
plt.show()

Output

Output
Using set_alpha

Explanation: All scatter points have 30% opacity.

Example 3: Set Alpha on Filled Area

Python
import matplotlib.pyplot as plt

x = [1, 2, 3, 4]
y = [1, 4, 9, 16]
fill = plt.fill_between(x, y, color='red')

fill.set_alpha(0.4)
plt.title("Fill with alpha using set_alpha")
plt.show()

Output

Output
Using set_alpha

Explanation: The red area between the line and x-axis is slightly see-through.

Using RGBA color

RGBA is a powerful way to define both color and transparency in one argument. It uses a tuple of four float values (Red, Green, Blue, Alpha) where each component ranges from 0.0 to 1.0.

Example 1: RGBA in Bar Chart

Python
import matplotlib.pyplot as plt

plt.bar([1, 2, 3], [3, 6, 9], color=(0.2, 0.6, 0.8, 0.3))
plt.title("Bar with RGBA color")
plt.show()

Output

Output
Using RGBA color

Explanation: The bars are colored light blue with 30% opacity.

Example 2: RGBA in Scatter Plot

Python
import matplotlib.pyplot as plt

plt.scatter([1, 2, 3], [3, 4, 5], color=(1.0, 0.0, 0.0, 0.5))
plt.title("Scatter with RGBA")
plt.show()

Output

Output
Using RGBA color

Explanation: The scatter points are semi-transparent red.

Example 3: Multiple Points with Same RGBA Color

Python
import matplotlib.pyplot as plt
x = [1, 2, 3, 4, 5]
y = [2, 3, 4, 5, 6]
colors = [(0.3, 0.5, 0.7, 0.2)] * 5

plt.scatter(x, y, color=colors)
plt.title("Scatter with same RGBA color")
plt.show()

Output

Output
Using RGBA color

Explanation: All points share the same transparent blue tone.


Next Article

Similar Reads