oafuncs 0.0.81__py2.py3-none-any.whl → 0.0.83__py2.py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (44) hide show
  1. oafuncs/data_store/OAFuncs.png +0 -0
  2. oafuncs/oa_cmap.py +1 -0
  3. oafuncs/oa_data.py +107 -28
  4. oafuncs/oa_down/__init__.py +5 -4
  5. oafuncs/oa_down/hycom_3hourly.py +152 -35
  6. oafuncs/oa_down/user_agent.py +34 -0
  7. oafuncs/oa_draw.py +165 -103
  8. oafuncs/oa_file.py +66 -53
  9. oafuncs/oa_help.py +19 -16
  10. oafuncs/oa_nc.py +82 -114
  11. oafuncs-0.0.83.dist-info/METADATA +91 -0
  12. oafuncs-0.0.83.dist-info/RECORD +26 -0
  13. oafuncs/oa_down/test.py +0 -151
  14. oafuncs/oa_s/__init__.py +0 -23
  15. oafuncs/oa_s/oa_cmap.py +0 -163
  16. oafuncs/oa_s/oa_data.py +0 -187
  17. oafuncs/oa_s/oa_draw.py +0 -451
  18. oafuncs/oa_s/oa_file.py +0 -332
  19. oafuncs/oa_s/oa_help.py +0 -39
  20. oafuncs/oa_s/oa_nc.py +0 -410
  21. oafuncs/oa_s/oa_python.py +0 -107
  22. oafuncs - /321/205/320/231/320/277/321/206/320/254/320/274/__init__.py" +0 -26
  23. oafuncs - /321/205/320/231/320/277/321/206/320/254/320/274/oa_cmap.py" +0 -163
  24. oafuncs - /321/205/320/231/320/277/321/206/320/254/320/274/oa_data.py" +0 -187
  25. oafuncs - /321/205/320/231/320/277/321/206/320/254/320/274/oa_down/__init__.py" +0 -20
  26. oafuncs - /321/205/320/231/320/277/321/206/320/254/320/274/oa_down/hycom_3hourly.py" +0 -1176
  27. oafuncs - /321/205/320/231/320/277/321/206/320/254/320/274/oa_down/literature.py" +0 -332
  28. oafuncs - /321/205/320/231/320/277/321/206/320/254/320/274/oa_down/test_ua.py" +0 -151
  29. oafuncs - /321/205/320/231/320/277/321/206/320/254/320/274/oa_draw.py" +0 -451
  30. oafuncs - /321/205/320/231/320/277/321/206/320/254/320/274/oa_file.py" +0 -332
  31. oafuncs - /321/205/320/231/320/277/321/206/320/254/320/274/oa_help.py" +0 -39
  32. oafuncs - /321/205/320/231/320/277/321/206/320/254/320/274/oa_nc.py" +0 -410
  33. oafuncs - /321/205/320/231/320/277/321/206/320/254/320/274/oa_python.py" +0 -107
  34. oafuncs - /321/205/320/231/320/277/321/206/320/254/320/274/oa_sign/__init__.py" +0 -21
  35. oafuncs - /321/205/320/231/320/277/321/206/320/254/320/274/oa_sign/meteorological.py" +0 -168
  36. oafuncs - /321/205/320/231/320/277/321/206/320/254/320/274/oa_sign/ocean.py" +0 -158
  37. oafuncs - /321/205/320/231/320/277/321/206/320/254/320/274/oa_sign/scientific.py" +0 -139
  38. oafuncs - /321/205/320/231/320/277/321/206/320/254/320/274/oa_tool/__init__.py" +0 -18
  39. oafuncs - /321/205/320/231/320/277/321/206/320/254/320/274/oa_tool/email.py" +0 -114
  40. oafuncs-0.0.81.dist-info/METADATA +0 -918
  41. oafuncs-0.0.81.dist-info/RECORD +0 -51
  42. {oafuncs-0.0.81.dist-info → oafuncs-0.0.83.dist-info}/LICENSE.txt +0 -0
  43. {oafuncs-0.0.81.dist-info → oafuncs-0.0.83.dist-info}/WHEEL +0 -0
  44. {oafuncs-0.0.81.dist-info → oafuncs-0.0.83.dist-info}/top_level.txt +0 -0
