Matplotlib - Textbox



A textbox is an area in a document or on a screen where you can type or display text. It is like a little box where you can input or output words, sentences, or paragraphs. Think of it like this −

Input Textbox − When you see a blank area on a form or a website where you can type your name, email, or any other information, that is a textbox. You can click on it and start typing.

Output Textbox − When you see a section of a webpage or document where there is text displayed, like a message or a description, that's also a textbox. It just shows text instead of letting you type into it.

Textbox

Textbox in Matplotlib

In Matplotlib, a Textbox is a graphical element used to display text within a plot, providing additional information or annotations. It allows you to include descriptive text, labels, or explanations directly on the plot.

You can create a textbox in Matplotlib using the text() function. In Matplotlib, the function used to create a textbox is plt.text(). This function allows you to add text directly to a plot, specifying the text content and its position within the plot. Additionally, you can customize various attributes of the textbox, such as font style, size, color, alignment, and background color, to suit their visualization needs.

Basic Text Box

A basic text box in Matplotlib is a simple way to add text to a plot. It allows you to include labels or annotations directly onto the plot. You can specify the content of the text and its position within the plot using the text() function.

Example

In the following example, we are creating a basic text box with the text "Sample Text Box" at the coordinates (2, 10) on the plot −

import matplotlib.pyplot as plt

# Creating a plot
plt.plot([1, 2, 3, 4], [1, 4, 9, 16])

# Adding a basic text box
plt.text(2, 10, 'Sample Text Box', fontsize=12, bbox=dict(facecolor='yellow', alpha=0.5))

# Displaying the plot
plt.show()

Output

Following is the output of the above code −

Basic Text Box

Rounded Text Box

A rounded text box in Matplotlib is a variation of a basic text box with rounded corners. It allows you to add text to a plot within a rectangular-shaped box, but with softened corners.

Example

In here, we are creating a rounded text box with the text "Rounded Text Box" at the coordinates (2, 10) on the plot −

import matplotlib.pyplot as plt

# Creating a plot
plt.plot([1, 2, 3, 4], [1, 4, 9, 16])

# Adding a rounded text box
plt.text(2, 10, 'Rounded Text Box', fontsize=12, bbox=dict(facecolor='lightblue', alpha=0.5, edgecolor='blue', boxstyle='round,pad=0.5'))

# Displaying the plot
plt.show()

Output

On executing the above code we will get the following output −

Rounded Text Box

Custom Text Box with Arrow

A custom text box with an arrow in Matplotlib is a special type of annotation that combines a text box with an arrow pointing to a specific location on a plot. It is a way to draw attention to particular features or data points within a plot.

Example

The following example creates a custom text box with an arrow pointing to the text "Custom Text Box" at the coordinates (2, 10) on the plot −

import matplotlib.pyplot as plt

# Creating a plot
plt.plot([1, 2, 3, 4], [1, 4, 9, 16])

# Adding a custom text box with arrow
plt.annotate('Custom Text Box', xy=(2, 10), xytext=(3, 15),
   arrowprops=dict(facecolor='orange', shrink=0.05))

# Displaying the plot
plt.show()

Output

After executing the above code, we get the following output −

Custom Text Box with Arrow

Multiline Text Box

In Matplotlib, a multiline text box allows you to include multiple lines of text within a single annotation box on a plot. This feature is useful for adding longer explanations, descriptions, or labels that span across multiple lines.

Example

Now, we are creating a multiline text box with three lines of text at the coordinates (2, 10) on the plot. We separate each line by "\n" −

import matplotlib.pyplot as plt

# Creating a plot
plt.plot([1, 2, 3, 4], [1, 4, 9, 16])

# Adding a multiline text box
plt.text(2, 10, 'Line 1\nLine 2\nLine 3', fontsize=12, bbox=dict(facecolor='lightgrey', alpha=0.5))

# Displaying the plot
plt.show()

Output

On executing the above code we will get the following output −

Multiline Text Box
Advertisements