【Cartopy】1.库的安装和使用
生活随笔
收集整理的這篇文章主要介紹了
【Cartopy】1.库的安装和使用
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
安裝cartopy庫
首先上官方文檔地址:cartopy
在線conda 安裝: conda install cartopy (安裝成功就不需要再看離線安裝方法)
離線安裝: http://www.lfd.uci.edu/~gohlke/pythonlibs/#cartopy
注意:需要一些必須的軟件包Shapely、pyshp,Cartopy所依賴的這兩個庫也都從上面的網址下載。
測試
import matplotlib.pyplot as plt import cartopy.crs as ccrsplt.figure(figsize=(6, 3)) ax = plt.axes(projection=ccrs.PlateCarree(central_longitude=180)) ax.coastlines(resolution='110m') ax.gridlines() plt.show()測試失敗 error/ warning: 沒有地圖文件。這里是因為cartopy運行程序默認從Natural Earth下載地圖文件。可以優先下載地圖文件放在本地文件夾下。
下載Natural Earth地圖數據
- 下載10m、50m和110m分辨率的cultural 和 physical類型數據。
- 解壓到本地路徑:C:\Users\Administrator.local\share\cartopy\shapefiles\natural_earth\physical or C:\Users\Administrator.local\share\cartopy\shapefiles\natural_earth\cultural 中。
注:如果找不到,搜索 C:\Users\xxx.local\share\cartopy\shapefiles\natural_earth\physical
解壓完成后,不能重新運行測試代碼,要對代碼進行修改
繪制地圖測試代碼
demo: 其中scale參數用于調整使用哪種分辨率的地圖,全球地圖建議用1:110的,小尺度地圖可以用1:50的或1:10的。
1 Global map
import matplotlib.pyplot as plt import cartopy.crs as ccrs import cartopy.feature as cfeature from cartopy.mpl.ticker import LongitudeFormatter, LatitudeFormatterscale = '110m' # 補充1 fig = plt.figure(figsize=(8, 10)) ax = plt.axes(projection=ccrs.PlateCarree(central_longitude=180)) ax.set_global() # 補充部分 ###### start ##### land = cfeature.NaturalEarthFeature('physical', 'land', scale, edgecolor='face',facecolor=cfeature.COLORS['land']) ax.add_feature(land, facecolor='0.75') ax.coastlines(scale) ###### end ###### # 標注坐標軸 ax.set_xticks([0, 60, 120, 180, 240, 300, 360], crs=ccrs.PlateCarree()) ax.set_yticks([-90, -60, -30, 0, 30, 60, 90], crs=ccrs.PlateCarree()) # zero_direction_label用來設置經度的0度加不加E和W lon_formatter = LongitudeFormatter(zero_direction_label=False) lat_formatter = LatitudeFormatter() ax.xaxis.set_major_formatter(lon_formatter) ax.yaxis.set_major_formatter(lat_formatter) # 添加網格線 # gl = ax.gridlines() ax.grid() plt.show()2. reginal map
import numpy as np import matplotlib.pyplot as plt import cartopy.crs as ccrs import cartopy.feature as cfeature from cartopy.mpl.ticker import LongitudeFormatter, LatitudeFormatterbox = [100, 150, 0, 50] scale = '50m' # 補充1 此處50m不知道為什么還是不能加載 xstep, ystep = 10, 10 fig = plt.figure(figsize=(8, 10)) ax = plt.axes(projection=ccrs.PlateCarree()) # set_extent需要配置相應的crs,否則出來的地圖范圍不準確 ax.set_extent(box, crs=ccrs.PlateCarree()) # 補充部分 ###### start ##### land = cfeature.NaturalEarthFeature('physical', 'land', scale, edgecolor='face',facecolor=cfeature.COLORS['land']) ax.add_feature(land, facecolor='0.75') ax.coastlines(scale) ###### end ###### # =================================================== # 圖像地址D:\Program\Anaconda\pkgs\cartopy-0.17.0-py37h5ae9855_1\Lib\site-packages\cartopy\data\raster\50-natural-earth-1-downsampled.png # 如果有其它高精度圖像文件,改名替換即可 ax.stock_img() # =================================================== #標注坐標軸 ax.set_xticks(np.arange(box[0], box[1]+xstep,xstep), crs=ccrs.PlateCarree()) ax.set_yticks(np.arange(box[2], box[3]+ystep,ystep), crs=ccrs.PlateCarree()) # zero_direction_label 用來設置經度的0度加不加E和W lon_formatter = LongitudeFormatter(zero_direction_label=False) lat_formatter = LatitudeFormatter() ax.xaxis.set_major_formatter(lon_formatter) ax.yaxis.set_major_formatter(lat_formatter) # 添加網格線 ax.grid() plt.show()注:仔細觀察可以發現上圖scale依然是110m的分辨率,因為50m不能運行,經過測試需要把Natural Earth下載的地圖文件修改名稱,ne_50m_land.shp修改為50m_land.shp(去掉ne_)
因為懶,所以選擇用程序修改文件名,在本文末,附上批量化修改文件名的代碼
3. Polar projection map
import matplotlib.path as mpath import matplotlib.pyplot as plt import numpy as np import matplotlib.ticker as mticker import cartopy.crs as ccrs import cartopy.feature as cfeaturefig = plt.figure(figsize=(6, 6)) ax = plt.axes(projection=ccrs.NorthPolarStereo()) box = [-180, 180, 55, 90] xstep, ystep = 30, 15 # Limit the map to -60 degrees latitude and below. ax.set_extent(box, crs=ccrs.PlateCarree()) # =================================================== scale = '50m' # 補充1 land = cfeature.NaturalEarthFeature('physical', 'land', scale, edgecolor='face',facecolor=cfeature.COLORS['land']) ocean = cfeature.NaturalEarthFeature('physical', 'ocean', scale, edgecolor='face',facecolor=cfeature.COLORS['water']) ax.add_feature(land, facecolor='0.75') ax.add_feature(ocean, facecolor='blue') ax.coastlines(scale, linewidth=0.9) # =================================================== # 標注坐標軸 line = ax.gridlines(draw_labels=False)line.ylocator = mticker.FixedLocator(np.arange(40, 90, 20)) # 手動設置x軸刻度 line.xlocator = mticker.FixedLocator(np.arange(-180, 210, 30)) # 手動設置x軸刻度 # Compute a circle in axes coordinates, which we can use as a boundary # for the map. We can pan/zoom as much as we like - the boundary will be # permanently circular. theta = np.linspace(0, 2 * np.pi, 100) center, radius = [0.5, 0.5], 0.5 verts = np.vstack([np.sin(theta), np.cos(theta)]).T circle = mpath.Path(verts * radius + center) ax.set_boundary(circle, transform=ax.transAxes)# 創建要標注的labels字符串 ticks = np.arange(0, 210, 30) etick = ['0'] + ['%d$^\circ$E' % tick for tick in ticks if (tick != 0) & (tick != 180)] + ['180'] wtick = ['%d$^\circ$W' % tick for tick in ticks if (tick != 0) & (tick != 180)] labels = etick + wtick # 創建與labels對應的經緯度標注位置 # xticks=[i for i in np.arange(0,210,30)]+[i for i in np.arange(-32,-180,-30)] xticks = [-0.8, 28, 58, 89.1, 120, 151, 182.9, -36, -63, -89, -114, -140] yticks = [53] + [53] + [54] + [55] * 2 + [54.5] + [54] + [50] + [49] * 3 + [50.6]# 標注經緯度 # ax.text(0.01,0.23,'60$^\circ$W',transform=ax.transAxes,rotation=25) # ax.text(-63,50,'60$^\circ$W',transform=ccrs.Geodetic(),rotation=25) for xtick, ytick, label in zip(xticks, yticks, labels):ax.text(xtick, ytick, label, transform=ccrs.Geodetic()) x = [180, 180, 0, 0] y = [50, 90, 90, 50] ax.plot([-180, 0], [80, 80], ':', transform=ccrs.Geodetic(), color='k', linewidth=0.4) ax.plot([-90, 90], [80, 80], ':', transform=ccrs.Geodetic(), color='k', linewidth=0.5) # ax.plot([90,0],[50,50],'-.',transform=ccrs.Geodetic(),color='r', linewidth=6)ax.text(11.9333, 78.9166, r'\bigstar', transform=ccrs.Geodetic(), size=15, color='r') plt.show()批量修改文件名
import os path = r'E:\temp' fileLists = os.listdir(path) for f in fileLists:if f[:3] == 'ne_'oldname = path + os.sep + fnewname = path + os.sep + f[3:]os.rename(oldname, newname)總結
以上是生活随笔為你收集整理的【Cartopy】1.库的安装和使用的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: OpenSolaris/Solaris
- 下一篇: 计算机科学刘培姣,高中物理课堂融入物理学