[Python] How do I change dimensions in matplotlib.pcolormesh?

Discussão em 'Python' iniciado por Stack, Setembro 10, 2025.

  1. Stack

    Stack Membro Participativo

    I want to open up a .nc file and it is not working.

    I keep getting this:

    TypeError: Dimensions of C (8400, 6800) should be one smaller than X(8400) and Y(6800) while using shading='flat' see help(pcolormesh)


    How do I change the dimensions for pcolormesh?

    This is my code:

    import xarray as xr
    from netCDF4 import Dataset
    import numpy as np
    import pandas as pd
    from mpl_toolkits.basemap import Basemap
    import matplotlib.pyplot as plt

    filepath = 'C:/Users/blahblah/Desktop/DATATAAA/.venv/OzWALD.Ssoil.2020.nc'

    data = Dataset(filepath, mode='r')

    soil = data.variables['Ssoil'][:]
    lat = data.variables['latitude'][:]
    lon = data.variables['longitude'][:]
    time = data.variables['time'][:]

    lon, lat = np.meshgrid(lon, lat)
    data = np.random.rand(8400, 6800)

    mp = Basemap(projection='merc',
    llcrnrlon=147.459100,
    llcrnrlat=-37.589225,
    urcrnrlon=148.210045,
    urcrnrlat=-27.673933,
    resolution='i')

    plt.figure(figsize=(10, 8))

    c_scheme = mp.pcolormesh(lon, lat, mean[0, :, :], cmap='Greens', shading='auto')

    mp.drawcoastlines()
    mp.drawstates()

    cbar = mp.colorbar(c_scheme, location='right', pad='10%')
    cbar.set_label('Trees')

    plt.title('Trees in 2016')
    plt.savefig('tave.jpg', dpi=300)
    plt.show()

    Continue reading...

Compartilhe esta Página