Search code examples
multidimensional-arraycoordinatesscatter-plotpython-xarray

How do i change the order of the coordinate labels in xarray?


When I create a xarray dataArray, I am able to set the labels of the coordinates in the order I want to but when I then use .combine_first to add some data from a different array to it, it always reorders the labels alphabetical. Then I want to plot the data in a facet line plot with multiple data in one graph, and here I get the problem, that the order of the labels of the coordinates define which line gets ploted above the other. Is there a way to reorder the labels after I combined the data, or how I get to choose which line gets ploted above the others?

Here an example with the coordinate Labels of 'type' which are not in the correct order

    import matplotlib.pyplot as plt
    import xarray as xr
    import numpy as np
    import pandas as pd
            
            
    data1 = np.random.randn(4, 4,3)
    type = ["b", "c", "a", "d"]
    loc= np.linspace(1,3,3)
            
    times = pd.date_range("2000-01-01", periods=4)
    foo = xr.DataArray(data1, coords=[times, type, loc], dims=["time", "type","loc"])
    foo
        
Out[1]: <xarray.DataArray (time: 4, type: 4, loc: 3)>
array([[
                   ...
                         ]])
Coordinates:
    * time     (time) datetime64[ns] 2000-01-01 2000-01-02 2000-01-03 2000-01-04
    * type     (type) <U1 'b' 'c' 'a' 'd'
    * loc      (loc) float64 1.0 2.0 3.0

Here I make a second array and combine it with the first one:

 data2=np.random.randn(4,1,3)
    type2 =["b"]
    loc2= np.linspace(1,3,3)
    times2 = pd.date_range("2000-01-01", periods=4)
    foo2 = xr.DataArray(data2, coords=[times2, type2, loc2,], dims=["time", "type","loc"])
    foo2
    comb=foo.combine_first(foo2)
    comb
Out[2]: 
<xarray.DataArray (time: 4, type: 4, loc: 3)>
array([[[-2.45206949e+00, -1.39563427e+00,  4.01038823e-01],
        ...
        [-1.60937495e+00,  1.23864314e+00, -3.89573178e-01]]])
Coordinates:
  * type     (type) <U1 'a' 'b' 'c' 'd'
  * time     (time) datetime64[ns] 2000-01-01 2000-01-02 2000-01-03 2000-01-04
  * loc      (loc) float64 1.0 2.0 3.0

After .combine_first the labels 'a', 'b', 'c', 'd' are reorderd alphabetical. Here is the way I want to plot it and where the order of the lines I want to change

t=comb.plot.line(x="time", col="loc", linewidth= 5, col_wrap=3)

Plot


Solution

  • You can use DataArray.sel to order the array along a coordinate to your liking (here I am using the type list you defined in your question):

    comb.sel(type=type).plot(x="time", col="loc", linewidth= 5, col_wrap=3)