[Python] How to enhance netcdf plotting compilation speed?

Discussão em 'Python' iniciado por Stack, Agosto 23, 2025.

  1. Stack

    Stack Membro Participativo

    I finally moved into python (using pycharm) after years with MATLAB (thanks to my classes during college, where they mostly used MATLAB). I work with netcdf data such as Outgoing Longwave Radiation. As much as I like a lot more the language, I noticed that the time to compile plots takes almost 2 minutes (which, for my previous work, seems like "a lot"). I would like to ask you if there is some way in which I am making my code slower to compile? Or is it just the way it works? This is my code, I use cartopy to plot a total of 4 subplots in a for loop:

    # Rangos de contornos
    clevs=np.arange(-25,26,2.5)
    nrows=4
    ncols=1


    # Define the figure and each axis for the 3 rows and 3 columns
    fig, axs = plt.subplots(nrows=nrows,ncols=ncols,
    subplot_kw={'projection': ccrs.PlateCarree(central_longitude=-180)},
    figsize=(6,20))

    # getting the original colormap using cm.get_cmap() function
    orig_map=cmaps.NEO_div_vegetation_c

    # reversing the original colormap using reversed() function
    reversed_map = orig_map.reversed()
    # axs is a 2 dimensional array of `GeoAxes`. We will flatten it into a 1-D array
    axs=axs.flatten()

    #Loop over all of the models
    for i,olrms in enumerate(F_mjo):
    # Select the week 1 forecast from the specified model
    data=PCP_MJO

    # Add the cyclic point
    data,lons=add_cyclic_point(data,coord=ds['lon'])

    # Contour plot
    cs=axs.contourf(lons,ds['lat'],data,clevs,
    transform = ccrs.PlateCarree(),
    cmap=reversed_map,extend='both')


    # Draw the coastines for each subplot
    axs.coastlines()
    gl = axs.gridlines(color='black',alpha=0.2)
    axs.set_extent([50,-60, -35, 35], crs=ccrs.PlateCarree(central_longitude=-180))

    # Longitude labels
    if i == 3:
    axs.set_xticks(np.arange(50,301,50), crs=ccrs.PlateCarree())
    lon_formatter = cticker.LongitudeFormatter()
    axs.xaxis.set_major_formatter(lon_formatter)
    else:
    axs.set_xticks(np.arange(50,301,50), crs=ccrs.PlateCarree())
    axs.set_xticklabels([])

    # Latitude labels
    axs.set_yticks(np.arange(-35,36,20), crs=ccrs.PlateCarree())
    lat_formatter = cticker.LatitudeFormatter()
    axs.yaxis.set_major_formatter(lat_formatter)
    # Title each subplot with the name of the model
    axs.set_ylabel(olrms)

    axs.set_title(str(days)+' days',loc='right',fontsize=10)
    # Adjust the location of the subplots on the page to make room for the colorbar
    fig.subplots_adjust(bottom=0.05, top=0.4, left=0.1, right=0.9,
    wspace=0.01, hspace=0.3)

    # Add a colorbar axis at the bottom of the graph
    cbar_ax = fig.add_axes([0.2, 0.005, 0.6, 0.015])

    # Draw the colorbar
    rango= np.arange(-25,26,5)
    cbar=fig.colorbar(cs, cax=cbar_ax,orientation='horizontal',ticks=rango)
    #plt.title('OLR ($W/m^{2}$)')
    cbar.set_label('OLR ($W/m^{2}$)')



    I would be glad to read your answers :) Thanks

    Continue reading...

Compartilhe esta Página