Alphabetically Sort Legend for Plotly Express (or Graph Objects) Scatter Plot

Is there a method for sorting Legend entries alphabetically without modifying the order in which traces are added to the figure?

From what I can tell, traceorder allows the legend entry in data input order or reversed data input order.

My trace names are essentially numbers and there can be 30+ traces in a figure. So it is important to have them in order to easily find traces you want to toggle.
However, I plan to add my traces in batches (2-5 batches per figure - based off the same y-values), but Iā€™m realizing that the legend will display them in an odd order. Iā€™m trying not to change the order I add the data in for performance and memory reasons.

Any help is appreciated!

Hi @GallopingNarwhal. Unfortunately, I would not know how to to this without changing the data (traces).

1 Like