✊🏿 Black Lives Matter. Please consider donating to Black Girls Code today.
📊 Dash 2.0 is Arriving. Register here.

Scatter traces: Could xaxis be a list of axis id ? Goal: plotting observations of differents trace, on different subplot, depending of one of their variable

Hello there,

I have a dataset in which I have, for the sake of simplification, 4 relevant variable:
“manufacturing year”, named “year” (trace name)
“% of moisture (storage)”, named “moisture” (xaxis)
“number of month (storage)”, named “month” (yaxis)
“grade” (marker size)

For the sake of readibility, I’ve made 3 subplots. This part is done:
On the left: if the % of moisture or if the number of months are in the lowest 10th percentile.
On the right: as above, but for the highest 10th percentile
In the middle: for what’s not an extrema.

So far I’ve tried 3 things:

  1. plotting my traces by year, no consideration for the quartile → all the data are plotted on the same subplot, because xaxis value is …x . No surprise. The number of legends matches the number of different years,
  2. plotting my traces by year, and by quartile (that is, kind of 3 dataset) → I get a lot of traces. Typically, the 13 items in the worst quartile (trace#1 for 2 items (2013), trace#2 for 3 items (2014), trace#3 for 1 item in 2016, trace#4 for 5 items in 2018, trace#5 for 2 items of 2019), then the 132 items “in the average” in the main graph (middle, traces 6 to 37)) and finally 7 items in the graph on the right. Then, there are too many legends. I would like that the year appears only once in the legend, without hacky solution. I’m aware I can group the legends, but this wont avoid the duplication of the year in the list of legends;
  3. plotting my traces by year, and by quartile, so that if for a given trace (2013), I have 8 observations (2 in the worst quartile, 1 in the best), their respective axis got set to x1, x2 or x3. But that does not work because xaxis can’t be a list of ids. It seems that it can only be a string, which is applied to each observation of the trace.

Is it possible for 1 trace to be spread over 3 different subplots, depending on the axis we set their observations?

Is there another way to achieve the same result?

:bulb: solution: second method (see above) + showlegend for data in graph 1, 2 and 3 are set to True, legend group of each trace is set to its corresponding “year”.

Tested. This solution works; and to remove the duplicates in the legend, I used the code snippet of A. Donda (https://stackoverflow.com/a/62162555)
That is, just put the below code before returning the figure object at the end of the callback:

names = set()
fig.for_each_trace(
    lambda trace:
        trace.update(showlegend=False)
        if (trace.name in names) else names.add(trace.name))


Eventually, depending on how traces were generated, the traces’ scatter objects will need to be sorted, based on one of their attributes (name or legendgroup, e.g), with something like:

sortedData = sorted(unsortedData, key=lambda x: x.legendgroup, reverse=False)
return sortedData