
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Adjusting Text background transparency in Matplotlib
The matplotlib is a library in Python that allows us to create graphs and plots for data visualization. This library have several built-in functions to style the plots, such as changing colors, adding titles, setting backgrounds, labels, and adjusting the layout of subplots. In this article, we will learn how to adjust the transparency of text backgrounds in matplotlib plots.
- Text Backgrounds in Matplotlib
- Adjusting Background Transparency of Title Text
- Adjusting Background Transparency of Labels
- Conclusion
Text Backgrounds in Matplotlib
In matplotlib, text backgrounds refer to the area behind the texts such as titles, labels, and annotations in a plot. By default, the background of text is transparent, but we can change it to a solid color (red, blue, etc) or a semi-transparent color (i.e., red with 50% transparency).
The image below shows an example of a plot with text backgrounds of different transparencies:

The label of first plot have background color blue with transparency 0.6. For the second plot, transparency is 0.9 for the same blue color.
Adjusting Background Transparency of Title Text
Title of a plot is a text given at the top of the plot to understand the purpose of the plot. The title() function of plt module is used to add a title to a plot. Inside this function, we can use the bbox parameter which will take a dictionary that specifies the background color and transparency of the title text.
Syntax
plt.plot([1, 2, 3], [1, 4, 6]) plt.title('Graph For Linear Function', bbox=dict(facecolor='yellow', alpha=0.9, edgecolor='black') ) plt.show()
In this code, we create a simple line plot and add a title 'Graph For Linear Function' to it. The bbox parameter is used to set the background color and transparency of the title text. The facecolor is set to 'yellow' and the alpha is set to 0.9, which means the background will be yellow with 90% opacity.
Example
In this example, we will create a simple line plot and adjust the background transparency of the title text.
import matplotlib.pyplot as plt # Create a Plot for y = 2x plt.figure(figsize=(4, 3)) # Set figure size x = [1, 2, 3] y = [2 * i for i in x] plt.plot(x, y) plt.title('Graph For Linear Function', bbox=dict(facecolor='yellow', alpha=0.5, edgecolor='black') ) plt.show()
The output of the above code will be:

Adjusting Background Transparency of Labels
Labels are used to describe the axes of a plot. We can adjust the background transparency of labels in a similar way as we did for the title text. Following is the code to add labels to a plot and adjust their background transparency:
plt.plot([1, 2, 3], [1, 4, 6]) plt.xlabel('X-axis Label', bbox=dict(facecolor='blue', alpha=0.6, edgecolor='black') ) plt.ylabel('Y-axis Label', bbox=dict(facecolor='green', alpha=0.6, edgecolor='black') ) plt.show()
In this code snippet, we create a simple line plot and add labels to the x-axis and y-axis. The bbox parameter is used to set the background color and transparency of the labels. The facecolor is set to 'blue' for x-axis label and 'green' for y-axis label, with an alpha value of 0.6.
Now, let's see a working example of this code:
Example
In this example, we will create a simple line plot and adjust the background transparency of the x-axis and y-axis labels.
import matplotlib.pyplot as plt # Create a Plot for y = 2x plt.figure(figsize=(4, 3)) # Set figure size x = [1, 2, 3] y = [2 * i for i in x] plt.plot(x, y) plt.xlabel('X-axis Label', bbox=dict(facecolor='blue', alpha=0.4, edgecolor='black') ) plt.ylabel('Y-axis Label', bbox=dict(facecolor='green', alpha=0.1, edgecolor='black') ) plt.show()
The output of the above code will be:

Conclusion
In this article, we learned how to adjust the background transparency of text in matplotlib plots. We can use the bbox parameter in the title(), xlabel(), and ylabel() functions to set the background color and transparency of the text. The facecolor value will determine the color of the background, and the alpha value will determine the transparency level (0 is fully transparent and 1 is fully opaque).