working with netCDF on python with matplotlib

So I am pretty new in programming, currently doing some research on netCDF .nc files to work with python. I have the following code which I am sure it will not work. The objective here is to plot a graph simple line graph of 10m u-component of winds over time.
The problem I think is that 10m u-component of winds has 4D shape of (time=840, expver=2, latitude=19, longitude=27) while time being only (time=840).
Any replies or ideas will be much appreciated! The code is as shown:

from netCDF4 import Dataset
import matplotlib.pyplot as plt
import numpy as np

nc = Dataset(r’C:\WAIG\Python\ERA5_py01\Downloaded_nc_file/’,‘r’)

for i in nc.variables:

lat = nc.variables[‘latitude’][:]
lon = nc.variables[‘longitude’][:]
time = nc.variables[‘time’][:]
u = nc.variables[‘u10’][:]

plt.plot(np.squeeze(u), np.squeeze(time))

This is a repost of a stackoverflow post. Perhaps preferable to link to that because there is a larger audience there.

As I said in that post, you have not specified what you are trying to do, other than “plot”. That is too vague for us to specifically help you.

1 Like

Agreeing w/ @jklymak that more detail would be helpful, but if you’re interested in a general tutorial on netCDF in python than check out the xarray documentation:

Also this might be another good netcdf resource

1 Like