氣象繪圖(一)-cartopy入門

概念引入

Axes/Subplot概念:

Axes/Subplot與Figure的關(guān)系

Axes/Subplot的異同:

共同點:兩者都相當(dāng)于畫布figure上的畫紙ax,概念上是相同的。

不同點:兩者在創(chuàng)建畫紙ax上略有不同。

Axes創(chuàng)建方式:

plt.axes(rect,projection=None,polar=False,**kwargs)

Parameters:
rect : None or 列表 = [left, bottom, width, height],取值范圍為0到1
projection : 投影類型

分別使用subplot和axes創(chuàng)建子圖

示例一,使用add_subplot()方法
1)導(dǎo)包

import numpy as np
import matplotlib.pyplot as plt

2)創(chuàng)建示例數(shù)據(jù)

# 創(chuàng)建示例數(shù)據(jù)
x = np.arange(1, 10, 0.1)  # start = 1, end = 10, step = 0.1
y1 = np.sin(x)
y2 = np.cos(x)

3)使用add_subplot()方法創(chuàng)建子圖

fig = plt.figure(figsize=(16, 9))  # 創(chuàng)建一個畫布

ax1 = fig.add_subplot(211)  # 創(chuàng)建子圖1
ax1.plot(x, y1, 'r', linestyle='--')

ax2 = fig.add_subplot(212)  # 創(chuàng)建子圖2
ax2.plot(x, y2, 'b', linestyle='-.')
add_subplot()方法創(chuàng)建的子圖.png

示例二,使用subplot()方法創(chuàng)建子圖

plt.figure(figsize=(16, 9))  # 創(chuàng)建一個畫布

# 使用subplot創(chuàng)建
plt.subplot(211)
plt.plot(x, y1, 'r', linestyle='--')

plt.subplot(212)
plt.plot(x, y2, 'b', linestyle='-.')

或:

plt.figure(figsize=(16, 9))  # 創(chuàng)建一個畫布

# 使用subplot創(chuàng)建
ax1 = plt.subplot(211)
ax1.plot(x, y1, 'r', linestyle='--')

ax2 = plt.subplot(212)
ax2.plot(x, y2, 'b', linestyle='-.')
使用subplot()方法創(chuàng)建的子圖.png

示例三,使用add_axes()方法創(chuàng)建子圖

# 創(chuàng)建示例數(shù)據(jù)
x = np.arange(1, 10, 0.1)  # start = 1, end = 10, step = 0.1
y1 = 1 / x
y2 = np.cos(x)


# ------------------畫圖------------------

fig = plt.figure(figsize=(16, 9))  # 創(chuàng)建一個畫布

ax1 = fig.add_axes([0.1, 0.1, 0.6, 0.75])
ax1.plot(x, y1, 'b', linestyle='-.')

ax2 = fig.add_axes([0.38, 0.5, 0.3, 0.3])
ax2.plot(x, y2, 'r', linestyle='--')

示例四,使用axes()方法創(chuàng)建子圖

plt.figure(figsize=(16, 9))  # 創(chuàng)建一個畫布

ax1 = plt.axes([0.1, 0.1, 0.6, 0.75])
ax1.plot(x, y1, 'b', linestyle='-.')

ax2 = plt.axes([0.38, 0.5, 0.3, 0.3])
ax2.plot(x, y2, 'r', linestyle='--')
使用axes()方法創(chuàng)建.png

非規(guī)則拼圖

# 非規(guī)則拼圖
fig = plt.figure(figsize=(16, 9))  # 創(chuàng)建一個畫布
ax1 = fig.add_subplot(221)  # 創(chuàng)建子圖1
ax2 = fig.add_subplot(222)  # 創(chuàng)建子圖2
ax3 = fig.add_subplot(212)  # 創(chuàng)建子圖3

ax1.plot(x, np.sin(x), 'r', linestyle='-.')
ax2.plot(x, np.cos(x), 'b', linestyle='--')
ax3.plot(x, np.log(x), 'g', )
非規(guī)則拼圖1.png

