When adding the legend to the layout, the legend displays both the label and the layer name which results in the legend showing duplicate names. I am also having trouble making items in the map not be visible in the legend, for example I don't want the "Incidents" layer to appear on the legend. To get around this I tried creating a custom style for the legend, but the issue with the duplicate names still exists. I know that the style is applied to some degree because other aspects like the legend title not showing up is still consistent with the style I made
This is the code for the legend:
# legend
legend_style_path = r"C:/Users/samir/AppData/Roaming/ESRI/ArcGISPro/Favorites.stylx"
style_name = arcpy.Describe(legend_style_path).baseName
legend_styles = p.listStyleItems(style_name, "Legend")
legend_style = legend_styles[0]
legend_x = 15
legend_y = 4
legend_width = 2.5
legend_height = 3.5
legend = lyt.createMapSurroundElement(
MakeRec_LL(legend_x, legend_y, legend_width, legend_height),
"Legend",
mf,
legend_style,
"Incident Legend"
)
legend.applyStyleItem(legend_style)
legend.autoAdd = False
legend.elementWidth = legend_width
legend.elementHeight = legend_height
In the properties box for the legend- turn off Headers.
showHeading = False # Turn off headers.
# Iterate through all layout elements to find the Legend element
for elm in lyt_cim.elements:
if elm.name == "Legend": # Legend name
# Iterate through legend items
for itm in elm.items:
if itm.name == "Willful killing of civilians": # Target specific layer
itm.showLayerName = True # Keep layer name visible
itm.showHeading = False # Turn off headers