用Matplotlib和Cartopy绘制正投影数据

atmip9wb  于 2023-03-23  发布在  其他
关注(0)|答案(1)|浏览(177)

我不知道如何得到一个Map,就像所附的例子,但没有海岸线,边界和网格线以外的绘制数据。第二个问题是,而不是一个正方形框架与经度和纬度标签,是否有可能设置x和y轴沿着绘制数据的轮廓,使标签是旁边的这个轮廓,而不是旁边的框架?

import xarray as xr
from matplotlib import pyplot as plt
import cartopy.crs as ccrs
import cartopy

ds = xr.open_dataset(r'D:\Python\qq_z1000.nc')
ds2 = ds['qq'].mean(dim='time')
minn = ds2.min()
maxx = ds2.max()

fig = plt.figure(figsize=(8, 8), dpi=300, num="False")
central_lon, central_lat = 12.5, 47.5
extent = [-22.5, 45, 25, 65]
ax = plt.axes(projection=ccrs.Orthographic(central_lon, central_lat))
ax.set_extent(extent)

gl = ax.gridlines(draw_labels=True, linestyle='--', color='grey', dms=True, 
                  x_inline=False, y_inline=False, linewidth=0.3)
gl.top_labels = False
gl.right_labels = False
gl.xlabel_style = {'size': 10, 'color': 'black'}
gl.ylabel_style = {'size': 10, 'color': 'black'}
gl.xlocator = plt.FixedLocator(range(-180, 181, 10))

ax.coastlines(resolution='50m')
ax.add_feature(cartopy.feature.BORDERS, edgecolor='black')
im = ax.pcolormesh(ds2.longitude, ds2.latitude, ds2, transform=ccrs.PlateCarree(), 
                   cmap='jet', vmin=minn, vmax=maxx)

cbar = fig.colorbar(im, orientation='horizontal', pad=0.05, shrink=0.8)

#plt.savefig('my_map2.jpg', dpi=300, bbox_inches='tight')
plt.show()

我尝试过用不同的方法更改代码中的这一行,但都无济于事。
im = ax.pcolormesh(ds2.longitude, ds2.latitude, ds2, transform=ccrs.PlateCarree(), cmap='jet', vmin=minn, vmax=maxx)

5jvtdoz2

5jvtdoz21#

我设法纠正了代码,得到了我想要的Map。

import matplotlib.pyplot as plt
import cartopy
import cartopy.crs as ccrs
import numpy as np
import xarray as xr
import matplotlib.path as mpath

ds = xr.open_dataset(r'R:\Python\qnew_1000.nc')
ds2 = ds['qq'].mean(dim='time')
minn = ds2.min()
maxx = ds2.max()

noProj = ccrs.PlateCarree()
myProj = ccrs.LambertConformal(central_longitude=0, central_latitude=55)
myProj._threshold = myProj._threshold/20.

plt.figure(figsize=(8,8))
axs = plt.axes(projection=myProj)

[axs_hdl] = axs.plot([60, -60, -60, 60, 60], [20, 20, 85, 85, 20],
         color='black', linewidth=0.5, marker='none',
         transform=noProj)

tx_path = axs_hdl._get_transformed_path()
path_in_data_coords, _ = tx_path.get_transformed_path_and_affine()

polygon = mpath.Path(path_in_data_coords.vertices)
axs.set_boundary(polygon) #This masks-out unwanted part of the plot
axs.set_xmargin(0)
axs.set_ymargin(0)
axs.set_global()
axs.add_feature(cartopy.feature.OCEAN, linewidth=.3, color='lightblue')
axs.add_feature(cartopy.feature.LAND, zorder=1, edgecolor='black')
axs.add_feature(cartopy.feature.COASTLINE, linewidth=0.5)
axs.add_feature(cartopy.feature.BORDERS, linewidth=0.5)
axs.set_extent([-60, 60, 20, 85], crs=noProj)
axs.tick_params(axis='both', which='major', labelsize=10)
axs.set_title("")
gl=axs.gridlines(draw_labels=True, x_inline=False, y_inline=False, color='k', linestyle='dashed', linewidth=0.5)
gl.top_labels = False

pcm = axs.pcolormesh(ds2.longitude, ds2.latitude, ds2, transform=ccrs.PlateCarree(), 
                     vmin=minn, vmax=maxx, cmap='jet',
                     shading='nearest')

cbar = plt.colorbar(pcm, ax=axs, orientation='horizontal', pad=0.08, shrink=0.8)
cbar.set_label('(g kg$^{-1}$)')

plt.subplots_adjust(left=0.05, right=0.95, bottom=0.05, top=0.95, wspace=0.2, hspace=0.2)
plt.savefig("figure.jpg", dpi=300, bbox_inches='tight', pad_inches=0.1, format='jpg', facecolor="w", transparent=False)
plt.show()

enter image description here

相关问题