@@ -1,451 +0,0 @@
1
- #!/usr/bin/env python
2
- # coding=utf-8
3
- '''
4
- Author: Liu Kun && 16031215@qq.com
5
- Date: 2024-09-17 17:26:11
6
- LastEditors: Liu Kun && 16031215@qq.com
7
- LastEditTime: 2024-11-21 13:10:47
8
- FilePath: \\Python\\My_Funcs\\OAFuncs\\oafuncs\\oa_draw.py
9
- Description:
10
- EditPlatform: vscode
11
- ComputerInfo: XPS 15 9510
12
- SystemInfo: Windows 11
13
- Python Version: 3.11
14
- '''
15
-
16
-
17
- import math
18
- import warnings
19
-
20
- import cartopy.crs as ccrs
21
- import cartopy.feature as cfeature
22
- import matplotlib as mpl
23
- import matplotlib.pyplot as plt
24
- import numpy as np
25
- import xarray as xr
26
- from cartopy.mpl.ticker import LatitudeFormatter, LongitudeFormatter
27
- from mpl_toolkits.axes_grid1 import make_axes_locatable
28
-
29
- __all__ = ['fig_minus', 'create_gif', 'xy2lonlat', 'plot_contourf', 'plot_contourf_lonlat', 'plot_quiver', 'plot_contourf_cartopy']
30
-
31
- warnings.filterwarnings('ignore')
32
-
33
- def fig_minus(ax_x=None, ax_y=None, cbar=None, decimal=None, add_space=False):
34
- '''
35
- Description: 将坐标轴刻度中的负号替换为减号
36
-
37
- param {*} ax_x : x轴
38
- param {*} ax_y : y轴
39
- param {*} cbar : colorbar
40
- param {*} decimal : 小数位数
41
- param {*} add_space : 是否在非负数前面加空格
42
-
43
- return {*} ax_x or ax_y or cbar
44
- '''
45
- if ax_x is not None:
46
- current_ticks = ax_x.get_xticks()
47
- if ax_y is not None:
48
- current_ticks = ax_y.get_yticks()
49
- if cbar is not None:
50
- current_ticks = cbar.get_ticks()
51
- # 先判断是否需要加空格,如果要,先获取需要加的索引
52
- if add_space:
53
- index = 0
54
- for _, tick in enumerate(current_ticks):
55
- if tick >= 0:
56
- index = _
57
- break
58
- if decimal is not None:
59
- # my_ticks = [(round(float(iii), decimal)) for iii in my_ticks]
60
- current_ticks = [f"{val:.{decimal}f}" if val != 0 else "0" for val in current_ticks]
61
-
62
- out_ticks = [f"{val}".replace("-", "\u2212") for val in current_ticks]
63
- if add_space:
64
- # 在非负数前面加两个空格
65
- out_ticks[index:] = [" " + m for m in out_ticks[index:]]
66
-
67
- if ax_x is not None:
68
- ax_x.set_xticklabels(out_ticks)
69
- return ax_x
70
- if ax_y is not None:
71
- ax_y.set_yticklabels(out_ticks)
72
- return ax_y
73
- if cbar is not None:
74
- cbar.set_ticklabels(out_ticks)
75
- return cbar
76
-
77
- # ** 将生成图片/已有图片制作成动图
78
-
79
-
80
- def create_gif(image_list: list, gif_name: str, duration=0.2): # 制作动图,默认间隔0.2
81
- '''
82
- func : 制作动图,将已有图片拼接
83
- description : Gif格式动图
84
- param {*} image_list 图片列表
85
- param {*} gif_name 动图名称(含路径)
86
- param {*} duration 动图间隔
87
- return {*} 自动保存至指定路径(包含于动图名称中)
88
- example :
89
- '''
90
- import imageio.v2 as imageio
91
- frames = []
92
- for image_name in image_list:
93
- frames.append(imageio.imread(image_name))
94
- imageio.mimsave(gif_name, frames, format='GIF', duration=duration)
95
- print('Gif制作完成!')
96
- return
97
-
98
-
99
- # ** 转化经/纬度刻度
100
- def xy2lonlat(xy, lonlat='lon', decimal=2):
101
- '''
102
- param {*} xy : 经/纬度列表
103
- param {*} lonlat : 'lon' or 'lat'
104
- param {*} decimal : 小数位数
105
- return {*} 转化后的经/纬度列表
106
- example : xy2lonlat(x, lonlat='lon', decimal=2)
107
- '''
108
- def format_longitude(x_list):
109
- out_list = []
110
- for x in x_list:
111
- if x > 180:
112
- x -= 360
113
- # degrees = int(abs(x))
114
- degrees = round(abs(x), decimal)
115
- direction = "E" if x >= 0 else "W"
116
- out_list.append(f"{degrees:.{decimal}f}°{direction}" if x != 0 and x != 180 else f"{degrees}°")
117
- return out_list if len(out_list) > 1 else out_list[0]
118
-
119
- def format_latitude(y_list):
120
- out_list = []
121
- for y in y_list:
122
- if y > 90:
123
- y -= 180
124
- # degrees = int(abs(y))
125
- degrees = round(abs(y), decimal)
126
- direction = "N" if y >= 0 else "S"
127
- out_list.append(f"{degrees:.{decimal}f}°{direction}" if y != 0 else f"{degrees}°")
128
- return out_list if len(out_list) > 1 else out_list[0]
129
-
130
- if lonlat == 'lon':
131
- return format_longitude(xy)
132
- elif lonlat == 'lat':
133
- return format_latitude(xy)
134
-
135
-
136
- # ** 设置colorbar格式
137
- class _MyFormatter(mpl.ticker.ScalarFormatter):
138
- def __init__(self, cticks, fmt=None, useOffset=True, useMathText=True):
139
- mpl.ticker.ScalarFormatter.__init__(self, useOffset, useMathText)
140
- self.cticks = cticks
141
- self.fmt = fmt
142
- self.vmin = min(cticks)
143
- self.vmax = max(cticks)
144
- self.p_n = self.vmin < 0 and self.vmax > 0
145
- self.magnitude_min = int(
146
- math.modf(math.log10(min(abs(cticks[cticks != 0]))))[1])
147
- self.magnitude_max = int(math.modf(math.log10(max(abs(cticks))))[1])
148
- # print(self.vmin, self.vmax)
149
-
150
- def __call__(self, x, pos):
151
- if ((abs(x) < 1e-2) or (abs(x) > 1e4)) and x != 0:
152
- if self.magnitude_max - self.magnitude_min == 1 and (int(math.modf(math.log10(abs(x)))[1]) == self.magnitude_min):
153
- a, b = '{:.1e}'.format(x).split('e')
154
- a = float(a) / 10
155
- b = int(b) + 1
156
- else:
157
- a, b = '{:.2e}'.format(x).split('e')
158
- a = float(a)
159
- b = int(b)
160
- # return '${}{} \\times 10^{{{}}}$'.format(' ' if (self.p_n and x > 0) else '', a, b)
161
- return '${}{:.2f}$'.format(' ' if (self.p_n and x > 0) else '', a)
162
- elif x == 0:
163
- return '0'
164
- else:
165
- return mpl.ticker.ScalarFormatter.__call__(self, x, pos)
166
-
167
-
168
- # ** 绘制单张填色图
169
- def plot_contourf(pic_data, picname=None, c_map='rainbow', minmax=None, labels=None, ticks_space=None, ticks=None, figsize=(12, 9)):
170
- '''
171
- func : 绘制填色等值线图,单张
172
- description : 绘制单张填色等值线图,输入参数为横纵坐标、等值数据、图例标注等
173
- param {*}pic_data : 填色等值线图的等值数据
174
- param {*}picname : 图片保存的文件名(含路径)
175
- param {*}c_map : 颜色映射,默认rainbow
176
- param {*}minmax : 指定绘图的最大、小值,默认不指定
177
- param {*}labels : x、y轴以及图例的标注,默认不标注
178
- param {*}ticks_space : x、y轴刻度,以及所显示的标签,默认不显示
179
- param {*}figsize : 图片大小,默认(12,9)
180
- example : plot_contourf(pic_data, pictpath, var_name, c_map='bwr', labels=None, ticks_space=None, ticks=None, h=0, figsize=(12, 9))
181
- '''
182
- cmap = mpl.colormaps.get_cmap(c_map)
183
- if minmax is not None:
184
- value_min, value_max = minmax[0], minmax[1]
185
- else:
186
- value_min, value_max = pic_data.nanmin(), pic_data.nanmax()
187
- v_bry = max(abs(value_min), abs(value_max))
188
- flag = (value_min < 0) and (value_max > 0)
189
- norm = mpl.colors.TwoSlopeNorm(
190
- vmin=-1 * v_bry, vcenter=0, vmax=v_bry) if flag else mpl.colors.Normalize(vmin=value_min, vmax=value_max)
191
- cticks = [num for num in np.linspace(-1 * v_bry if flag else value_min, v_bry if flag else value_max, 9)] if value_min != value_max else None
192
- levels = np.linspace(-1 * v_bry, v_bry, 20) if flag else None if value_min == value_max else np.linspace(value_min, value_max, 20)
193
-
194
- shape = np.array(pic_data).shape
195
- x, y = np.meshgrid(np.arange(shape[1]), np.arange(shape[0]))
196
-
197
- fig, ax = plt.subplots(figsize=figsize)
198
- flag_lc = levels is not None and cticks is not None
199
- CS = ax.contourf(x, y, pic_data, cmap=cmap, norm=norm, levels=levels, extend='both') if flag_lc else ax.contourf(x, y, pic_data, cmap=cmap, norm=norm, extend='both')
200
- cb = fig.colorbar(CS, ax=ax, orientation='vertical', shrink=1, format='%.3g', spacing='uniform', ticks=cticks) if cticks is not None else fig.colorbar(CS, ax=ax, orientation='vertical', shrink=1, format='%.3g', spacing='uniform')
201
- """%.3g采用的是自动调整格式,也可设置为%.3f,则改为3位小数"""
202
-
203
- # 将格式化器设置为自定义的函数
204
- class_cticks = np.array(cb.get_ticks())
205
- # print(class_cticks)
206
- cct = abs(class_cticks[class_cticks != 0])
207
- if (min(cct) < 1e-2) or max(cct) > 1e4: # 判断是否需要采用科学计数法
208
- cb.formatter = _MyFormatter(class_cticks)
209
- cb.update_ticks()
210
-
211
- if labels is not None:
212
- cb.set_label(labels['c'])
213
- plt.xlabel(labels['x'])
214
- plt.ylabel(labels['y'])
215
- if ticks_space is not None and ticks is not None:
216
- plt.xticks(np.arange(0, len(x[0, :]) +
217
- 1e-5, ticks_space['x']), ticks['x'])
218
- plt.yticks(np.arange(0, len(y[:, 0]) +
219
- 1e-5, ticks_space['y']), ticks['y'])
220
-
221
- plt.title('Min: {:.3g}, Max: {:.3g}'.format(
222
- pic_data.min(), pic_data.max()))
223
-
224
- plt.savefig(
225
- picname, bbox_inches='tight') if picname is not None else plt.show()
226
- # plt.show()
227
- plt.clf()
228
- # 关闭当前figure
229
- plt.close()
230
-
231
-
232
- # ** 画等高线图,带经纬度坐标轴
233
- def plot_contourf_lonlat(data, lon, lat, interval=5, picname=None, c_map='rainbow'):
234
- '''
235
- param {*} data : 二维数据
236
- param {*} lon : 经度
237
- param {*} lat : 纬度
238
- param {*} interval : 经纬度间隔
239
- param {*} picname : 图片保存的文件名(含路径)
240
- param {*} c_map : 颜色映射,默认rainbow
241
- return {*} 无返回值
242
- '''
243
- if len(lon.shape) == 2:
244
- lon = lon[0, :]
245
- if len(lat.shape) == 2:
246
- lat = lat[:, 0]
247
- # interval是经纬度间隔,单位为°
248
- # 将lon,lat作为坐标轴刻度显示
249
-
250
- def format_longitude(x):
251
- if x > 180:
252
- x -= 360
253
- degrees = int(abs(x))
254
- direction = "E" if x >= 0 else "W"
255
- return f"{degrees}°{direction}" if x != 0 and x != 180 else f"{degrees}°"
256
-
257
- def format_latitude(y):
258
- if y > 90:
259
- y -= 180
260
- degrees = int(abs(y))
261
- direction = "N" if y >= 0 else "S"
262
- return f"{degrees}°{direction}" if y != 0 else f"{degrees}°"
263
-
264
- plt.contourf(data, cmap=c_map)
265
- x_space = int(len(lon) * interval / (lon[-1] - lon[0]))
266
- y_space = int(len(lat) * interval / (lat[-1] - lat[0]))
267
- plt.xticks(np.arange(0, len(lon), x_space), [format_longitude(lon[i]) for i in range(0, len(lon), x_space)])
268
- plt.yticks(np.arange(0, len(lat), y_space), [format_latitude(lat[i]) for i in range(0, len(lat), y_space)])
269
- plt.colorbar()
270
- plt.savefig(
271
- picname, bbox_inches='tight') if picname is not None else plt.show()
272
- plt.close()
273
-
274
-
275
- # ** 绘制矢量场
276
- def plot_quiver(u, v, lon, lat, picname=None, cmap='coolwarm', scale=0.25, width=0.002, x_space=5, y_space=5):
277
- '''
278
- param {*} u : 二维数据
279
- param {*} v : 二维数据
280
- param {*} lon : 经度, 1D or 2D
281
- param {*} lat : 纬度, 1D or 2D
282
- param {*} picname : 图片保存的文件名(含路径)
283
- param {*} cmap : 颜色映射,默认coolwarm
284
- param {*} scale : 箭头的大小 / 缩小程度
285
- param {*} width : 箭头的宽度
286
- param {*} x_space : x轴间隔
287
- param {*} y_space : y轴间隔
288
- return {*} 无返回值
289
- '''
290
- # 创建新的网格位置变量(lat_c, lon_c)
291
- if len(lon.shape) == 1 and len(lat.shape) == 1:
292
- lon_c, lat_c = np.meshgrid(lon, lat)
293
- else:
294
- lon_c, lat_c = lon, lat
295
-
296
- # 设置箭头的比例、颜色、宽度等参数
297
- # scale = 0.25 # 箭头的大小 / 缩小程度
298
- # color = '#E5D1FA'
299
- # width = 0.002 # 箭头的宽度
300
- # x_space = 1
301
- # y_space = 1
302
-
303
- # 计算矢量的大小
304
- S = xr.DataArray(np.hypot(np.array(u), np.array(v)))
305
-
306
- mean_S = S.nanmean()
307
-
308
- # 使用 plt.quiver 函数绘制矢量图
309
- # 通过设置 quiver 函数的 pivot 参数来指定箭头的位置
310
- quiver_plot = plt.quiver(lon_c[::y_space, ::x_space],
311
- lat_c[::y_space, ::x_space],
312
- u[::y_space, ::x_space],
313
- v[::y_space, ::x_space],
314
- S[::y_space, ::x_space], # 矢量的大小,可以不要
315
- pivot='middle',
316
- scale=scale,
317
- # color=color, # 矢量的颜色,单色
318
- cmap=cmap, # 矢量的颜色,多色
319
- width=width)
320
- # plt.quiverkey(quiver_plot, X=0.90, Y=0.975, U=1, label='1 m/s', labelpos='E', fontproperties={'size': 10})
321
- plt.quiverkey(quiver_plot, X=0.87, Y=0.975, U=mean_S, label=f'{mean_S:.2f} m/s', labelpos='E', fontproperties={'size': 10})
322
- plt.colorbar(quiver_plot)
323
- plt.xlabel('X')
324
- plt.ylabel('Y')
325
-
326
- plt.savefig(picname, bbox_inches='tight') if picname is not None else plt.show()
327
- plt.clf()
328
- plt.close()
329
-
330
-
331
- def plot_contourf_cartopy(data, lon, lat, picname=None, cmap='rainbow', cn_fill_num=20, fig_size=(12, 9), title='Cartopy', land_color='green', ocean_color='lightgrey'):
332
- '''
333
- param {*} data : 二维数据
334
- param {*} lon : 经度
335
- param {*} lat : 纬度
336
- param {*} picname : 图片保存的文件名(含路径)
337
- param {*} cmap : 颜色映射,默认rainbow
338
- param {*} cn_fill_num : 等值线数量
339
- param {*} fig_size : 图片大小,默认(12,9)
340
- param {*} title : 图片标题
341
- param {*} land_color : 陆地颜色
342
- param {*} ocean_color : 海洋颜色
343
- return {*} 无返回值
344
- '''
345
- if len(lon.shape) == 2:
346
- lon = lon[0, :]
347
- if len(lat.shape) == 2:
348
- lat = lat[:, 0]
349
-
350
- data_max = np.nanmax(data)
351
- data_min = np.nanmin(data)
352
- levels = np.linspace(data_min, data_max, cn_fill_num)
353
- cbar_ticks = np.linspace(data_min, data_max, 9)
354
-
355
- fig = plt.figure(figsize=fig_size)
356
- proj = ccrs.PlateCarree()
357
- ax = fig.add_subplot(1, 1, 1, projection=proj)
358
-
359
- ax.set_extent([lon[0], lon[-1], lat[0], lat[-1]], crs=proj)
360
-
361
- ax.add_feature(cfeature.LAND, facecolor=land_color, edgecolor="k")
362
- ax.add_feature(cfeature.COASTLINE, linewidth=0.5)
363
- # ax.add_feature(cfeature.BORDERS, linewidth=0.5, linestyle=":") # 加载国界线
364
-
365
- ax.set_xticks(np.arange(lon[0], lon[-1] + 1e-5, 5), crs=proj)
366
- ax.set_yticks(np.arange(lat[0], lat[-1] + 1e-5, 5), crs=proj)
367
- lon_formatter = LongitudeFormatter(zero_direction_label=True)
368
- ax.xaxis.set_major_formatter(lon_formatter)
369
- lat_formatter = LatitudeFormatter()
370
- ax.yaxis.set_major_formatter(lat_formatter)
371
- # plt.title(title, fontsize=16)
372
-
373
- cmap = plt.get_cmap(cmap)
374
- X, Y = np.meshgrid(lon, lat)
375
-
376
- # Fill color for land and ocean
377
- # ax.background_patch.set_facecolor(ocean_color)
378
- ax.patch.set_facecolor(ocean_color)
379
- ax.add_feature(cfeature.LAND, facecolor=land_color)
380
-
381
- cticks = cbar_ticks
382
- norm = mpl.colors.BoundaryNorm(cticks, cmap.N)
383
-
384
- cnplot = ax.contourf(X, Y, data, levels=levels, cmap=cmap, norm=norm, transform=proj, extend='both', alpha=1, zorder=0)
385
- # cllevels = np.linspace(data_min, data_max, 9)
386
- # clplot = ax.contour(X, Y, data, levels=levels[9::10], colors='k', linewidths=0.5, transform=proj, zorder=1, alpha=0.8, linestyle='--')
387
- # 添加色标,并选择位置
388
- divider = make_axes_locatable(ax)
389
- location = 3
390
- if location == 1: # 左侧
391
- cax = divider.new_horizontal(
392
- size="5%", pad=1, axes_class=plt.Axes, pack_start=True)
393
- fig.add_axes(cax)
394
- cbar = plt.colorbar(
395
- cnplot, cax=cax, orientation='vertical', extend='both')
396
- elif location == 2: # 下方
397
- cax = divider.new_vertical(
398
- size="5%", pad=0.3, axes_class=plt.Axes, pack_start=True)
399
- fig.add_axes(cax)
400
- cbar = plt.colorbar(
401
- cnplot, cax=cax, orientation='horizontal', extend='both')
402
- elif location == 3: # 右侧
403
- cax = divider.new_horizontal(size="5%", pad=0.1, axes_class=plt.Axes)
404
- fig.add_axes(cax)
405
- # cbar = plt.colorbar(cnplot, cax=cax, orientation='vertical', extend='both', format='%.0f')
406
- cbar = fig.colorbar(mpl.cm.ScalarMappable(cmap=cmap, norm=norm), cax=cax, orientation='vertical', extend='both', format='%.3f')
407
- cax.yaxis.set_ticks_position('right')
408
- cax.yaxis.set_label_position('right')
409
- else: # 上方
410
- cax = divider.new_vertical(size="5%", pad=0.2, axes_class=plt.Axes)
411
- fig.add_axes(cax)
412
- cbar = plt.colorbar(cnplot, cax=cax, orientation='horizontal', extend='both')
413
- cbar.ax.tick_params(labelsize=10)
414
- cbar.ax.xaxis.set_tick_params(direction='in', width=1, length=2)
415
- # 添加cbar_ticks
416
- # cbar.set_ticks(np.arange(round(levels[0]), round(levels[-1]), round((levels[-1]-levels[0])/9))) # 设置色标刻度
417
- # cbar.set_ticks(cbar_ticks) # 设置色标刻度
418
-
419
- cbar.set_ticks(cticks)
420
- # cbar.ax.ticks = np.linspace(data_min, data_max, 8)
421
- # cbar.set_ticks(np.arange(round(levels[0]), round(levels[-1]), round((levels[-1]-levels[0])/9))) # 设置色标刻度
422
-
423
- # 单独设置label
424
- cbar.set_label(title, fontsize=10, weight='bold')
425
- # cax.set_position([0.1, 0.2, 0.02, 0.6]) # 调整色标位置
426
- fig.savefig(picname, bbox_inches='tight', dpi=600) if picname is not None else plt.show()
427
- plt.close()
428
-
429
-
430
- if __name__ == '__main__':
431
- # ** 绘制填色图
432
- data = np.random.randn(100, 100)
433
- picname = 'test.png'
434
- plot_contourf(data, picname, c_map='rainbow', minmax=None, labels=None, ticks_space=None, ticks=None, figsize=(12, 9))
435
- # ** 绘制矢量场
436
- u = np.random.randn(100, 100)
437
- v = np.random.randn(100, 100)
438
- lon = np.linspace(0, 360, 100)
439
- lat = np.linspace(-90, 90, 100)
440
- picname = 'test.png'
441
- plot_quiver(u, v, lon, lat, picname, cmap='coolwarm', scale=0.25, width=0.002, x_space=5, y_space=5)
442
- # ** 绘制经纬度填色图
443
- data = np.random.randn(100, 100)
444
- lon = np.linspace(0, 360, 100)
445
- lat = np.linspace(-90, 90, 100)
446
- picname = 'test.png'
447
- plot_contourf_lonlat(data, lon, lat, interval=5, picname=picname, c_map='rainbow')
448
- # ** 制作动图
449
- image_list = ['test1.png', 'test2.png', 'test3.png']
450
- gif_name = 'test.gif'
451
- create_gif(image_list, gif_name, duration=0.2)