If you add many plt.text in the for statement, the characters will overlap and you will not be able to read it ...
import numpy as np
import matplotlib.pyplot as plt
np.random.seed(0)
x, y = np.random.random((2,30))
fig, ax = plt.subplots()
plt.plot(x, y, 'bo')
texts = [plt.text(x[i], y[i], 'Text%s' %i, ha='center', va='center') for i in range(len(x))]
** I have a nice library that can solve such a problem, so I will share it **
This library is said to have been influenced by the ggplot package of R / ggplot2. (I don't know about R) Installation can be done with ** pip **.
pip install adjustText
Easy to use, just ** list the texts you want to align with adjust_text in adjustText **
from adjustText import adjust_text
fig, ax = plt.subplots()
plt.plot(x, y, 'bo')
texts = [plt.text(x[i], y[i], 'Text%s' %i, ha='center', va='center') for i in range(len(x))]
adjust_text(texts)
It is also possible to insert an arrow ** such as ** plt.annotate to make it easy to understand which point of the annotation.
fig, ax = plt.subplots()
plt.plot(x, y, 'bo')
texts = [plt.text(x[i], y[i], 'Text%s' %i, ha='center', va='center') for i in range(len(x))]
adjust_text(texts, arrowprops=dict(arrowstyle='->', color='red'))
Thank you! !! !!
https://github.com/Phlya/adjustText
Recommended Posts