Select to view content in your preferred language

Need help with legend order after python script

223
3
03-04-2025 01:50 AM
HackRentmeesters
New Contributor

Hello, I'm having trouble with the ordering of my legend.

When I export a map using Python, the layer order automatically changes to alphabetical.

For example, instead of the correct order (1–15), my legend appears as:
1-10-11-12-13-14-15-2-3-4-5-6-7-8-9

I'm running the Python script in a PythonCaller within FME. When exporting manually in ArcGIS Pro, this issue does not occur—it only happens when exporting through my FME workspace and Python script. The layer order setting is turned off, but even when I turn it on, it doesn't make a difference. In the python script itself, there is no piece of code regarding this sorting or regarding the legend.

Is there a way to prevent this automatic alphabetical sorting? Thank you in regard!

 

0 Kudos
3 Replies
DanPatterson
MVP Esteemed Contributor

That sounds like an FME issue.  

If you are coding everything, you can produce a "natural sort" of the text representation of numbers.  It is a known issue when sorting numbers that are now text.

python - Is there a built in function for string natural sort? - Stack Overflow

there are a number of options additions that you could make to the sorting.


... sort of retired...
0 Kudos
HaydenWelch
MVP Regular Contributor

I usually just use a dead simple regex lambda and pass that as a sort key:

import re
natsort = lambda s: [int(t) if t.isdigit() else t.lower() for t in re.split(r'(\d+)', s)]

>>> vals = ['1', '2', '3', '4', '10', '20', '30', '40']
>>> vals.sort()
>>> vals
['1', '10', '2', '20', '3', '30', '4', '40']

>>> vals.sort(key=natsort)
>>> vals
['1', '2', '3', '4', '10', '20', '30', '40']
0 Kudos
TonyAlmeida
MVP Regular Contributor

Maybe look into matplotlib?

0 Kudos