[PYTHON] You can manage overlapping characters with multiple plt.text.

I want to get rid of overlapping characters in multiple plt.text.

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))]

wiki_example_before.png

** I have a nice library that can solve such a problem, so I will share it **

Just insert adjustText

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)

wiki_example_after_no_arrows.png

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'))

wiki_example_after_with_arrows.png

Thank you! !! !!

reference

https://github.com/Phlya/adjustText

Recommended Posts

You can manage overlapping characters with multiple plt.text.
Python | What you can do with Python
What you can do with API vol.1
What you can do with programming skills
Until you can use opencv with python
Manage multiple Python versions with update-alternatives (Ubuntu)
Can you challenge Kaggle with just your iPad?
Manage Python multiple version environment with Pythonz, virtualenv
You can easily create a GUI with Python
Manage multiple context managers together with Python contextlib.ExitStack
You can now display slides with nbviewer! [Jupyter]
You can read the analog meter with the example MNIST.
What you can and cannot do with Tensorflow 2.x
Until you can do simple image recognition with Jupyter