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?

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

3 Comments

It should be noted that annotate is just a fancy wrapper for text which takes care of the transforms for you in the background.
This is really help. I had changed it to figure pixels for the location. Thank You.
This answer is perfect. Why don't you accept it @Franke ?

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.