在Visual Studio Code中利用python3 matplotlib绘制等值线图

配置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()

在这里插入图片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章