<p>Before you construct the figure could you try sorting the values in the dataframe in the order you require? Thus set the True/False flag according to what you want.</p>
<pre><code>orderedDF = df.sort_values(
  by=col_label, 
  ascending=True)
</code></pre>
<p>then add the following into the plot denoting the labels:</p>
<pre><code>sort=False
</code></pre>
<p>Thus added below.</p>
<pre><code>fig = px.box(
    orderedDF,
    x=selected_x,
    y=selected_y,
    points='all',
    hover_data=hover_data,
    color=colour_by,
    width=800,
    height=600,
    labels={
        selected_y: &quot;{} {}&quot;.format(selected_y_gene, selected_y),
        selected_x: &quot;{} {}&quot;.format(selected_x_gene, selected_x),
# I think it should go here
        sort=False
    }
    )
</code></pre>
<p>The gist is you are wanting to stop the automated re-ordering and force the dataframe order onto it.</p>

CC BY-SA 4.0
2022-03-28T16:58:36.310Z
2022-03-28T18:21:30.470Z
2022-03-28T18:21:30.470Z
M__
<p>I am using Plotly dash for creating a scatter plot. The legends I am getting in the final figure are randomly placed. I want to sort labels alphabetically from A to Z with <code>category_orders</code>.</p>
<pre><code>fig = px.box(
        df,
        x=selected_x,
        y=selected_y,
        points='all',
        hover_data=hover_data,
        color=colour_by,
        width=800,
        height=600,
        labels={
            selected_y: &quot;{} {}&quot;.format(selected_y_gene, selected_y),
            selected_x: &quot;{} {}&quot;.format(selected_x_gene, selected_x),
        },
</code></pre>

<python>
How to sort legends alphabetically in plotly dash with category_orders function
# How to sort legends alphabetically in plotly dash with category_orders function

(Asked by: username_0 on 2022-03-28)

I am using Plotly dash for creating a scatter plot. The legends I am getting in the final figure are randomly placed. I want to sort labels alphabetically from A to Z with category_orders.
fig = px.box(
        df,
        x=selected_x,
        y=selected_y,
        points='all',
        hover_data=hover_data,
        color=colour_by,
        width=800,
        height=600,
        labels={
            selected_y: "{} {}".format(selected_y_gene, selected_y),
            selected_x: "{} {}".format(selected_x_gene, selected_x),
        },

**Tags:** python

---

## 1 answers

### Answer by username_1 on 2022-03-28. Score: 2 | Accepted answer

Before you construct the figure could you try sorting the values in the dataframe in the order you require? Thus set the True/False flag according to what you want.
orderedDF = df.sort_values(
  by=col_label, 
  ascending=True)

then add the following into the plot denoting the labels:
sort=False

Thus added below.
fig = px.box(
    orderedDF,
    x=selected_x,
    y=selected_y,
    points='all',
    hover_data=hover_data,
    color=colour_by,
    width=800,
    height=600,
    labels={
        selected_y: "{} {}".format(selected_y_gene, selected_y),
        selected_x: "{} {}".format(selected_x_gene, selected_x),
# I think it should go here
        sort=False
    }
    )

The gist is you are wanting to stop the automated re-ordering and force the dataframe order onto it.


---

