Adding text inside a plot can help in labeling specific data points, highlighting areas of interest, or providing additional information to make your plots more informative and easier to understand. In this tutorial, we will learn how to add text inside plots using Matplotlib, a popular plotting library in Python. We will cover: 1. Adding simple text inside a plot 2. Customizing text properties 3. Using annotations to highlight specific data points 4. Advanced example with multiple annotations ## Adding Simple Text Inside a Plot The `text()` function in Matplotlib allows you to add text at an arbitrary location inside the plot.
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 inside the plot plt.text(2, 6, "Important Point", fontsize=12, color='red') # Display the plot plt.xlabel('X-axis') plt.ylabel('Y-axis') plt.title('Simple Text Inside a Plot') plt.show()
In this example, we plot some data and use the `text()` function to add the text "Important Point" at the location x=2, y=6 inside the plot. ## Customizing Text Properties You can customize the text by changing its font size, color, alignment, rotation, and more.
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 customized text inside the plot plt.text(3, 5, "Prime Number", fontsize=14, color='green', fontweight='bold', ha='center', va='bottom', rotation=45) # Display the plot plt.xlabel('X-axis') plt.ylabel('Y-axis') plt.title('Customized Text Inside a Plot') plt.show()
In this example, the text "Prime Number" is customized with a larger font size, green color, bold font weight, center horizontal alignment, bottom vertical alignment, and 45-degree rotation. ## Using Annotations to Highlight Specific Data Points The `annotate()` function in Matplotlib provides more flexibility when you need to add text with an arrow pointing to a specific data point.
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 annotation to highlight a specific data point plt.annotate('Highest Value', xy=(5, 11), xytext=(3, 10), arrowprops=dict(facecolor='black', shrink=0.05)) # Display the plot plt.xlabel('X-axis') plt.ylabel('Y-axis') plt.title('Annotation Inside a Plot') plt.show()
In this example, the `annotate()` function is used to add the text "Highest Value" with an arrow pointing to the data point at x=5, y=11. The text is positioned at (3, 10) and an arrow is drawn pointing from the text to the annotated point. ### Advanced Example with Multiple Annotations Let's create a more complex plot with multiple annotations to showcase how you can combine these techniques.
import matplotlib.pyplot as plt import numpy as np # Create some data x = np.linspace(0, 10, 100) y = np.sin(x) # Basic plot plt.plot(x, y) # Add multiple pieces of text and annotations plt.text(2, 0.5, 'First Peak', fontsize=12, color='blue') plt.annotate('Second Peak', xy=(3*np.pi/2, np.sin(3*np.pi/2)), xycoords='data', xytext=(3*np.pi/2, np.sin(3*np.pi/2) - 0.5), textcoords='data', arrowprops=dict(facecolor='black', shrink=0.05)) plt.text(7, 0.5, 'Third Peak', fontsize=12, color='green') plt.annotate('Fourth Peak', xy=(2*np.pi, np.sin(2*np.pi)), xycoords='data', xytext=(2*np.pi, 1), textcoords='data', arrowprops=dict(facecolor='red', shrink=0.05)) # Display the plot plt.show()
This example showcases: - Adding simple text at arbitrary locations. - Using `plt.annotate()` to add arrows pointing to specific peaks in the data. By using these techniques, you can effectively add informative text annotations to your plots, making them more informative and easier to understand.