0

I have been working with x-array. How do we reshape the array? Is there any method like re-shape in NumPy?

temp = data['__xarray_dataarray_variable__'][:, :, :]

The above line of code selects all the 3 dimensions of the x-array. Ie the shape of the array is (5, 2, 1). How do I change the x-array so that it is of the form (5*2, 1). Thanks in advance.

0

1 Answer 1

1

Xarray syntax is a bit unintuitive. In any case, you can use stack for this:

temp = temp.stack(dim_new=['dim_0','dim_1'])

Assuming originally it was an xarray of shape (dim_0: 5, dim_1: 2, dim_2: 1), temp will then become an xarray with dimensions (dim_2: 1, dim_new: 10). You can further apply temp.transpose('dim_new','dim_2') to get it to (10,1) shape.

Sign up to request clarification or add additional context in comments.

3 Comments

Here I added 0.01 to the values of xarray because earlier it showed that log of 0 is infinite.
So with your solution I could get my xarray in the required form but when I try to plot it, I get some wierd error. plt.figure(figsize=(3,5)) data_slice = temp1[:, :] xr.ufuncs.log(data_slice + 0.01).plot(cmap='magma', vmin=0, vmax = max_value*.7). Here I am getting the error ----> Plotting requires coordinates to be numeric or dates of type np.datetime64, datetime.datetime, cftime.datetime or pd.Interval. Also i cannot find the variable 'xarray_dataarray_variable' . How do I solve there errors.
I can't really tell without seeing your array and your code. It's likely there's some sort of formatting error. Can you try converting it to a numpy array and then plot?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.