Reputation: 85
I am hoping to plot a graph representing monthly temperature from 1981-01-01 to 2016-12-31.
I would like the months "Jan Feb Mar Apr May...Dec" on the x-axis and the temperature record as the y-axis as my plan is to compare monthly temperature record of 1981 - 1999 with 2000 - 2016.
I have read in the data no problem.
temp1 = xr.open_dataarray('temp1981-1999.nc') temp2 = xr.open_dataarray('temp2000-2016.nc')
and have got rid of the lat and lon dimensions
temp1mean = temp1.mean(dim=['latitude','longitude']) temp2mean = temp2.mean(dim=['latitude','longitude'])
I tried to convert it into a dataframe to allow me to carry on the next step such as averaging the months using group by
temp1.cftime_range(start=None, end=None, periods=None, freq='M', normalize=False, name=None, closed=None, calendar='standard')
t2m time 1981-01-01 276.033295 1981-02-01 278.882935 1981-03-01 282.905579 1981-04-01 289.908936 1981-05-01 294.862457 ... ... 1999-08-01 295.841553 1999-09-01 294.598053 1999-10-01 289.514771 1999-11-01 283.360687 1999-12-01 278.854431
monthly = temp1mean.groupby(temp1mean.index.month).mean()
However I got the following error.
"'DataArray' object has no attribute 'index'"
Therefore, I am wondering if there's any way to groupby all the monthly means and create a graph as followed.
In addition to the main question, I would greatly appreciate if you could also suggest ways to convert the unit kelvin into celsius when plotting the graph. As I have tried the command
celsius = temp1mean.attrs['units'] = 'kelvin'
but the output is merely
"'air_temperature"
I greatly appreciate any suggestion you may have for plotting this grpah! Thank you so so much and if you need any further information please do not hesitate to ask, I will reply as soon as possible.
Upvotes: 4
Views: 2136
Reputation: 15442
The xarray docs have a helpful section on using the datetime accessor on any datetime dimensions:
Similar to pandas, the components of datetime objects contained in a given
DataArray
can be quickly computed using a special .dt accessor....
The .dt accessor works on both coordinate dimensions as well as multi-dimensional data.
xarray also supports a notion of “virtual” or “derived” coordinates for datetime components implemented by pandas, including “year”, “month”, “day”, “hour”, “minute”, “second”, “dayofyear”, “week”, “dayofweek”, “weekday” and “quarter”
In your case, you need to use the name of the datetime coordinate (whatever it is named) along with the .dt.month
reference in your groupby. If your datetime coordinate is named "time", the groupby operation would be:
monthly_means = temp1mean.groupby(temp1mean.time.dt.month).mean()
or, using the string shorthand:
monthly_means = temp1mean.groupby('time.month').mean()
As for units, you should definitely know that xarray does not interpret/use attributes or metadata in any way, with the exception of plotting and display.
The following assignment:
temp1mean.attrs['units'] = 'kelvin'
simply assigns the string "kelvin" to the user-defined attribute "units" - nothing else. This may show up as the data's units in plots, but that doesn't mean the data isn't in Fahrenheit or dollars or m/s. It's just a string you put there.
If the data is in fact in kelvin, the best way to convert it to Celsius that I know of is temp1mean - 273.15
:)
If you do want to work with units explicitly, check out the pint-xarray extension project. It's currently in early stages and is experimental, but it does what I think you're looking for.
Upvotes: 4