2

In this 1D scatter plot:

import numpy as np
import matplotlib.pyplot as plt

D = {'label1': 7.33, 'label2': 7.12, 'label3': 4.26, 'label4': 6.98}
plt.scatter(D.values(), [0] * 4, marker='o', s=[100] * 4, cmap=plt.get_cmap('Spectral'))
for i, k in enumerate(D):
    plt.annotate(k, xy=(D[k], 0), xytext=(-20, 20), textcoords='offset points', ha='right', va='bottom', bbox=dict(boxstyle='round,pad=0.5', fc='yellow', alpha=0.5), arrowprops=dict(arrowstyle = '->', connectionstyle='arc3,rad=0'))

plt.show()

the labels overwrite each other.

How to let scatter / annotate find the most appropriate place to put the labels automatically around the points?

enter image description here

Basj
  • 41,386
  • 99
  • 383
  • 673

0 Answers0