【问题标题】:xarray multi-dimensional data plotting with matplotlib LogLocator使用 matplotlib LogLocator 绘制 xarray 多维数据
【发布时间】:2020-10-11 23:27:24
【问题描述】:

我有一个名为 da 的 xarray DataArray 对象:

<xarray.DataArray 'sm_pct' (month: 12, latitude: 681, longitude: 841)>
array([[[0, 0, ..., 0, 0],
        [0, 0, ..., 0, 0],
        ...,
        [0, 0, ..., 0, 0],
        [0, 0, ..., 0, 0]],

       [[0, 0, ..., 0, 0],
        [0, 0, ..., 0, 0],
        ...,
        [0, 0, ..., 0, 0],
        [0, 0, ..., 0, 0]],

       ...,

       [[0, 0, ..., 0, 0],
        [0, 0, ..., 0, 0],
        ...,
        [0, 0, ..., 0, 0],
        [0, 0, ..., 0, 0]],

       [[0, 0, ..., 0, 0],
        [0, 0, ..., 0, 0],
        ...,
        [0, 0, ..., 0, 0],
        [0, 0, ..., 0, 0]]])
Coordinates:
  * latitude   (latitude) float64 -10.0 -10.05 -10.1 ... -43.9 -43.95 -44.0
  * longitude  (longitude) float64 112.0 112.0 112.1 112.2 ... 153.9 153.9 154.0
  * month      (month) int64 1 2 3 4 5 6 7 8 9 10 11 12

我想在 xarray DataArray.plot() 函数中使用 matplotlib LogLocator 和 cartopy 作为底图。

cmap=plt.cm.RdBu_r
central_lon, central_lat = 145, -37
p = da.plot(transform=ccrs.PlateCarree(), levels=20, cmap=cmap, col_wrap=3, aspect=2, size=4, x='longitude', y='latitude', col='month', subplot_kws={'projection': ccrs.Orthographic(central_lon, central_lat)})

原生 matplotlib ax.contourf() 函数具有 locator=ticker.LogLocator() 作为属性。

如何让locator=ticker.LogLocator() 使用 xarray DataArray.plot() 包装函数?

【问题讨论】:

    标签: python matplotlib python-xarray cartopy


    【解决方案1】:

    您可以使用 cartopy 轴对象,我建议您先创建它,然后通过 ax= 关键字参数将其传递给 da.plot()

    语法如下(此处为 x 轴)

    ax = plt.axes(projection=ccrs.Orthographic(central_lon, central_lat))
    
    p = da.plot(
        ax=ax,
        transform=ccrs.PlateCarree(),
        levels=20,
        cmap=cmap,
        col_wrap=3,
        aspect=2,
        size=4,
        x="longitude",
        y="latitude",
        col="month",
    )
    
    ax.gridlines(
        draw_labels=True,
        xlocs=ticker.LogLocator(base=10.0, numticks=5),  # or whatever your LogLocator specs are
        ylocs=ticker.LogLocator(base=10.0, numticks=5),  
    )
    

    请注意,这只适用于正经度和纬度。对于负坐标,您可能必须创建一个自定义的 LogLocator 子类来处理它。

    【讨论】:

      猜你喜欢
      • 2020-12-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-02-07
      • 1970-01-01
      • 2022-01-27
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多