1

I am using Matplotlib Basemap to draw a map and points with labels:

map = Basemap(...)

x, y = map(lons, lats)    
for label, xpt, ypt in zip(labels, x, y):
    plt.text(xpt + 10, ypt + 10, label, size=2)

I am getting lots of overlapped labels in dense areas. Is there a way to prevent labels from overlapping?

2
  • Don't know of a way out of the box to do it, I do a manual hack of estimating text bounding boxes and then compute overlap onto a pixel grid of previous plotted text, if no overlap is found, I plot the text and add its bounding box rectangle to the pixel grid. Commented Apr 17, 2017 at 16:36
  • You could have a look at the answer from this question. Hope it will make sense. Commented Sep 23, 2017 at 12:29

1 Answer 1

2

The only ways I can think of is to

  1. Adjust the distance from which text printing starts ( which you have specified as 10 )
  2. Zoom into the map while showing the labeled points

A crude example for point 2

Full view Map

further Zoom High Zoom

from mpl_toolkits.basemap import Basemap
import matplotlib.pyplot as plt
m = Basemap(width=120000,height=90000,projection='aeqd',
            resolution=None,lat_0=30.,lon_0=80.)
lats=[30.0,30.1,30.2,30.0,30.1,30.2]
lons=[80.0,80.1,80.2,80.3,80.4,80.5]
m.bluemarble()
x, y = m(lons,lats)
labels=['Point1','Point2','Point3','Point4','Point5','Point6']
m.scatter(x,y,10,marker='o',color='k')
for label, xpt, ypt in zip(labels, x, y):
    plt.text(xpt + 10, ypt + 10, label, size=20)
plt.show()
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.