複数のplt.textで重なる文字をなんとかできる。
複数のplt.textで重なる文字をなんとかしたい。
plt.textをfor文で何個も付けていると文字同士が重なり読めなくなってしまう…
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))]
そんなお困りを解決してくれる嬉しいライブラリがあったので共有します
adjustTextを入れるだけ
このライブラリはR/ggplot2のggrepel packageに影響を受けて作られたそうです。
(Rの方は知りません)
導入はpipでできます。
pip install adjustText
使い方も簡単で、adjustTextのadjust_textに整列したいtextsをリストにして入れるだけ
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)
どの点のアノテーションかわかりやすいようにplt.annotateのような矢印を入れることも可能。
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'))
ありがてえ!!!
参考
Author And Source
この問題について(複数のplt.textで重なる文字をなんとかできる。), 我々は、より多くの情報をここで見つけました https://qiita.com/mosomoso_1910/items/54b1f8da014676687d2b著者帰属:元の著者の情報は、元のURLに含まれています。著作権は原作者に属する。
Content is automatically searched and collected through network algorithms . If there is a violation . Please contact us . We will adjust (correct author information ,or delete content ) as soon as possible .