HOW TO
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.
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.
p = arcpy.mp.ArcGISProject('current') lyt = p.listLayouts()[0]
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": #Update visibility itm.showLayerName = True itm.showHeading = False itm.showGroupLayerName = False
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)
Get help from ArcGIS experts
Download the Esri Support App