Categories
Python Answers

How to create Python Matplotlib scatter plot with different text at each data point?

Spread the love

Sometimes, we want to create Python Matplotlib scatter plot with different text at each data point

In this article, we’ll look at how to create Python Matplotlib scatter plot with different text at each data point.

How to create Python Matplotlib scatter plot with different text at each data point?

To create Python Matplotlib scatter plot with different text at each data point, we call annotate with the value we want to label the point with.

For instance, we write

import matplotlib.pyplot as plt

y = [2.56422, 3.77284, 3.52623, 3.51468, 3.02199]
z = [0.15, 0.3, 0.45, 0.6, 0.75]
n = [58, 651, 393, 203, 123]

fig, ax = plt.subplots()
ax.scatter(z, y)

for i, txt in enumerate(n):
    ax.annotate(txt, (z[i], y[i]))

to call enumerate with n to loop through the points in n with the index i.

Then we call annotate with the txt label, and the point coordinates in a tuple to label each point with txt.

Conclusion

To create Python Matplotlib scatter plot with different text at each data point, we call annotate with the value we want to label the point with.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *