9

I would like to draw a scatter plot with labeling. However, these labels are overlapping. How can I enhance its look so that I can see numbers better? Also, I have numbers in integer, but it shows label values in float. I am wondering why.

Looking forward to hearing back from you guys.

Here's my code:

col = df['type'].map({'a':'r', 'b':'b', 'c':'y'})
ax = df.plot.scatter(x='x', y='y', c=col)

df[['x','y','id']].apply(lambda x: ax.text(*x),axis=1)

enter image description here

Thomas Kühn
  • 9,412
  • 3
  • 47
  • 63
ejshin1
  • 1,107
  • 6
  • 17
  • 35
  • 1
    From how your points are distributed I think it will be very difficult to find an automated way to make the labels not overlap. A few alternative ways of visualisation come to mind, but with the information you gave us it's hard to know if they are applicable. Can you maybe add information of what the colours of the data points and the label numbers represent? – Thomas Kühn Aug 18 '17 at 05:55
  • 2
    It might be worth considering making the marker size/area proportional to the value. When the points are in dense clusters it it unlikely that the reader can resolver which labels are proportional to which points. – oliversm Aug 18 '17 at 08:03
  • 7
    You may look at the [adjustText](https://github.com/Phlya/adjustText) package. [Here](https://stackoverflow.com/questions/43737511/access-matplotlib-objects-of-scatter-plot) would be an example question about it. – ImportanceOfBeingErnest Aug 18 '17 at 10:51
  • @ImportanceOfBeingErnest very nice! I especially like the [example section](https://github.com/Phlya/adjustText/blob/master/examples/Examples.ipynb) of that github page. – Thomas Kühn Aug 18 '17 at 11:29

1 Answers1

2

I suggest that you install the adjustText package:

python -m pip install adjustText

and later follow this old Stackoverflow answer.

gboffi
  • 22,939
  • 8
  • 54
  • 85
ZahraRezaei
  • 251
  • 2
  • 14