配置Python開發環境
安裝python
記得勾選添加環境變量。Add Python to PATH
import this
欣賞下python之禪
下載安裝第三方包
pip install flake8
pip install yapf
檢查是否已存在需要的包
pip list
配置Visual Studio Code
用戶設置
Visual Studio Code 文件 -> 首選項 -> 設置 -> 用戶 配置settings.json
"python.pythonPath": "python3",
工作區域設置
在工作區域輸入以下內容:
{
"python.linting.flake8Enabled": true,
"python.formatting.provider": "yapf",
"python.linting.flake8Args": ["--max-line-length=248"],
"python.linting.pylintEnabled": false
}
matplotlib
安裝matplotlib
python -m pip install matplotlib
測試運行matplotlib
運行測試程序:
import numpy as np
from matplotlib import pyplot as plt
x = np.arange(1, 11)
y = 2 * x + 5
plt.title("Matplotlib demo")
plt.xlabel("x axis caption")
plt.ylabel("y axis caption")
plt.plot(x, y)
plt.show()
basemap
安裝geos
pip install geos
安裝basemap
basemap無法用pip安裝
訪問 https://www.lfd.uci.edu/~gohlke/pythonlibs/
選擇basemap,根據自己安裝的Python版本選擇,32和64是指安裝的Python的版本,cp後面的數字是Python的版本。
還需在當前頁面下載pyproj,pyhdf,netCDF4對應的whl包
pip install pyproj-2.4.0-cp37-cp37m-win_amd64.whl
pip install pyhdf-0.10.1-cp37-cp37m-win_amd64.whl
pip install basemap-1.2.1-cp37-cp37m-win_amd64.whl
pip install netCDF4-1.5.3-cp37-cp37m-win_amd64.whl
測試運行basemap
運行測試程序:
import matplotlib.pyplot as plt
from mpl_toolkits.basemap import Basemap
plt.figure(figsize=(16, 8))
m = Basemap()
m.drawcoastlines()
m.drawcountries(linewidth=1.5)
plt.show()
運行
參考 https://matplotlib.org/basemap/users/examples.html
我們使用第一個例子:Plot contour lines on a basemap(在地圖上畫輪廓線)
from mpl_toolkits.basemap import Basemap
import matplotlib.pyplot as plt
import numpy as np
# set up orthographic map projection with
# perspective of satellite looking down at 50N, 100W.
# use low resolution coastlines.
map = Basemap(projection='ortho',lat_0=45,lon_0=-100,resolution='l')
# draw coastlines, country boundaries, fill continents.
map.drawcoastlines(linewidth=0.25)
map.drawcountries(linewidth=0.25)
map.fillcontinents(color='coral',lake_color='aqua')
# draw the edge of the map projection region (the projection limb)
map.drawmapboundary(fill_color='aqua')
# draw lat/lon grid lines every 30 degrees.
map.drawmeridians(np.arange(0,360,30))
map.drawparallels(np.arange(-90,90,30))
# make up some data on a regular lat/lon grid.
nlats = 73; nlons = 145; delta = 2.*np.pi/(nlons-1)
lats = (0.5*np.pi-delta*np.indices((nlats,nlons))[0,:,:])
lons = (delta*np.indices((nlats,nlons))[1,:,:])
wave = 0.75*(np.sin(2.*lats)**8*np.cos(4.*lons))
mean = 0.5*np.cos(2.*lats)*((np.sin(2.*lats))**2 + 2.)
# compute native map projection coordinates of lat/lon grid.
x, y = map(lons*180./np.pi, lats*180./np.pi)
# contour data over the map.
cs = map.contour(x,y,wave+mean,15,linewidths=1.5)
plt.title('contour lines over filled continent background')
plt.show()