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?

Denis Kulagin
  • 8,472
  • 17
  • 60
  • 129
  • 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. – daryl Apr 17 '17 at 16:36
  • You could have a look at the answer from [this question](https://stackoverflow.com/questions/19073683/matplotlib-overlapping-annotations-text). Hope it will make sense. – Boorn Sep 23 '17 at 12:29

1 Answers1

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()
Anant Gupta
  • 1,090
  • 11
  • 11