Multi-categorical Axes : How to order the category?

Hi there,

In my dataset, i have quarterly observations, and after rebuilding the index I usually get something like this:

             Value_A    Value_B   Year    Quarter

NaT NaN 1 -1 -1
2007Q1 … … … …
2007Q2 … … … …
… … … … …
2015Q4 NaN 1 2015 4
2016Q1 NaN 1 2016 1
2016Q2 NaN 1 2016 2
2016Q3 36 1 2016 3

The NaT is there on purpose; they reflect inconsistencies and I want to keep them. Therefore, my xaxis is a multicategorical axis, and not a datetime axis.

I get the expected output, which is:
StartFromQ12007

It’s worth to mention that:
a) my data are already properly sorted in the dataframe,
b) when plotting my traces, they are plotted by Quarter, like this:

for i in [-1, 1, 2, 3, 4]:
   trace=go.Bar(...)

Problems arise if the first observation for a specific year is for Q2, Q3, or Q4.
For example, say we have:
Value_A Value_B Year Quarter
NaT NaN 1 -1 -1
2015Q4 NaN 1 2015 4
2016Q1 NaN 1 2016 1
2016Q2 NaN 1 2016 2
2016Q3 36 1 2016 3

In such a case, i get this output:

So, either
a) I find a way to force the multicategorical axis to be order by year
(I tried with categoryorder =“array”, and categoryarray=["-1". “2007”, “2008”, …“2021”]), but it didnt work
or,
b) I apply a workaround: in my dataset, if the first observation is not for Q1, then it must insert data for Q1.

Solution a) would be better, but I can’t find the way. Does anyone know a way to sort a multicategorical axis? The references give an example for a categorical array, not a multicategorical one (Categorical Axes | Python | Plotly)

Edit: In the meantime, I found this discussion: Extend `categoryorder` to multicategory axes · Issue #3908 · plotly/plotly.js · GitHub
and also this one: Multi-category sorting bug with missing categories · Issue #3723 · plotly/plotly.js · GitHub

While I was looking for a way to sort a multicategorcal axis, I found my own post, written 2y earlier…

I have an xaxis whose external categories are [2017, 2017, 2017, 2017, 2018, 2018, 2018, 2018]
and inner categories are [Q1, Q2, Q3, Q4].

Because some traces don’t always have data for each date, it starts with Q2…
image

My xaxis already set categoryorder=“category ascending”. But it looks like it’s only applied on the external level (?)

Is there any workardound to fix that?

It happens because the first external category does not contain all the inner categories:
image

So, I assume the starting point is making sure that I do have data for Q1 if the dataset starts at Q2, for Q1 and Q2 if it starts at Q3, etc…and fill the blank with 0 or NA…

For other readers who might have the same issues, it seems some work has been done to fix it, here :slight_smile: #2175 & #3723 - Multicategory Multilevel 2+ & Sorting Multicategory by richardnm-2 · Pull Request #6327 · plotly/plotly.js · GitHub