58同城做网站被骗,搜索引擎营销原理,建设个人网银登录入口,中铁建设集团有限公司待遇上期读取soda#xff0c;并subplot
但是存在一些不完美#xff0c;本期修饰
本期内容
subplots_adjust布局调整
1#xff1a;未调整布局的 2#xff1a;调整布局 往期推荐
【python海洋专题一】查看数据nc文件的属性并输出属性到txt文件
【python海洋专题二】读取水深…上期读取soda并subplot
但是存在一些不完美本期修饰
本期内容
subplots_adjust布局调整
1未调整布局的 2调整布局 往期推荐
【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海洋专题十九】找范围的语句进阶版本
参考文献及其在本文中的作用
1matplotlib之pyplot模块——调整子图布局subplots_adjust、tight_layout_tight_layout函数_mighty13的博客-CSDN博客
全文代码
1# -*- 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, namemy_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(rE:\data\soda\soda3.12.2_5dy_ocean_reg_2017.nc)
print(f1)
# # 提取经纬度这样就不需要重复读取
lat f1[yt_ocean].data
lon f1[xt_ocean].data
ssh f1[ssh].data
time f1[time].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]
# # # 画图网格
lon1 lon[ln1:ln2]
lat1 lat[la1:la2]
X, Y np.meshgrid(lon1, lat1)
ssh_aim ssh[:, la1:la2, ln1:ln2]
# # ----------对时间维度求平均 得到春夏秋冬的ssh------------------
ssh_spr_mean np.mean(ssh_aim[2:5, :, :], axis0)
ssh_sum_mean np.mean(ssh_aim[5:8, :, :], axis0)
ssh_atu_mean np.mean(ssh_aim[8:11, :, :], axis0)
ssh_win_mean (ssh_aim[0, :, :]ssh_aim[1, :, :]ssh_aim[11, :, :])/3
# # -------------# plot ------------
scale 50m
plt.rcParams[font.sans-serif] [Times New Roman] # 设置整体的字体为Times New Roman
fig plt.figure(dpi300, figsize(3, 2), facecolorw, edgecolorblue) # 设置一个画板将其返还给fig
# 通过subplots_adjust()设置间距配置
fig.subplots_adjust(left0.1, bottom0.05, right0.9, top0.95, wspace0.05, hspace0.1)
# --------第一个子图----------
ax fig.add_subplot(2, 2, 1, projectionccrs.PlateCarree(central_longitude180))
ax.set_extent([100, 125, 0, 25], crsccrs.PlateCarree()) # 设置显示范围
land feature.NaturalEarthFeature(physical, land, scale, edgecolorface,facecolorfeature.COLORS[land])
ax.add_feature(land, facecolor0.6)
ax.add_feature(feature.COASTLINE.with_scale(50m), lw0.3) # 添加海岸线关键字lw设置线宽; lifestyle设置线型
cs ax.contourf(X, Y, ssh_spr_mean, extendboth, cmapcmap_r2, levelsnp.linspace(0, 1, 50),transformccrs.PlateCarree()) #
# ------color-bar设置------------
cb plt.colorbar(cs, axax, extendboth, orientationvertical, ticks[0, 0.2, 0.4, 0.6, 0.8, 1.0]) #
# cb.set_label(SSH, fontsize4, colork) # 设置color-bar的标签字体及其大小
cb.ax.tick_params(labelsize4, directionin, length1.5, colork) # 设置color-bar刻度字体大小。
# --------------添加标题----------------
# ax.set_title(SSH, fontsize4)
# ------------------利用Formatter格式化刻度标签-----------------
ax.set_xticks(np.arange(100, 126, 5), crsccrs.PlateCarree()) # 添加经纬度
ax.set_xticklabels(np.arange(100, 126, 5), fontsize4)
ax.set_yticks(np.arange(0, 26, 5), crsccrs.PlateCarree())
ax.set_yticklabels(np.arange(0, 26, 5), fontsize4)
ax.xaxis.set_major_formatter(LongitudeFormatter())
ax.yaxis.set_major_formatter(LatitudeFormatter())
ax.tick_params(axisx, topTrue, whichmajor, directionin, length2, width0.8, labelsize4, pad1,colork) # 刻度样式
ax.tick_params(axisy, rightTrue, whichmajor, directionin, length2, width0.8, labelsize4, pad1,colork) # 更改刻度指向为朝内颜色设置为蓝色
gl ax.gridlines(crsccrs.PlateCarree(), draw_labelsFalse, xlocsnp.arange(100, 126, 5), ylocsnp.arange(0, 26, 5),linewidth0.25, linestyle--, colork, alpha0.8) # 添加网格线
gl.top_labels, gl.bottom_labels, gl.right_labels, gl.left_labels False, False, False, False
# --------第二个子图----------
ax fig.add_subplot(2, 2, 2, projectionccrs.PlateCarree(central_longitude180))
ax.set_extent([100, 125, 0, 25], crsccrs.PlateCarree()) # 设置显示范围
land feature.NaturalEarthFeature(physical, land, scale, edgecolorface,facecolorfeature.COLORS[land])
ax.add_feature(land, facecolor0.6)
ax.add_feature(feature.COASTLINE.with_scale(50m), lw0.3) # 添加海岸线关键字lw设置线宽; lifestyle设置线型
cs ax.contourf(X, Y, ssh_sum_mean, extendboth, cmapcmap_r2, levelsnp.linspace(0, 1, 50),transformccrs.PlateCarree()) #
# ------color-bar设置------------
cb plt.colorbar(cs, axax, extendboth, orientationvertical, ticks[0, 0.2, 0.4, 0.6, 0.8, 1.0]) #
# cb.set_label(SSH, fontsize4, colork) # 设置color-bar的标签字体及其大小
cb.ax.tick_params(labelsize4, directionin, length1.5, colork) # 设置color-bar刻度字体大小。
# --------------添加标题----------------
# ax.set_title(SSH, fontsize4)
# ------------------利用Formatter格式化刻度标签-----------------
ax.set_xticks(np.arange(100, 126, 5), crsccrs.PlateCarree()) # 添加经纬度
ax.set_xticklabels(np.arange(100, 126, 5), fontsize4)
ax.set_yticks(np.arange(0, 26, 5), crsccrs.PlateCarree())
ax.set_yticklabels(np.arange(0, 26, 5), fontsize4)
ax.xaxis.set_major_formatter(LongitudeFormatter())
ax.yaxis.set_major_formatter(LatitudeFormatter())
ax.tick_params(axisx, topTrue, whichmajor, directionin, length2, width0.8, labelsize4, pad1,colork) # 刻度样式
ax.tick_params(axisy, rightTrue, whichmajor, directionin, length2, width0.8, labelsize4, pad1,colork) # 更改刻度指向为朝内颜色设置为蓝色
gl ax.gridlines(crsccrs.PlateCarree(), draw_labelsFalse, xlocsnp.arange(100, 126, 5), ylocsnp.arange(0, 26, 5),linewidth0.25, linestyle--, colork, alpha0.8) # 添加网格线
gl.top_labels, gl.bottom_labels, gl.right_labels, gl.left_labels False, False, False, False
# --------第三个子图----------
ax fig.add_subplot(2, 2, 3, projectionccrs.PlateCarree(central_longitude180))
ax.set_extent([100, 125, 0, 25], crsccrs.PlateCarree()) # 设置显示范围
land feature.NaturalEarthFeature(physical, land, scale, edgecolorface,facecolorfeature.COLORS[land])
ax.add_feature(land, facecolor0.6)
ax.add_feature(feature.COASTLINE.with_scale(50m), lw0.3) # 添加海岸线关键字lw设置线宽; lifestyle设置线型
cs ax.contourf(X, Y, ssh_atu_mean, extendboth, cmapcmap_r2, levelsnp.linspace(0, 1, 50),transformccrs.PlateCarree()) #
# ------color-bar设置------------
cb plt.colorbar(cs, axax, extendboth, orientationvertical, ticks[0, 0.2, 0.4, 0.6, 0.8, 1.0]) #
# cb.set_label(SSH, fontsize4, colork) # 设置color-bar的标签字体及其大小
cb.ax.tick_params(labelsize4, directionin, length1.5, colork) # 设置color-bar刻度字体大小。
# --------------添加标题----------------
# ax.set_title(SSH, fontsize4)
# ------------------利用Formatter格式化刻度标签-----------------
ax.set_xticks(np.arange(100, 126, 5), crsccrs.PlateCarree()) # 添加经纬度
ax.set_xticklabels(np.arange(100, 126, 5), fontsize4)
ax.set_yticks(np.arange(0, 26, 5), crsccrs.PlateCarree())
ax.set_yticklabels(np.arange(0, 26, 5), fontsize4)
ax.xaxis.set_major_formatter(LongitudeFormatter())
ax.yaxis.set_major_formatter(LatitudeFormatter())
ax.tick_params(axisx, topTrue, whichmajor, directionin, length2, width0.8, labelsize4, pad1,colork) # 刻度样式
ax.tick_params(axisy, rightTrue, whichmajor, directionin, length2, width0.8, labelsize4, pad1,colork) # 更改刻度指向为朝内颜色设置为蓝色
gl ax.gridlines(crsccrs.PlateCarree(), draw_labelsFalse, xlocsnp.arange(100, 126, 5), ylocsnp.arange(0, 26, 5),linewidth0.25, linestyle--, colork, alpha0.8) # 添加网格线
gl.top_labels, gl.bottom_labels, gl.right_labels, gl.left_labels False, False, False, False
# --------第四个子图----------
ax fig.add_subplot(2, 2, 4, projectionccrs.PlateCarree(central_longitude180))
ax.set_extent([100, 125, 0, 25], crsccrs.PlateCarree()) # 设置显示范围
land feature.NaturalEarthFeature(physical, land, scale, edgecolorface,facecolorfeature.COLORS[land])
ax.add_feature(land, facecolor0.6)
ax.add_feature(feature.COASTLINE.with_scale(50m), lw0.3) # 添加海岸线关键字lw设置线宽; lifestyle设置线型
cs ax.contourf(X, Y, ssh_win_mean, extendboth, cmapcmap_r2, levelsnp.linspace(0, 1, 50),transformccrs.PlateCarree()) #
# ------color-bar设置------------
cb plt.colorbar(cs, axax, extendboth, orientationvertical, ticks[0, 0.2, 0.4, 0.6, 0.8, 1.0]) #
# cb.set_label(SSH, fontsize4, colork) # 设置color-bar的标签字体及其大小
cb.ax.tick_params(labelsize4, directionin, length1.5, colork) # 设置color-bar刻度字体大小。
# --------------添加标题----------------
# ax.set_title(SSH, fontsize4)
# ------------------利用Formatter格式化刻度标签-----------------
ax.set_xticks(np.arange(100, 126, 5), crsccrs.PlateCarree()) # 添加经纬度
ax.set_xticklabels(np.arange(100, 126, 5), fontsize4)
ax.set_yticks(np.arange(0, 26, 5), crsccrs.PlateCarree())
ax.set_yticklabels(np.arange(0, 26, 5), fontsize4)
ax.xaxis.set_major_formatter(LongitudeFormatter())
ax.yaxis.set_major_formatter(LatitudeFormatter())
ax.tick_params(axisx, topTrue, whichmajor, directionin, length2, width0.8, labelsize4, pad1,colork) # 刻度样式
ax.tick_params(axisy, rightTrue, whichmajor, directionin, length2, width0.8, labelsize4, pad1,colork) # 更改刻度指向为朝内颜色设置为蓝色
gl ax.gridlines(crsccrs.PlateCarree(), draw_labelsFalse, xlocsnp.arange(100, 126, 5), ylocsnp.arange(0, 26, 5),linewidth0.25, linestyle--, colork, alpha0.8) # 添加网格线
gl.top_labels, gl.bottom_labels, gl.right_labels, gl.left_labels False, False, False, False
# -------添加子图的大标题--------
plt.suptitle(SSH, x0.48, y0.995, fontsize6, colorred)
plt.savefig(SSH_2.jpg, dpi600, bbox_inchestight, pad_inches0.1) # 输出地图并设置边框空白紧密
plt.show()