在Basemap上打印文本字符串,以代替Python中的点

在Basemap上打印文本字符串,以代替Python中的点,python,plot,matplotlib-basemap,Python,Plot,Matplotlib Basemap,我有79个lat和LON,我有数据可以在Python的底图上绘制。我有一个包含79个数字的数组,我想用它来绘图,而不是一个普通的点(也就是说,我想用一个“1”或“2”来显示,而不是一个普通的点)。我尝试了plt.annotate函数,但没有成功 import numpy as np import matplotlib.pyplot as plt from mpl_toolkits.basemap import Basemap locs = np.genfromtxt('/Volumes/NO_

我有79个lat和LON,我有数据可以在Python的底图上绘制。我有一个包含79个数字的数组,我想用它来绘图,而不是一个普通的点(也就是说,我想用一个“1”或“2”来显示,而不是一个普通的点)。我尝试了
plt.annotate
函数,但没有成功

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

locs = np.genfromtxt('/Volumes/NO_NAME/Classwork/OK_vic_grid.txt')
lat = locs[:,1] # 79 values
lon = locs[:,2] # 79 values

m = Basemap(projection='stere',lon_0=-95,lat_0=35.,lat_ts=40,\
        llcrnrlat=33,urcrnrlat=38,\
        llcrnrlon=-103.8,urcrnrlon=-94) 

X,Y = m(lon,lat)    
m.drawcoastlines()
m.drawstates()
m.drawcountries()
m.drawmapboundary(fill_color='lightblue')
m.drawparallels(np.arange(0.,40.,2.),color='gray',dashes=[1,3],labels=[1,0,0,0])
m.drawmeridians(np.arange(0.,360.,2.),color='gray',dashes=[1,3],labels=[0,0,0,1])

m.scatter(X,Y)
????? (Want to plot an array "maxpc" which has 79 numbers that I want to plot the string of)
OK_vic_grid.txt:

1   33.75   -97.75
2   33.75   -97.25
3   33.75   -96.75
4   33.75   -96.25
5   33.75   -95.75
6   33.75   -95.25
7   33.75   -94.75
8   34.25   -99.75
9   34.25   -99.25
10  34.25   -98.75
11  34.25   -98.25
12  34.25   -97.75
13  34.25   -97.25
14  34.25   -96.75
15  34.25   -96.25
16  34.25   -95.75
17  34.25   -95.25
18  34.25   -94.75
19  34.75   -99.75
20  34.75   -99.25
21  34.75   -98.75
22  34.75   -98.25
23  34.75   -97.75
24  34.75   -97.25
25  34.75   -96.75
26  34.75   -96.25
27  34.75   -95.75
28  34.75   -95.25
29  34.75   -94.75
30  35.25   -99.75
31  35.25   -99.25
32  35.25   -98.75
33  35.25   -98.25
34  35.25   -97.75
35  35.25   -97.25
36  35.25   -96.75
37  35.25   -96.25
38  35.25   -95.75
39  35.25   -95.25
40  35.25   -94.75
41  35.75   -99.75
42  35.75   -99.25
43  35.75   -98.75
44  35.75   -98.25
45  35.75   -97.75
46  35.75   -97.25
47  35.75   -96.75
48  35.75   -96.25
49  35.75   -95.75
50  35.75   -95.25
51  35.75   -94.75
52  36.25   -99.75
53  36.25   -99.25
54  36.25   -98.75
55  36.25   -98.25
56  36.25   -97.75
57  36.25   -97.25
58  36.25   -96.75
59  36.25   -96.25
60  36.25   -95.75
61  36.25   -95.25
62  36.25   -94.75
63  36.75   -102.75
64  36.75   -102.25
65  36.75   -101.75
66  36.75   -101.25
67  36.75   -100.75
68  36.75   -100.25
69  36.75   -99.75
70  36.75   -99.25
71  36.75   -98.75
72  36.75   -98.25
73  36.75   -97.75
74  36.75   -97.25
75  36.75   -96.75
76  36.75   -96.25
77  36.75   -95.75
78  36.75   -95.25
79  36.75   -94.75

ax.scatter
ax.text
都需要一个x,y点,而不是一个位置数组

两者都可以很好地工作,但需要使用循环

例如:

import numpy as np
import matplotlib.pyplot as plt

xy = np.random.random((5, 2))
text = ['A', 'B', 'C', 'D', 'E']

fig, ax = plt.subplots()
for (x,y), label in zip(xy, text):
    ax.text(x, y, label, ha='center', size=20)
plt.show()

使用
basemap
,您需要将经度和纬度投影到投影地图坐标中(即
X
Y
阵列)。例如(我还将在此处使用
注释
将标签偏移几点):


在一个旁注下,您也可以考虑使用BaseMax。代码>基本地图

或多或少已被
cartopy
完全取代。两者都是基于matplotlib的映射工具包,
cartopy
比i.m.o.好得多。映射轴是
的真正子类,因此没有单独的
Basemap
对象,而是真正的轴。此外,根据我的经验,Cartopy比basemap具有更多的功能,并且通常更快。

您的问题是什么?多了解一点背景可能有助于找到答案。@skywalker我想在我的底图上画一个散点图,但我想让“maxpc”中包含的数字显示出来,而不是每个lat lon位置的点。你能提供OK_vic_grid.txt吗?为什么不提供热图?你真的想看到每个经度和纬度的数值吗?只是好奇而已。@skywalker我不熟悉热图。但是,是的,我只想要我在maxpc阵列中的每个lat和LON的数字,而不是点。谢谢你的回复,乔。运行此操作时,我收到一条错误消息,说明Basemap没有将annotate作为属性。@DJV-您需要使用
ax.annotate
,而不是
m.annotate
。(我在最初的版本中有一个打字错误。现在正在编辑。)啊,太好了!谢谢。乔,卡托皮看起来很棒。我有一个免费的Canopy学生版,看起来我得付钱。不管怎样,你都可以单独安装。我不知道默认情况下Canopy提供了什么版本,但它们都不应该阻止您安装它。(顺便说一句,虽然天篷很漂亮,但你也可以看看水蟒,看看在更宽松的许可证下类似的分布。)
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.basemap import Basemap

num = 79
lat = 5 * np.random.random(num) + 33
lon = 10 * np.random.random(num) - 104

fig, ax = plt.subplots()
m = Basemap(projection='stere',lon_0=-95,lat_0=35.,lat_ts=40,
            llcrnrlat=33,urcrnrlat=38,
            llcrnrlon=-103.8,urcrnrlon=-94,
            resolution='h', ax=ax)

X,Y = m(lon,lat)
m.drawcoastlines()
m.drawstates()
m.drawcountries()
m.drawmapboundary(fill_color='lightblue')
m.drawparallels(np.arange(0.,40.,2.),color='gray',dashes=[1,3],labels=[1,0,0,0])
m.drawmeridians(np.arange(0.,360.,2.),color='gray',dashes=[1,3],labels=[0,0,0,1])

ax.scatter(X,Y)

for i, (x, y) in enumerate(zip(X, Y), start=1):
    ax.annotate(str(i), (x,y), xytext=(5, 5), textcoords='offset points')

plt.show()