如何在basemap上绘制文本,python

2024-05-23 17:12:13 发布

您现在位置:Python中文网/ 问答频道 /正文

我想在地图上写一段文字,比如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')

然而,“Jul-24-2012”并没有出现在我的图上。 我想这是因为地图不在笛卡尔坐标系中。

有谁能帮我弄清楚怎么做吗?


Tags: thefromimportnumpymatplotlibasnp地图
1条回答
网友
1楼 · 发布于 2024-05-23 17:12:13

文本未显示的原因是,您试图绘制的点对于正在使用的地图投影无效。

如果您只想将文本放在坐标(例如,绘图的左上角)的某个点上,请使用annotate,而不是text

事实上,很少有人真正想使用textannotate更加灵活,实际上是为了标注绘图,而不仅仅是将文本放在数据坐标中的x,y位置。(例如,即使要在数据坐标系中注释x、y位置,也通常希望文本与该位置的偏移量为而不是数据单位中的距离。)

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

相关问题 更多 >