3

I made scatter_geo plot in Python within plotly.

import plotly.express as px
import pandas as pd

rows=[['501-600','65','122.58333','45.36667'],
      ['till 500','54','12.5','27.5'],
      ['more 1001','51','-115.53333','38.08'],
      ['601-1000','54','120.54167','21.98'],
      ]

colmns=['bins','data','longitude','latitude']
df=pd.DataFrame(data=rows, columns=colmns)

fig=px.scatter_geo(df,lon='longitude', lat='latitude',color='bins',
                      opacity=0.5,
                      projection="natural earth")
fig.show()

Are there any possibility to make customise order in legend labels if I have only one trace of data?

Coz now labels in legend looks like this:

501-600
till 500
more 1001
601-1000

Dut I need to make them looks like that:

till 500
501-600
601-1000
more 1001
vestland
  • 55,229
  • 37
  • 187
  • 305
Dmitry
  • 361
  • 6
  • 21
  • Does this answer your question? [Customizing the order of legends in plotly](https://stackoverflow.com/questions/56808693/customizing-the-order-of-legends-in-plotly) – vestland May 01 '20 at 22:03
  • There are considered only case where you have several traces, but I have only one and need to customise labels in legend manually. – Dmitry May 02 '20 at 08:11
  • In order to demonstrate why *one* trace would need ordering, please consider sharing a complete example with a data samlple. And perhaps a screenshot. – vestland May 02 '20 at 09:04
  • @vestland, I've edited quesion, pls, check. Thx – Dmitry May 02 '20 at 11:13

1 Answers1

6

If you take a look at traceorder you'll see that items are displayed top-to-bottom in the same order as the input data for the "normal" option. So you can just change the order of your input data to get what you want:

enter image description here

And if you'd like to specify an arbitrary order, you can do so by defining the order in the input data. Specifying row order can ba a bit tedious, so I often transpose the dataframe, order the categories that now appear in the column, and transpose it right back to its original form:

order  = ['till 500', '501-600', '601-1000', 'more 1001']
df_ordered = df.T[order].T

With this order, the result will be the same as the figure above. Here's an updated code snippet:

Complete code:

import plotly.express as px
import pandas as pd

rows=[['501-600','65','122.58333','45.36667'],
      ['till 500','54','12.5','27.5'],
      ['more 1001','51','-115.53333','38.08'],
      ['601-1000','54','120.54167','21.98'],
      ]

colmns=['bins','data','longitude','latitude']
df=pd.DataFrame(data=rows, columns=colmns)

fig=px.scatter_geo(df,lon='longitude', lat='latitude',color='bins',
                      opacity=0.5,
                      projection="natural earth")
fig.show()


order  = ['till 500', '501-600', '601-1000', 'more 1001']
df = df.set_index('bins')
df_ordered = df.T[order].T.reset_index()
df_ordered

fig2=px.scatter_geo(df_ordered,lon='longitude', lat='latitude',color='bins',
                      opacity=0.5,
                      projection="natural earth")
fig2.show()
vestland
  • 55,229
  • 37
  • 187
  • 305
  • 1
    Then what is the way of making reorder in input data not manually, but through code in this sample? – Dmitry May 03 '20 at 07:42
  • @Dmitry Change, yes. Customize, no. Again, look at [traceorder](https://plotly.com/python/reference/#layout-legend-traceorder). To customize the order of the input data you can organize your source as a pandas dataframe and take it from there. – vestland May 03 '20 at 07:44