I did some further testing, and it looks like the only reason the legend in my example is well-behaved is that as we move the slider left to right, the elements that remain in the ‘group’ column are in the order we expect them to be (ABC, then just BC as all the As are eliminated, then just C).
If you change the CDS in my example to:
master_cds = ColumnDataSource(data=dict(
x=[1, 2, 3, 4, 5, 6, 7, 8, 9],
y=[3, 2, 1, 3, 2, 1, 3, 2, 1],
color=['red', 'green', 'blue', 'red', 'green', 'blue', 'red', 'green', 'blue'],
group=['A', 'B', 'C', 'A', 'B', 'C', 'A', 'B', 'C']
))
… then you see, as you move the slider, that it also goes into party mode.
So the question is: can you impose a sort order on legend entries? and my initial searching suggests: maybe not at this time, with this specific use case. Others have been down this road before with GroupBy legends, but the examples I’ve found 1) acknowledge that this is an area for improvement, and 2) don’t take your use case of restructuring the dataset into account.