11
Lesson 11
Adding annotations and text
Objective
By the end of this lesson, students will learn how to add annotations and text to their plots using Matplotlib's annotate() and text() functions, enhancing the clarity and communication of important data points in their visualizations.
1. Adding annotations with annotate():
The annotate() function allows you to add notes pointing to specific data points in a plot. You can use arrows or other symbols to highlight these points and provide extra information, such as labels, numerical values, or descriptions.
Syntax:
By the end of this lesson, students will learn how to add annotations and text to their plots using Matplotlib's annotate() and text() functions, enhancing the clarity and communication of important data points in their visualizations.
1. Adding annotations with annotate():
The annotate() function allows you to add notes pointing to specific data points in a plot. You can use arrows or other symbols to highlight these points and provide extra information, such as labels, numerical values, or descriptions.
Syntax:
plt.annotate(text, xy, xytext, arrowprops)
- text : The annotation text.
- xy : The position (x, y) of the point to annotate.
- xytext : The position where the annotation text will appear.
- arrowprops : A dictionary of properties for the arrow (optional).
Example: Basic annotation
import matplotlib.pyplot as plt # Sample data x = [1, 2, 3, 4, 5] y = [2, 3, 5, 7, 11] # Create a plot plt.plot(x, y, marker='o') # Annotate a point (at x=3, y=5) plt.annotate('Prime Number', xy=(3, 5), xytext=(3, 8), arrowprops=dict(facecolor='black', shrink=0.05)) # Add title and labels plt.title('Annotation Example') plt.xlabel('X-axis') plt.ylabel('Y-axis') # Show the plot plt.show()
In this example:
- The point (3, 5) is annotated with the text "Prime Number".
- The annotation includes an arrow pointing from the text to the data point.
2. Customizing annotations:
You can customize annotations by modifying the text position, arrow style, and font properties. This is helpful when you want to improve the visual appeal and readability of the annotation.
a. Changing the arrow style:
You can modify the appearance of the arrow using the arrowprops parameter.
Example:
import matplotlib.pyplot as plt # Sample data x = [1, 2, 3, 4, 5] y = [2, 3, 5, 7, 11] # Create a plot plt.plot(x, y, marker='o') # Annotate a point with a dashed arrow plt.annotate('Prime Number', xy=(3, 5), xytext=(3, 8), arrowprops=dict(facecolor='blue', arrowstyle='->', linestyle='dashed')) # Add title and labels plt.title('Customized Annotation Example') plt.xlabel('X-axis') plt.ylabel('Y-axis') # Show the plot plt.show()
In this example:
- The arrow has a dashed style, pointing to the annotated data point.
- The arrowstyle parameter can be customized with different arrow types (->, <-, -[, etc.).
b. Adjusting font properties:
You can customize the font size, color, and style for the annotation text.
Example:
import matplotlib.pyplot as plt # Sample data x = [1, 2, 3, 4, 5] y = [2, 3, 5, 7, 11] # Create a plot plt.plot(x, y, marker='o') # Annotate a point with custom font properties plt.annotate('Prime Number', xy=(3, 5), xytext=(3, 8), arrowprops=dict(facecolor='black', shrink=0.05), fontsize=12, color='red', fontweight='bold') # Add title and labels plt.title('Annotation with Custom Font Properties') plt.xlabel('X-axis') plt.ylabel('Y-axis') # Show the plot plt.show()
In this example:
- The annotation text is displayed in red with a bold font.
- The fontsize, color, and fontweight parameters are used to modify the appearance of the text.
3. Adding text to plots with text():
The text() function allows you to place static text at a specific position in the plot. Unlike annotate(), this function does not use arrows or highlight specific data points, but it is useful for adding labels or notes directly on the plot.
Syntax:
plt.text(x, y, text, fontsize, color)
- x : The x-coordinate where the text will be placed.
- y : The y-coordinate where the text will be placed.
- text : The text string to display.
- fontsize : The size of the text (optional).
- color : The color of the text (optional).
Example: Adding static text
import matplotlib.pyplot as plt # Sample data x = [1, 2, 3, 4, 5] y = [2, 3, 5, 7, 11] # Create a plot plt.plot(x, y, marker='o') # Add text at a specific location plt.text(1.5, 10, 'This is a text note', fontsize=12, color='blue') # Add title and labels plt.title('Text Example') plt.xlabel('X-axis') plt.ylabel('Y-axis') # Show the plot plt.show()
In this example:
- The text "This is a text note" is placed at the coordinates (1.5, 10).
- The text is displayed in blue with a font size of 12.
4. Exercises:
Exercice: 1
1. Create a line plot using the data:
X-axis: [1, 2, 3, 4, 5]
Y-axis: [2, 4, 6, 8, 10]
2. Annotate the point (3, 6) with the label "Key Point" and draw an arrow pointing to the point.
Exercice: 2
1. Use the same plot as the previous exercise.
a. Modify the annotation by:
b. Changing the arrow color to green.
c. Making the arrow dashed.
d. Displaying the text in bold red font.
Exercice: 3
1. Add static text to a plot. Use the following data:
X-axis: [0, 1, 2, 3, 4]
Y-axis: [1, 2, 4, 8, 16]
2. Add the text "Exponential Growth" at the position (1.5, 12) in blue font.
Exercice: 4
1. Create a plot with the following data:
X-axis: [10, 20, 30, 40, 50]
Y-axis: [100, 200, 150, 300, 250]
2. Add a text label "Highest Point" near the peak point, and annotate the point (40, 300).
Conclusion
In this lesson, we explored how to add annotations and text to plots using Matplotlib. We learned how to highlight specific data points with annotate(), customize the arrow and text styles, and use text() to add static notes to a plot. These techniques are essential for making your plots more informative and easier to interpret. In the next lesson, we will cover more advanced plot customization techniques.