Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scatter plot label overlaps - matplotlib

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

like image 456
ejshin1 Avatar asked Aug 18 '17 04:08

ejshin1


People also ask

How do you avoid overlapping labels on a scatter plot?

There may be several approaches, create a data frame for the annotation, group by column value and list the indexes. Set annotations in the created data frame. In this data example, more strings overlap, so we change the offset values only for the indices we do not want to overlap.

How do you prevent labels overlapping in Matplotlib?

Use legend() method to avoid overlapping of labels and autopct. To display the figure, use show() method.

How do you avoid overlapping plots in python?

Dot Size. You can try to decrease marker size in your plot. This way they won't overlap and the patterns will be clearer.


1 Answers

I suggest that you install the adjustText package:

python -m pip install adjustText

and later follow this old Stackoverflow answer.

like image 179
ZahraRezaei Avatar answered Sep 23 '22 08:09

ZahraRezaei