I am making scatter plot where I want the color to depend on a discrete value. Exactly as in Discrete colors in Python, I convert the integer value to a string. However, just as in the example (third figure), the integer values are not sorted in the legend.
Plotly express does some work for you under the hood like grouping your dataframe and plot each group as plotly.graph_objects trace.
I assumed the grouping to be in sequential order of the unique values found in your feature1 column. By sorting the column, you assured the order to be ascending. You could try sorting it descending, if the order of the traces is reversed, it backs my theory
So I ran into this issue and found a solution which is applicable to your question and then some. What I do is I keep the initial pandas column as an integer, then I create a new column which is the same data just stored as a string. This allows the plotβs x axis to be sensitive to the integer values and not create a disproportionate x axis, which sometimes happens to me when I try to have a categorical x value.
So for example, I have a column called reference_value and I want to plot the endpoints of some kinetic data and donβt want it to be a continuous color plot I do this: