[Python] Contourf bug outside extent with cartopy Orthographic

Discussão em 'Python' iniciado por Stack, Janeiro 20, 2025.

  1. Stack

    Stack Membro Participativo

    I'm trying to plot this xarray data array (file here) :


    file_cyclic :

    <xarray.DataArray (latitude: 26, longitude: 361)>
    array([[66.66666667, 61.53846154, 52.94117647, ..., 66.66666667,
    68.75 , 66.66666667],
    [58.82352941, 57.14285714, 50. , ..., 82.35294118,
    75. , 58.82352941],
    [66.66666667, 66.66666667, 64.28571429, ..., 75. ,
    72.22222222, 66.66666667],
    ...,
    [85.91549296, 87.14285714, 85.07462687, ..., 86.95652174,
    87.67123288, 85.91549296],
    [92.06349206, 91.80327869, 88.70967742, ..., 93.5483871 ,
    90.76923077, 92.06349206],
    [92.59259259, 92.85714286, 91.07142857, ..., 90.56603774,
    90.19607843, 92.59259259]])
    Coordinates:
    * longitude (longitude) float32 0.0 1.0 2.0 3.0 ... 357.0 358.0 359.0 360.0
    * latitude (latitude) float32 -60.0 -61.0 -62.0 -63.0 ... -83.0 -84.0 -85.0


    using this code :

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

    file_cyclic = xr.open_dataset('./my_data.nc') #If you want to try with my data
    file_cyclic= file_cyclic['__xarray_dataarray_variable__'] #If you want to try with my data


    plt.close('all')


    fig, ax = plt.subplots(1, 1, figsize=(12, 12),
    subplot_kw={'projection': ccrs.Orthographic(central_latitude=-90, central_longitude=0),
    'transform': ccrs.PlateCarree()})
    cont = file_cyclic.plot.contourf(levels=np.arange(0,100.1,5),cmap='YlGnBu', transform=ccrs.PlateCarree(), vmin=0, vmax=100, ax=ax,add_colorbar=True, extend='neither')

    ax.coastlines()
    ax.set_extent([file_cyclic.longitude.min(), file_cyclic.longitude.max(), file_cyclic.latitude.min(), file_cyclic.latitude.max()], crs=ccrs.PlateCarree())


    I get a bug : my colorscale is off and it fills my no-data area (where I have 0, nan or no data actually) with color...

    Bug

    However when I change the levels to :

    levels=np.arange(1,100.1,5)


    it doesn't produce a bug, but this is not the levels I want.

    no bug, but not the levels I want

    I don't understand where this is coming from since, when I manually put

    levels=[ 1, 5., 10., 15., 20., 25., 30., 35., 40., 45., 50.,
    55., 60., 65., 70., 75., 80., 85., 90., 95., 100.]


    the bug is still here, so this is not coming from the lowest level. Just bottom level changed, still bugging

    The bug is also here if I change the higher level...

    This is not just filling my no-data area, otherwise I would have cropped this area from the plot, this is also messing with my colorscale, so that's a huge problem.

    How can I handle that?

    Thank you,

    Continue reading...

Compartilhe esta Página