title: “【譯】Basemap手冊 第二章 進階(二)”
date: 2020-02-02T10:28:32+08:00
draft: true
繪製數據
添加註釋
用帶箭頭的文本給地圖上的點添加註釋。用不帶箭頭的文本添加註釋的方法,參見 text
annotate(*args, **kwargs)
- 這個方法不屬於Basemap,而是屬於matplotlib,所以只能和plot、axis一起使用。
- 第一個參數是文本的內容。
- xy是包含x、y軸座標的列表,這個座標點就是箭頭指向的點。
- xycoords 指定xy使用的座標類型:
- data表示座標使用的data(投影座標)
- offset points表示points的偏移量
- axes pixels 表示座標左下角的像素點
- 其他參數參見annotation docs
- xytext 文本標籤的位置,在箭頭的起點
- textcoords 指定座標類型,和xycoords一樣
- arrowprops 設置箭頭屬性,參見Line2D
- color 文本的顏色,參見This page explains all the color options
標記處武漢
from mpl_toolkits.basemap import Basemap
import matplotlib.pyplot as plt
map = Basemap(projection='ortho',
lat_0=30, lon_0=110)
map.drawmapboundary(fill_color='aqua')
map.fillcontinents(color='coral',lake_color='aqua')
map.drawcoastlines()
x, y = map(114, 30) # 經緯度座標 東經114°,北緯30°
x2, y2 = (80, -20)
plt.annotate('Wuhan', xy=(x, y), xycoords='data',
xytext=(x2, y2), textcoords='offset points',
color='r',
arrowprops=dict(arrowstyle="fancy", color='g')
)
x2, y2 = map(80, 30)
plt.annotate('Wuhan', xy=(x, y), xycoords='data',
xytext=(x2, y2), textcoords='data',
arrowprops=dict(arrowstyle="->")
)
plt.show()
效果如下:
barbs
在地圖上繪製風向
barbs(x, y, u, v, *args, **kwargs)
這個暫時不太常用,具體用法參見The barbs docs from the matplotlib documentation。
contour
繪製等高線
contour(x, y, data)
- x和y是矩陣數據
- data是包含點的座標的矩陣
- 還可以傳遞第四個參數,這個參數的值是關於等高線等級的列表
- 默認的colormap是jet,但是可以修改爲cmap
- tri=True可以假定柵格爲非結構化的
示例代碼
import matplotlib.pyplot as plt
from osgeo import gdal
from numpy import linspace
from numpy import meshgrid
map = Basemap(projection='tmerc',
lat_0=0, lon_0=3,
llcrnrlon=1.819757266426611,
llcrnrlat=41.583851612359275,
urcrnrlon=1.841589961763497,
urcrnrlat=41.598674173123)
ds = gdal.Open("dem.tiff")
data = ds.ReadAsArray()
x = linspace(0, map.urcrnrx, data.shape[1])
y = linspace(0, map.urcrnry, data.shape[0])
xx, yy = meshgrid(x, y)
map.contour(xx, yy, data)
plt.show()
如果運行不成功,可能出現的錯誤有兩點:
- 沒有gdal庫
gdal庫的下載方法同前面講的,在這裏找到
- 沒有dem.tiff文件
可以在這裏下載dem.tiff
contourf
繪製着色的等高線圖
這些在上一張中也有介紹,詳情可以參考文檔contourf