【python海洋专题三十】画南海115°E的温度剖面图
【python海洋专题一】查看数据nc文件的属性并输出属性到txt文件
【python海洋专题二】读取水深nc文件并水深地形图
【python海洋专题三】图像修饰之画布和坐标轴
【Python海洋专题四】之水深地图图像修饰
【Python海洋专题五】之水深地形图海岸填充
【Python海洋专题六】之Cartopy画地形水深图
【python海洋专题】测试数据
【Python海洋专题七】Cartopy画地形水深图的陆地填充
【python海洋专题八】Cartopy画地形水深图的contourf填充间隔数调整
【python海洋专题九】Cartopy画地形等深线图
【python海洋专题十】Cartopy画特定区域的地形等深线图
【python海洋专题十一】colormap调色
【python海洋专题十二】年平均的南海海表面温度图
【python海洋专题十三】读取多个nc文件画温度季节变化图
【python海洋专题十四】读取多个盐度nc数据画盐度季节变化图
【python海洋专题十五】给colorbar加单位
【python海洋专题十六】对大陆周边的数据进行临近插值
【python海洋专题十七】读取几十年的OHC数据,画四季图
【python海洋专题十八】读取Soda数据,画subplot的海表面高度四季变化图
【python海洋专题十九】找范围的语句进阶版本
【python海洋专题二十】subplots_adjust布局调整
【python海洋专题二十一】subplots共用一个colorbar
【python海洋专题二十二】在海图上text
【python海洋专题二十三】共用坐标轴
【python海洋专题二十四】南海年平均海流图
【python海洋专题二十五】给南海年平均海流+scale
【python海洋专题二十六】南海海流流速图
【python海洋专题二十七】南海四季海流图
【python海洋专题二十八】南海四季海流流速图
【python海洋专题二十九】读取CTD文件数据并画温度点剖面图
【MATLAB海洋专题】历史汇总
【matlab程序】图片平面制作||文末点赞分享||海报制作等
大佬推荐一下物理海洋教材吧?
【matlab海洋专题】高级玫瑰图–风速风向频率玫瑰图–此图细节较多
【海洋科普】沉积物分为粘性沉积物和非粘性沉积物
【海洋科普】黄渤海地理介绍
【科普知识】海洋尺度图和解释
【海洋科普】海洋环流与等高线岩特征联系
# -*- coding: utf-8 -*-
# ---导入数据读取和处理的模块-------
from netCDF4 import Dataset
from pathlib import Path
import xarray as xr
import numpy as np
# ------导入画图相关函数--------
import matplotlib.pyplot as plt
from matplotlib.font_manager import FontProperties
import matplotlib.ticker as ticker
from cartopy import mpl
import cartopy.crs as ccrs
import cartopy.feature as feature
from cartopy.mpl.ticker import LongitudeFormatter, LatitudeFormatter
from pylab import *
# -----导入颜色包---------
import seaborn as sns
from matplotlib import cm
import palettable
from palettable.cmocean.diverging import Delta_4
from palettable.colorbrewer.sequential import GnBu_9
from palettable.colorbrewer.sequential import Blues_9
from palettable.scientific.diverging import Roma_20
from palettable.cmocean.diverging import Delta_20
from palettable.scientific.diverging import Roma_20
from palettable.cmocean.diverging import Balance_20
from matplotlib.colors import ListedColormap
# -------导入插值模块-----
from scipy.interpolate import interp1d # 引入scipy中的一维插值库
from scipy.interpolate import griddata # 引入scipy中的二维插值库
from scipy.interpolate import interp2d
# ----define reverse_colourmap定义颜色的反向函数----
def reverse_colourmap(cmap, name='my_cmap_r'):
reverse = []
k = []
for key in cmap._segmentdata:
k.append(key)
channel = cmap._segmentdata[key]
data = []
for t in channel:
data.append((1 - t[0], t[2], t[1]))
reverse.append(sorted(data))
LinearL = dict(zip(k, reverse))
my_cmap_r = mpl.colors.LinearSegmentedColormap(name, LinearL)
return my_cmap_r
# ---colormap的读取和反向----
cmap01 = Balance_20.mpl_colormap
cmap0 = Blues_9.mpl_colormap
cmap_r = reverse_colourmap(cmap0)
cmap1 = GnBu_9.mpl_colormap
cmap_r1 = reverse_colourmap(cmap1)
cmap2 = Roma_20.mpl_colormap
cmap_r2 = reverse_colourmap(cmap2)
# ---read_data---
f1 = xr.open_dataset(r'E:\data\soda\soda3.12.2_5dy_ocean_reg_2017.nc')
print(f1)
# # 提取经纬度(这样就不需要重复读取)
lat = f1['yt_ocean'].data
lon = f1['xt_ocean'].data
temp = f1['temp'].data
# print(time)
# # -------- find scs 's temp-----------
ln1 = np.where(lon >= 100)[0][0]
ln2 = np.where(lon >= 125)[0][0]
la1 = np.where(lat >= 0)[0][0]
la2 = np.where(lat >= 25)[0][0]
# time_all=[(time>=1058760) & (time<=1059096)] #13-27 Oct
# # # 画图网格
lon1 = lon[ln1:ln2]
lat1 = lat[la1:la2]
X, Y = np.meshgrid(lon1, lat1)
temp = temp[6, 1, la1:la2, ln1:ln2]
# ----plot--------------
scale = '50m'
plt.rcParams['font.sans-serif'] = ['Times New Roman'] # 设置整体的字体为Times New Roman
# 设置显示中文字体
mpl.rcParams["font.sans-serif"] = ["SimHei"]
mpl.rcParams["mathtext.fontset"] = 'cm' # 数学文字字体
mpl.rcParams["font.size"] = 12 # 字体大小
mpl.rcParams["axes.linewidth"] = 1 # 轴线边框粗细(默认的太粗了)
fig = plt.figure(dpi=300, figsize=(3, 2), facecolor='w', edgecolor='blue') # 设置一个画板,将其返还给fig
ax = fig.add_axes([0.05, 0.08, 0.92, 0.8], projection=ccrs.PlateCarree(central_longitude=180))
ax.set_extent([100, 123, 0, 25], crs=ccrs.PlateCarree()) # 设置显示范围
land = feature.NaturalEarthFeature('physical', 'land', scale, edgecolor='face',
facecolor=feature.COLORS['land'])
ax.add_feature(land, facecolor='0.6')
ax.add_feature(feature.COASTLINE.with_scale('50m'), lw=0.3) # 添加海岸线:关键字lw设置线宽; lifestyle设置线型
cf = ax.contourf(X, Y, temp, extend='both', zorder=0, cmap=cmap_r2, levels=np.linspace(20, 30, 50),
transform=ccrs.PlateCarree()) #
# ------color-bar设置------------
cb = plt.colorbar(cf, ax=ax, extend='both', orientation='vertical', ticks=np.linspace(20, 30, 5))
cb.ax.tick_params(labelsize=4, direction='in') # 设置color-bar刻度字体大小。
# --------------添加标题----------------
ax.set_title('南海表层温度分布', loc="center", fontsize=6, pad=1)
# ------------------利用Formatter格式化刻度标签-----------------
ax.set_xticks(np.arange(100, 123, 4), crs=ccrs.PlateCarree()) # 添加经纬度
ax.set_xticklabels(np.arange(100, 123, 4), fontsize=4)
ax.set_yticks(np.arange(0, 25, 2), crs=ccrs.PlateCarree())
ax.set_yticklabels(np.arange(0, 25, 2), fontsize=4)
ax.xaxis.set_major_formatter(LongitudeFormatter())
ax.yaxis.set_major_formatter(LatitudeFormatter())
ax.tick_params(axis='x', top=True, which='major', direction='in', length=3, width=0.8, labelsize=5, pad=0.8,
color='k') # 刻度样式 pad代表标题离轴的远近
ax.tick_params(axis='y', right=True, which='major', direction='in', length=3, width=0.8, labelsize=5, pad=0.8,
color='k') # 更改刻度指向为朝内,颜色设置为蓝色
gl = ax.gridlines(crs=ccrs.PlateCarree(), draw_labels=False, xlocs=np.arange(100, 123, 4), ylocs=np.arange(0, 25, 2),
linewidth=0.25, linestyle='--', color='k', alpha=0.8) # 添加网格线
gl.top_labels, gl.bottom_labels, gl.right_labels, gl.left_labels = False, False, False, False
#
plt.plot([115, 115], [0, 25], color='blue', linewidth=0.5, marker='.', transform=ccrs.PlateCarree())
plt.savefig('sst1.jpg', dpi=600, bbox_inches='tight', pad_inches=0.1) # 输出地图,并设置边框空白紧密
plt.show()
# -*- coding: utf-8 -*-
# ---导入数据读取和处理的模块-------
from netCDF4 import Dataset
from pathlib import Path
import xarray as xr
import numpy as np
# ------导入画图相关函数--------
import matplotlib.pyplot as plt
from matplotlib.font_manager import FontProperties
import matplotlib.ticker as ticker
from cartopy import mpl
import cartopy.crs as ccrs
import cartopy.feature as feature
from cartopy.mpl.ticker import LongitudeFormatter, LatitudeFormatter
from pylab import *
# -----导入颜色包---------
import seaborn as sns
from matplotlib import cm
import palettable
from palettable.cmocean.diverging import Delta_4
from palettable.colorbrewer.sequential import GnBu_9
from palettable.colorbrewer.sequential import Blues_9
from palettable.scientific.diverging import Roma_20
from palettable.cmocean.diverging import Delta_20
from palettable.scientific.diverging import Roma_20
from palettable.cmocean.diverging import Balance_20
from matplotlib.colors import ListedColormap
# -------导入插值模块-----
from scipy.interpolate import interp1d # 引入scipy中的一维插值库
from scipy.interpolate import griddata # 引入scipy中的二维插值库
from scipy.interpolate import interp2d
# ----define reverse_colourmap定义颜色的反向函数----
def reverse_colourmap(cmap, name='my_cmap_r'):
reverse = []
k = []
for key in cmap._segmentdata:
k.append(key)
channel = cmap._segmentdata[key]
data = []
for t in channel:
data.append((1 - t[0], t[2], t[1]))
reverse.append(sorted(data))
LinearL = dict(zip(k, reverse))
my_cmap_r = mpl.colors.LinearSegmentedColormap(name, LinearL)
return my_cmap_r
# ---colormap的读取和反向----
cmap01 = Balance_20.mpl_colormap
cmap0 = Blues_9.mpl_colormap
cmap_r = reverse_colourmap(cmap0)
cmap1 = GnBu_9.mpl_colormap
cmap_r1 = reverse_colourmap(cmap1)
cmap2 = Roma_20.mpl_colormap
cmap_r2 = reverse_colourmap(cmap2)
# ---read_data---
f1 = xr.open_dataset(r'E:\data\soda\soda3.12.2_5dy_ocean_reg_2017.nc')
print(f1)
# # 提取经纬度(这样就不需要重复读取)
lat = f1['yt_ocean'].data
lon = f1['xt_ocean'].data
temp = f1['temp'].data
depth = f1['st_ocean'].data
time = f1['time'].data
print(depth)
# # -------- find scs 's temp-----------
ln1 = np.where(lon >= 115)[0][0]
ln2 = np.where(lon >= 116)[0][0]
la1 = np.where(lat >= 0)[0][0]
la2 = np.where(lat >= 25)[0][0]
dep = np.where(depth >= 1000)[0][0]
print(la1, la2, ln1, ln2, dep)
# time_all=[(time>=1058760) & (time<=1059096)] #13-27 Oct
tem_aim = temp[6, 0:28, la1:la2, 230]
print(tem_aim)
depth1 = -depth[0:28]
lat1 = lat[la1:la2]
#
X, Y = np.meshgrid(lat1, depth1)
# # ----plot--------------
scale = '50m'
plt.rcParams['font.sans-serif'] = ['Times New Roman'] # 设置整体的字体为Times New Roman
# 设置显示中文字体
mpl.rcParams["font.sans-serif"] = ["SimHei"]
mpl.rcParams["mathtext.fontset"] = 'cm' # 数学文字字体
mpl.rcParams["font.size"] = 12 # 字体大小
mpl.rcParams["axes.linewidth"] = 1 # 轴线边框粗细(默认的太粗了)
fig = plt.figure(dpi=300, figsize=(3, 2), facecolor='w', edgecolor='blue') # 设置一个画板,将其返还给fig
ax = fig.add_axes([0.05, 0.08, 0.92, 0.8])
cs = ax.contourf(X, Y, tem_aim, extend='both', zorder=0, cmap=cmap_r2, levels=np.linspace(0, 31, 50)) #
# ------color-bar设置------------
cb = plt.colorbar(cs, ax=ax, extend='both', orientation='vertical', ticks=np.linspace(0, 30, 7))
cb.ax.set_title('SST', fontsize=6, color='r')
cb.ax.tick_params(labelsize=4, direction='in') # 设置color-bar刻度字体大小。
ax.contour(X, Y, tem_aim, levels=np.linspace(0, 31, 10), colors='w', linestyles='-',
linewidths=0.2)
ax.contour(X, Y, tem_aim, levels=[16], colors='r', linestyles='-',
linewidths=0.5)
# --------------添加标题----------------
ax.set_title('南海115°E的温度剖面图', loc="center", fontsize=6, pad=1)
# ------------------利用Formatter格式化刻度标签-----------------
ax.set_xticks(np.arange(0, 25, 5)) # 添加经纬度
ax.set_xticklabels(np.arange(0, 25, 5), fontsize=4)
ax.xaxis.set_major_formatter(LatitudeFormatter())
font = {
'color': 'k',
'weight': 'normal',
'size': 6,
}
ax.set_ylabel("深度(米)", fontdict=font, backgroundcolor='w')
ax.set_yticks(np.arange(-1000, 1, 100))
ax.set_yticklabels(np.arange(-1000, 1, 100), fontsize=4)
ax.tick_params(axis='x', top=True, which='major', direction='in', length=3, width=0.8, labelsize=5, pad=0.8,
color='k') # 刻度样式 pad代表标题离轴的远近
ax.tick_params(axis='y', right=True, which='major', direction='in', length=3, width=0.8, labelsize=5, pad=0.8,
color='k') # 更改刻度指向为朝内,颜色设置为蓝色
plt.savefig('temp_profile_3.jpg', dpi=600, bbox_inches='tight', pad_inches=0.1) # 输出地图,并设置边框空白紧密
plt.show()