如何在Python底图上绘制值?

时间:2018-09-09 17:18:24

标签: python-3.x matplotlib-basemap

是否可以在底图上绘制值?

假设我有3个数据列表。

lat = [50.3, 62.1, 41.4, ...]
lon = [12.4, 14.3, 3.5, ...]
val = [3, 5.4, 7.4, ...]

我已经创建了一个简单的底图:

def create_map(ax=None, lllon=6.00, lllat=47.0, urlon=16.00, urlat=55.10):

    m = Basemap(llcrnrlon=lllon, llcrnrlat=lllat, \
            urcrnrlon=urlon, urcrnrlat=urlat, \
            resolution='h', \
            projection='tmerc', \
            lon_0=(lllon+urlon)/2, lat_0=(lllat+urlat)/2)

    m.drawcoastlines()
    m.drawcountries()
    m.drawrivers()

    return m

现在,我想根据其坐标在此地图上绘制“ val”列表的值:

m = create_map()

x, y = m(lon,lat)

m.scatter(x, y, val) # somthing like that

plt.show()

好吧,我已经知道底图无法绘制3d值,但是有办法实现吗?

1 个答案:

答案 0 :(得分:0)

第一个问题的简短,甜蜜和简单的答案是,可以使用底图(此处为documentation)进行绘制。

如果您希望在3d模式下进行绘制,则documentation会说明如何使用底图进行绘制。这是一个入门的简单脚本:

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

plt.close('all')
fig = plt.figure()
ax = fig.gca(projection='3d')
extent = [-127, -65, 25, 51]

# make the map and axis.
m = Basemap(llcrnrlon=extent[0], llcrnrlat=extent[2],
             urcrnrlon=extent[1], urcrnrlat=extent[3],
             projection='cyl', resolution='l', fix_aspect=False, ax=ax)
ax.add_collection3d(m.drawcoastlines(linewidth=0.25))
ax.add_collection3d(m.drawcountries(linewidth=0.25))
ax.add_collection3d(m.drawstates(linewidth=0.25))
ax.view_init(azim = 230, elev = 15)
ax.set_xlabel(u'Longitude (°E)', labelpad=10)
ax.set_ylabel(u'Latitude (°N)', labelpad=10)
ax.set_zlabel(u'Altitude (ft)', labelpad=20)

# values to plot - change as needed. Plots 2 dots, one at elevation 0 and another 100.
# also draws a line between the two.
x, y = m(-85.4808, 32.6099)
ax.plot3D([x, x], [y, y], [0, 100], color = 'green', lw = 0.5)
ax.scatter3D(x, y, 100, s = 5, c = 'k', zorder = 4)
ax.scatter3D(x, y, 0, s = 2, c = 'k', zorder = 4)

ax.set_zlim(0., 400.)
plt.show()