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:
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