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


               


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



Python


In this article, we show how to change the transparency of a graph plot in matplotlib with Python.

So when you create a plot of a graph, by default, matplotlib will have the default transparency set (a transparency of 1).

However, this transparency can be adjusted.

Matplotlib allows you to adjust the transparency of a graph plot using the alpha attribute.

By default, alpha=1

If you want to make the graph plot more transparent, then you can make alpha less than 1, such as 0.5 or 0.25.

If you want to make the graph plot less transparent, then you can make alpha greater than 1. This solidifies the graph plot, making it less transparent and more thick and dense, so to speak.

In the following code shown below, we show how to change the transparency of the graph plot in matplotlib with Python.



So the first thing we have to do is import matplotlib. We do this with the line, import matplotlib.pyplot as plt

We then create a variable fig, and set it equal to, plt.figure()

This creates a figure object, which of course is initially empty, because we haven't populated it with anything. We then add axes to this figure. We then have our x coordinates that range from 0 to 10.

We then plot the square of the x coordinates.

It is with the plot() function that we specify the transparency of the plot. We do this with the alpha attribute. In this case, we set the transparency equal to a very low value, 0.1, giving the graph plot a lot of transparency.

If you want to make the graph plot have a very low transparency, you would give the alpha attribute a very high value.

Lastly, we show the figure with the show() function.

This works if you're using a python IDE other than jupyter notebooks. If you are using jupyter notebooks, then you would not use, plt.show(). Instead you would specify in the code right after importing matplotlib, %matplotlib inline

This line allows the figure of a graph to be shown with jupyter notebooks.

After running the following code above, we get the following figure with the graph plot being very transparent shown in the image below.


Figure object with increased transparency in matplotlib with Python


So now you see a figure object with a graph plot that is very transparent.

And this is how you can change the transparency of a graph plot in matplotlib with Python.


Related Resources

How to Randomly Select From or Shuffle a List in Python



HTML Comment Box is loading comments...