Matplotlib Pcolormesh叠加异常问题求助
Looking at your code trying to replicate the GOES-16 thermal map tutorial, here are the key issues causing unexpected output, along with straightforward fixes:
Empty Title Rendering
Yourplt.titlecall uses a placeholder-free format string, so it won't display any text. The current line:plt.title(' on '.format(data_long_name, data_time_grab))Should be updated to include placeholders for your variables:
plt.title('{0} on {1}'.format(data_long_name, data_time_grab))This will properly show the data's long name and timestamp in the plot title.
Incorrect Missing Data Handling
You're usingnp.where(data.data != 65535)to find valid data bounds, but GOES NetCDF data is often stored as masked arrays. Leveraging the built-in mask attribute is more reliable and avoids accidentally including invalid values:data_bounds = np.where(~data.mask) # Invert the mask to select valid pointsIf your data isn't masked, double-check that 65535 is indeed the correct fill value for your specific variable (verify from the NetCDF metadata—some GOES products use different values).
Shading Parameter Mismatch
Theshading='gouraud'parameter inm.pcolormeshis designed for irregular triangular grids, but GOES-16 data uses regular gridded data. This can cause rendering artifacts or incorrect color interpolation. Switch to the appropriate setting for regular grids:m.pcolormesh(lon.data, lat.data, data, latlon=True, zorder=999, shading='flat')TeX Setup Placement
Yourplt.rc('text', usetex=True)line comes after creating the figure and colorbar, so it won't affect the colorbar label or title. Move this line to the top of your script, before initializing the figure:plt.rc('text', usetex=True) fig = plt.figure(figsize=(9, 4), dpi=200)Fragile Unit String Manipulation
Your manual string replacement for units is error-prone. Use raw strings and proper TeX syntax directly instead:# Replace your current unit processing line with this cb.set_label(r'{} {}'.format(var_name, data_units))The
rprefix ensures TeX commands are interpreted correctly without needing messy manual fixes.Minor: Simplified Savefig Color
Yourfacecolorvalue can be rewritten as a hex string for better readability:plt.savefig('goes_16_data_demo.png', dpi=200, facecolor='#fcfcfc')
Fixed Full Code Snippet
plt.rc('text', usetex=True) # Move to top for proper TeX rendering nc_folder = data_path # define folder where .nc files are located lon, lat = lat_lon_reproj(nc_folder) data, data_units, data_time_grab, data_long_name, var_name = data_grab(nc_folder, file_indx) # main data grab from function above # Handle masked or fill-value based invalid data data_bounds = np.where(~data.mask) if hasattr(data, 'mask') else np.where(data.data != 65535) bbox = [np.min(lon[data_bounds]), np.min(lat[data_bounds]), np.max(lon[data_bounds]), np.max(lat[data_bounds])] # set bounds for plotting # figure routine for visualization fig = plt.figure(figsize=(9, 4), dpi=200) n_add = 0 # for zooming in and out m = Basemap(llcrnrlon=bbox[0] - n_add, llcrnrlat=bbox[1] - n_add, urcrnrlon=bbox[2] + n_add, urcrnrlat=bbox[3] + n_add, resolution='i', projection='cyl') m.fillcontinents(color='#d9b38c', lake_color='#bdd5d5', zorder=1) # continent colors m.drawmapboundary(fill_color='#bdd5d5', zorder=0) # ocean color m.drawcoastlines(linewidth=0.5) m.drawcountries(linewidth=0.25) m.drawstates(zorder=2) # Plot data with correct shading for regular grids m.pcolormesh(lon.data, lat.data, data, latlon=True, zorder=999, shading='flat') # plotting actual LST data parallels = np.linspace(bbox[1], bbox[3], 5) m.drawparallels(parallels, labels=[True, False, False, False], zorder=2, fontsize=8) meridians = np.linspace(bbox[0], bbox[2], 5) m.drawmeridians(meridians, labels=[False, False, False, True], zorder=1, fontsize=8) cb = m.colorbar() # Clean unit label with raw TeX string cb.set_label(r'{} {}'.format(var_name, data_units)) # Fixed title with proper variable insertion plt.title('{0} on {1}'.format(data_long_name, data_time_grab)) plt.tight_layout() # Simplified facecolor hex code plt.savefig('goes_16_data_demo.png', dpi=200, facecolor='#fcfcfc') # uncomment to save figure plt.show()
内容的提问来源于stack exchange,提问作者Hasham Ghuffary