# 非規(guī)則拼圖
fig = plt.figure(figsize=(16, 9))  # 創(chuàng)建一個畫布
ax1 = fig.add_subplot(221)  # 創(chuàng)建子圖1
ax2 = fig.add_subplot(223)  # 創(chuàng)建子圖2
ax3 = fig.add_subplot(122)  # 創(chuàng)建子圖3

ax1.plot(x, np.sin(x), 'r', linestyle='-.')
ax2.plot(x, np.cos(x), 'b', linestyle='--')
ax3.plot(x, np.log(x), 'g', )
非規(guī)則拼圖2.png

畫你的第一張地圖

四種投影:

等經(jīng)緯度投影(PlateCarree)(默認(rèn))、蘭伯特投影(Lambert)、墨卡托投影(Mercator)、極射赤面投影

class cartopy.crs.Projection,定義投影類

1)等經(jīng)緯度投影

class cartopy.crs.PlateCarree(central_longitude=0.0, globe=None)

Parameters:

central_longitude:中心經(jīng)度

2)蘭伯特投影

class cartopy.crs.LambertConformal(central_longitude=-96.0, central_latitude=39.0, false_easting=0.0, false_northing=0.0, secant_latitudes=None, standard_parallels=None, globe=None, cutoff=-30)
3)墨卡托投影

class cartopy.crs.Mercator(central_longitude=0.0, min_latitude=-80.0, max_latitude=84.0, globe=None, latitude_true_scale=0.0)

4)極射赤面投影

class cartopy.crs.Stereographic(central_latitude=0.0, central_longitude=0.0, false_easting=0.0, false_northing=0.0, true_scale_latitude=None, globe=None)

在上述基礎(chǔ)知識鋪墊后,你就可以畫你的第一張地圖了!讓我們先畫一張世界地圖吧!

1)導(dǎo)入本次畫圖所需要的包

import matplotlib.pyplot as plt
import cartopy.crs as ccrs

2)畫一張世界地圖

fig = plt.figure(figsize=(16, 8))  # 設(shè)置畫布

proj = ccrs.PlateCarree()  # 創(chuàng)建一個投影
ax = plt.axes(projection=proj)  # 創(chuàng)建一個畫紙, 并指明投影類型
ax.coastlines()  # 畫海岸線
plt.show()  # 顯示圖像
世界地圖.png

在上述代碼中,你就畫出了第一張地圖,但是這張圖還明顯缺少刻度信息,在下面的代碼中將添加坐標(biāo)信息。
(部分語句參考鏈接:https://matplotlib.org/3.3.2/api/ticker_api.html)
1)導(dǎo)包

import numpy as np
import matplotlib.pyplot as plt
import cartopy.crs as ccrs
from cartopy.mpl.ticker import LongitudeFormatter, LatitudeFormatter  # 刻度格式
import matplotlib.ticker as mticker  # 添加網(wǎng)格線

2)畫圖

fig = plt.figure(figsize=(16, 8))  # 設(shè)置畫布

proj = ccrs.PlateCarree()  # 創(chuàng)建一個投影
ax = plt.axes(projection=proj)  # 創(chuàng)建一個畫紙, 并指明投影類型
ax.coastlines()  # 畫海岸線


# 設(shè)置范圍
extent = [-180, 180, -90, 90]

# 設(shè)置刻度
dx = 60
dy = 30
offset = min(dx, dy)

xticks = np.arange(extent[0], extent[1] + offset, dx)  # 創(chuàng)建x軸刻度
yticks = np.arange(extent[2], extent[3] + offset, dy)  # 創(chuàng)建y軸刻度


# 添加刻度
ax.set_xticks(xticks, crs=ccrs.PlateCarree())  
ax.set_yticks(yticks, crs=ccrs.PlateCarree()) 

# 設(shè)置刻度字體大小

# 方法一
# ax.tick_params(axis='x', labelsize=15) 
ax.tick_params(labelsize=15) 

# 方法二
# plt.xticks(fontsize = 15)
# plt.yticks(fontsize = 15)

