English

How To: Configure Legend Item for a layout in ArcGIS Pro using Python

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)

Related Information

Last Published: 10/21/2021

Article ID: 000026663

Software: ArcGIS Pro 2.8.3, 2.8.2, 2.8.1, 2.8, 2.7.4, 2.7.3, 2.7.2, 2.7.1, 2.7, 2.6.7, 2.6.6, 2.6.5, 2.6.4, 2.6.3, 2.6.2, 2.6.1, 2.6