how to plot a text on basemap, python
Asked Answered
W

1

6

I was thinking to put a text on my map, like the satellite imagery.

import numpy as np, matplotlib.pyplot as plt    
from mpl_toolkits.basemap import Basemap    

m = Basemap(resolution='l',projection='geos',lon_0=-75.)    
fig = plt.figure(figsize=(10,8))    
m.drawcoastlines(linewidth=1.25)    
x,y = m(-150,80)
plt.text(x,y,'Jul-24-2012')

However, the text "Jul-24-2012" doesn't show up on my figure. I guess the reason of this is because the map is not in Cartesian coordinates.

So, could anyone help me to figure out how to do this, please?

Wisner answered 3/12, 2013 at 23:35 Comment(0)
R
10

The reason that your text didn't show up is that you're trying to plot a point that's invalid for the map projection that you're using.

If you're just wanting to place text at a point in axes coordinates (e.g. the upper left hand corner of the plot) use annotate, not text.

In fact, it's fairly rare that you'll actually want to use text. annotate is much more flexible, and is actually geared towards annotating a plot, rather than just placing text at an x,y position in data coordinates. (For example, even if you want to annotate an x,y position in data coords, you often want the text offset from it by a distance in points instead of data units.)

import matplotlib.pyplot as plt

from mpl_toolkits.basemap import Basemap

m = Basemap(resolution='l',projection='geos',lon_0=-75.)

fig = plt.figure(figsize=(10,8))

m.drawcoastlines(linewidth=1.25)

#-- Place the text in the upper left hand corner of the axes
# The basemap instance doesn't have an annotate method, so we'll use the pyplot
# interface instead.  (This is one of the many reasons to use cartopy instead.)
plt.annotate('Jul-24-2012', xy=(0, 1), xycoords='axes fraction')

plt.show()

enter image description here

Resin answered 4/12, 2013 at 0:9 Comment(3)
It should be noted that annotate is just a fancy wrapper for text which takes care of the transforms for you in the background.Jesseniajessey
This is really help. I had changed it to figure pixels for the location. Thank You.Wisner
This answer is perfect. Why don't you accept it @Franke ?Ultan

© 2022 - 2024 — McMap. All rights reserved.