Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

I create various layers programmically with Python in ArcMap 10. I can adjust how the layers are sequenced using the arcpy.mapping.MoveLayer command. But I want to have the legend reflect a different order. I have yet to figure out how to do this in Python. I have tried using legend.autoAdd=True or False. I think I am close in using:

legend=arcpy.mapping.ListLayoutElements(mxd,"LEGEND_ELEMENT","My Legend") [0]

and then legend.items.something. I have four legend items A, B, C, and D which appear incorrectly as D, C, A, B Can anyone provde a link to code examples or better documentation?

share|improve this question

2 Answers 2

With the AddLayer method you cannot be more precise than "TOP" or "BOTTOM" as shown above.

However, if you use the InsertLayer method (ESRI Help Pages), you can specify a reference layer (or multiple reference layers) and add your new layer in reference to that.

import arcpy
mxd = arcpy.mapping.MapDocument(r"C:\Project\Project.mxd")
df = arcpy.mapping.ListDataFrames(mxd, "County Maps")[0]
refLayer = arcpy.mapping.ListLayers(mxd, "Lakes", df)[0]
insertLayer = arcpy.mapping.Layer(r"C:\Project\Data\Rivers.lyr")
arcpy.mapping.InsertLayer(df, refLayer, insertLayer, "BEFORE")
del mxd, insertLayer
share|improve this answer

As near as I can tell (from working with the mapping class a couple of days ago), legend items themselves are created only using auto-add. I have found no methods for manipulating layer names and positions in the legend directly. The mapping class is not all-powerful in my experience, and is generally only as useful as the .mxd it is combined with. Most fine-tuning seems to be best left to ArcMap's UI.

That being said, you can order your elements by adding them to your map in the same order they are to appear in your legend.

(This is taken mostly from arcpy's documentation):

df = arcpy.mapping.ListDataFrames(mxd, "New Data Frame")[0]  
lyrA = arcpy.mapping.Layer(r"C:\Path\LayerA.lyr")  
lyrB = arcpy.mapping.Layer(r"C:\Path\LayerB.lyr")  
lyrC = arcpy.mapping.Layer(r"C:\Path\LayerC.lyr")  
lyrD = arcpy.mapping.Layer(r"C:\Path\LayerD.lyr")  
legend = arcpy.mapping.ListLayoutElements(mxd, "LEGEND_ELEMENT", "Legend")[0]  
legend.autoAdd = True  
arcpy.mapping.AddLayer(df, lyrA, "BOTTOM")  
arcpy.mapping.AddLayer(df, lyrB, "BOTTOM")  
arcpy.mapping.AddLayer(df, lyrC, "BOTTOM")  
arcpy.mapping.AddLayer(df, lyrD, "BOTTOM")  
del mxd
share|improve this answer
I didn't have time to test an example, but I was wondering if you can change the index value for a layer to change the order. I am guessing not, but it might be possible. –  DavidF Mar 8 '11 at 14:34
AFAIK, you cannot index the elements inside the legend itself. However, changing the layer orders within the ToC might do it. –  Nathanus Mar 8 '11 at 17:16

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.