How to change the opacity of a plot using Matplotlib

1 min

Changing the opacity of plot can be extremely useful when you want to compare multiple features, especially when your plot is crowded.

Here is a simple example of a line plot, using the matplotlib library.

import matplotlib.pyplot as plt
import pandas as pd

# We create our dataframe
df = pd.DataFrame(index=range(0,10), data={"col1" : range(0,10)})

fig, axes = plt.subplots(1,1, figsize=(8,6))

# We do a line plot on the axes
axes.plot(df.index,
          df["col1"],
          alpha=0.5) # see the alpha

# Fixing the layout to fit the size
fig.tight_layout()

# Showing the plot
plt.show()
How to change the opacity of a plot using the alpha parameter

As you can see we use the alpha parameter that specify the opacity of the plot which has as value [0,1] (e.g. 0.5).

Here you are ! you now know how to set the opacity of a plot using Matplotlib.