How to Label Each Point in Scatter Plot using Matplotlib
Scatter plots are a great way to visualize the relationship between two variables. However, sometimes it is necessary to label each point in the scatter plot to provide additional information. In this tutorial, we will learn how to label each point in a scatter plot using Matplotlib.
1. Basic Scatter Plot with Labels
import matplotlib.pyplot as plt
x = [1, 2, 3, 4, 5]
y = [2, 3, 1, 4, 5]
labels = ['A', 'B', 'C', 'D', 'E']
plt.scatter(x, y)
for i, label in enumerate(labels):
plt.annotate(label, (x[i], y[i]))
plt.show()
Output:
![How to Label Each Point in Scatter Plot using Matplotlib](https://apidemos.geek-docs.com/matplotlib/2024/06/14/20240606122333-1.png)
2. Customizing Label Positions
import matplotlib.pyplot as plt
x = [1, 2, 3, 4, 5]
y = [2, 3, 1, 4, 5]
labels = ['A', 'B', 'C', 'D', 'E']
plt.scatter(x, y)
for i, label in enumerate(labels):
plt.annotate(label, (x[i] + 0.1, y[i] + 0.1))
plt.show()
Output:
![How to Label Each Point in Scatter Plot using Matplotlib](https://apidemos.geek-docs.com/matplotlib/2024/06/14/20240606122333-2.png)
3. Colored Labels
import matplotlib.pyplot as plt
x = [1, 2, 3, 4, 5]
y = [2, 3, 1, 4, 5]
labels = ['A', 'B', 'C', 'D', 'E']
plt.scatter(x, y)
for i, label in enumerate(labels):
plt.annotate(label, (x[i], y[i]), color='red')
plt.show()
Output:
![How to Label Each Point in Scatter Plot using Matplotlib](https://apidemos.geek-docs.com/matplotlib/2024/06/14/20240606122333-3.png)
4. Adding Arrows to Labels
import matplotlib.pyplot as plt
x = [1, 2, 3, 4, 5]
y = [2, 3, 1, 4, 5]
labels = ['A', 'B', 'C', 'D', 'E']
plt.scatter(x, y)
for i, label in enumerate(labels):
plt.annotate(label, (x[i], y[i]), arrowprops=dict(arrowstyle='->'))
plt.show()
Output:
![How to Label Each Point in Scatter Plot using Matplotlib](https://apidemos.geek-docs.com/matplotlib/2024/06/14/20240606122333-4.png)
5. Customizing Font Size and Style
import matplotlib.pyplot as plt
x = [1, 2, 3, 4, 5]
y = [2, 3, 1, 4, 5]
labels = ['A', 'B', 'C', 'D', 'E']
plt.scatter(x, y)
for i, label in enumerate(labels):
plt.annotate(label, (x[i], y[i]), fontsize=12, fontstyle='italic')
plt.show()
Output:
![How to Label Each Point in Scatter Plot using Matplotlib](https://apidemos.geek-docs.com/matplotlib/2024/06/14/20240606122333-5.png)
6. Rotating Labels
import matplotlib.pyplot as plt
x = [1, 2, 3, 4, 5]
y = [2, 3, 1, 4, 5]
labels = ['A', 'B', 'C', 'D', 'E']
plt.scatter(x, y)
for i, label in enumerate(labels):
plt.annotate(label, (x[i], y[i]), rotation=45)
plt.show()
Output:
![How to Label Each Point in Scatter Plot using Matplotlib](https://apidemos.geek-docs.com/matplotlib/2024/06/14/20240606122333-6.png)
7. Adding Background Color to Labels
import matplotlib.pyplot as plt
x = [1, 2, 3, 4, 5]
y = [2, 3, 1, 4, 5]
labels = ['A', 'B', 'C', 'D', 'E']
plt.scatter(x, y)
for i, label in enumerate(labels):
plt.annotate(label, (x[i], y[i]), bbox=dict(facecolor='red', alpha=0.5))
plt.show()
Output:
![How to Label Each Point in Scatter Plot using Matplotlib](https://apidemos.geek-docs.com/matplotlib/2024/06/14/20240606122333-7.png)
8. Customizing Arrow Properties
import matplotlib.pyplot as plt
x = [1, 2, 3, 4, 5]
y = [2, 3, 1, 4, 5]
labels = ['A', 'B', 'C', 'D', 'E']
plt.scatter(x, y)
for i, label in enumerate(labels):
plt.annotate(label, (x[i], y[i]), arrowprops=dict(arrowstyle='->', connectionstyle='arc3, rad=0.5'))
plt.show()
Output:
![How to Label Each Point in Scatter Plot using Matplotlib](https://apidemos.geek-docs.com/matplotlib/2024/06/14/20240606122333-8.png)
9. Using a Custom Font
import matplotlib.pyplot as plt
x = [1, 2, 3, 4, 5]
y = [2, 3, 1, 4, 5]
labels = ['A', 'B', 'C', 'D', 'E']
plt.scatter(x, y)
for i, label in enumerate(labels):
plt.annotate(label, (x[i], y[i]), fontname='Comic Sans MS')
plt.show()
Output:
![How to Label Each Point in Scatter Plot using Matplotlib](https://apidemos.geek-docs.com/matplotlib/2024/06/14/20240606122333-9.png)
10. Labeling a Subset of Points
import matplotlib.pyplot as plt
x = [1, 2, 3, 4, 5]
y = [2, 3, 1, 4, 5]
labels = ['A', '', 'C', '', 'E']
plt.scatter(x, y)
for i, label in enumerate(labels):
if label:
plt.annotate(label, (x[i], y[i]))
plt.show()
Output:
![How to Label Each Point in Scatter Plot using Matplotlib](https://apidemos.geek-docs.com/matplotlib/2024/06/14/20240606122333-10.png)
Conclusion
In this tutorial, we learned how to label each point in a scatter plot using Matplotlib. We covered various customization options such as changing label positions, colors, adding arrows, customizing font size and style, rotating labels, adding background colors, customizing arrow properties, using custom fonts, and labeling a subset of points. By using these techniques, you can effectively communicate additional information in your scatter plots.