HOW TO

Configure Legend Item for a layout in ArcGIS Pro using Python

Last Published: October 22, 2021

Summary

In ArcGIS Pro, categories to display in a layout legend can be selected in the Legend Item pane from the legend properties, as shown in the image below.

Legend Items pane in ArcGIS Pro when configuring legend properties

However, in ArcPy, the LegendItem class does not contain the method to select the categories to display. Refer to ArcGIS Pro: LegendItem for more information. To enable the category selection, the Python Cartographic Information Module (CIM) must be used in the script, and this article describes the steps to do so.

Procedure

  1. Set the desired workspace.
p = arcpy.mp.ArcGISProject('current')
lyt = p.listLayouts()[0]
  1. Get the layout CIM definition.
lyt_cim = lyt.getDefinition('V2')
  1. Iterate through all the layout elements to find the Legend element.
for elm in lyt_cim.elements:
  if elm.name == "Legend":
    #Legend item changes
    for itm in elm.items:
      if itm.name == "GreatLakes":
        #Update visibility
          itm.showLayerName = True
          itm.showHeading = False
          itm.showGroupLayerName = False
  1. Set the CIM changes back to the layout
lyt.setDefinition(lyt_cim)

The code block below demonstrates the full script.

p = arcpy.mp.ArcGISProject('current')
lyt = p.listLayouts()[0]
#Get the layout's CIM definition
lyt_cim = lyt.getDefinition('V2')

for elm in lyt_cim.elements:
  if elm.name == "Legend":
    #Legend item changes
    for itm in elm.items:
      if itm.name == "GreatLakes":
          itm.showLayerName = True
          itm.showHeading = False
          itm.showGroupLayerName = False
          itm.showLabel = True   

lyt.setDefinition(lyt_cim)

Article ID:000026663

Software:
  • ArcGIS Pro 2 8 x
  • ArcGIS Pro 2 7 x
  • ArcGIS Pro 2 x

Get help from ArcGIS experts

Contact technical support

Download the Esri Support App

Go to download options

Related Information

Discover more on this topic