# 設(shè)置刻度格式為經(jīng)緯度格式
ax.xaxis.set_major_formatter(LongitudeFormatter(zero_direction_label=True))  
ax.yaxis.set_major_formatter(LatitudeFormatter())

# 設(shè)置畫圖范圍
ax.set_extent(extent)

# 添加網(wǎng)格線
# gl = ax.gridlines(crs=proj, draw_labels=False, linewidth=1, linestyle=':', color='k', alpha=0.8)
# gl.xlocator = mticker.FixedLocator(xticks)  # 設(shè)置
# gl.ylocator = mticker.FixedLocator(yticks)
添加坐標(biāo)后的第一張地圖.png

將刻度設(shè)置封裝為函數(shù)方便調(diào)用

# # 函數(shù)封裝,方便調(diào)用

def add_geo_ticks(ax, proj, extent, lat_span = 5, lon_span = 5):
    '''
        在圖中添加刻度信息
        
        Version 1.0
    
    '''

    from cartopy.mpl.ticker import LongitudeFormatter, LatitudeFormatter  # 刻度格式

    
    offset = min(lon_span, lat_span)

    xticks = np.arange(extent[0], extent[1] + offset, lon_span)  # 創(chuàng)建x軸刻度
    yticks = np.arange(extent[2], extent[3] + offset, lat_span)  # 創(chuàng)建y軸刻度
    
    # 添加刻度
    ax.set_xticks(xticks, crs=proj)  
    ax.set_yticks(yticks, crs=proj) 
    
    # 設(shè)置刻度格式為經(jīng)緯度格式
    ax.xaxis.set_major_formatter(LongitudeFormatter(zero_direction_label=True))  
    ax.yaxis.set_major_formatter(LatitudeFormatter())

    # 設(shè)置畫圖范圍
    ax.set_extent(extent)

上述代碼變?yōu)椋?/p>

fig = plt.figure(figsize=(16, 8))  # 設(shè)置畫布

proj = ccrs.PlateCarree()  # 創(chuàng)建一個投影
ax = plt.axes(projection=proj)  # 創(chuàng)建一個畫紙, 并指明投影類型
ax.coastlines()  # 畫海岸線


# 設(shè)置范圍
extent = [-180, 180, -90, 90]

# 添加地理坐標(biāo)
add_geo_ticks(ax, proj, extent, lat_span = 30, lon_span = 60)

# 設(shè)置刻度字體大小
ax.tick_params(labelsize=15) 

添加地理特征
cartopy包里自帶了七種特征,它們分別如下:
(參考鏈接:https://scitools.org.uk/cartopy/docs/latest/matplotlib/feature_interface.html#cartopy.feature.LAND

地理特征.png

add_feature()函數(shù)說明:
參考(https://scitools.org.uk/cartopy/docs/v0.16/matplotlib/geoaxes.html#cartopy.mpl.geoaxes.GeoAxes.add_feature)

add_feature()函數(shù)說明.png

添加地理特征代碼:

import cartopy.feature as cfeature

fig = plt.figure(figsize=(16, 8))  # 設(shè)置畫布

proj = ccrs.PlateCarree()  # 創(chuàng)建一個投影
ax = plt.axes(projection=proj)  # 創(chuàng)建一個畫紙, 并指明投影類型
ax.coastlines()  # 畫海岸線


# 設(shè)置范圍
extent = [-180, 180, -90, 90]

# 添加地理坐標(biāo)
add_geo_ticks(ax, proj, extent, lat_span = 30, lon_span = 60)

# 設(shè)置刻度字體大小
ax.tick_params(labelsize=15) 


ax.add_feature(cfeature.LAND)
ax.add_feature(cfeature.OCEAN)
ax.add_feature(cfeature.COASTLINE)
ax.add_feature(cfeature.BORDERS, linestyle=':')
ax.add_feature(cfeature.LAKES, alpha=0.5)
ax.add_feature(cfeature.RIVERS)
添加地理特征后的地圖.png
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

友情鏈接更多精彩內(nèi)容