You can do this by manipulating the idata.posterior
object, which is an xarray Dataset. If you only care about changing the order in which the plots appear, then you can index the data based on school using the Dataset.sel method.
Here’s an example where I select schools in the opposite order (sorry about the missing cells, I’m sure no one wants to see my typos!):
In [1]: import arviz as az
In [2]: inf = az.load_arviz_data("non_centered_eight")
In [3]: inf.posterior
Out[3]:
<xarray.Dataset>
Dimensions: (chain: 4, draw: 500, school: 8)
Coordinates:
* chain (chain) int64 0 1 2 3
* draw (draw) int64 0 1 2 3 4 5 6 7 8 ... 492 493 494 495 496 497 498 499
* school (school) object 'Choate' 'Deerfield' ... "St. Paul's" 'Mt. Hermon'
Data variables:
mu (chain, draw) float64 ...
theta_t (chain, draw, school) float64 ...
tau (chain, draw) float64 ...
theta (chain, draw, school) float64 ...
Attributes:
created_at: 2019-06-21T17:36:37.382566
inference_library: pymc3
inference_library_version: 3.7
In [4]: _ = az.plot_trace(inf.posterior, var_names=["theta"])
# will plot image 1 below
In [6]: order = inf.posterior.school.values[::-1]
In [7]: order
Out[7]:
array(['Mt. Hermon', "St. Paul's", 'Lawrenceville', 'Hotchkiss',
'Phillips Exeter', 'Phillips Andover', 'Deerfield', 'Choate'],
dtype=object)
In [9]: _ = az.plot_trace(inf.posterior.sel(school=order), var_names=["theta"])
# will plot image 2 below
Cell [4] plots this:
Cell [9] plots this:
In general, arviz integrates very nicely with xarray—both are, in my experience, very well-designed and quite flexible, so hats off to the developers of both
If you want to plot subsets of your posterior, to rename the labels, reorder things, etc., you can usually do it by manipulating the underlying xarray Dataset and passing that straight to arviz.
With that said, this was challenging to do at first—mostly because learning the language of xarray Datasets and the new abstractions they use took some trial and error. I think this might be a useful example to add to the arviz docs to highlight how you can do some operations in xarray to make your plotting life easier.