Hi All,
Is the below 'Toggle Visibility' accessible from arcpy for ArcGIS Pro?
Would like to make some items within the legend invisible even though they are shown in the Map Frame without the need of modifying it manually in ArcGIS Pro.
Thanks
Solved! Go to Solution.
A Legend and a LegendItem are related, but not the same. We can get individual LegendItem objects from the Legend itself, using the items property.
Here's an example:
aprx = arcpy.mp.ArcGISProject('CURRENT')
lyt = aprx.listLayouts()[0]
the_legend = lyt.listElements('LEGEND_ELEMENT')[0]
lyr_list = ['the', 'layers', 'i', 'want']
for i in the_legend.items:
if i.name in [l.name for l in lyr_list]:
i.visible = True
else:
i.visible = False
In the example given, I'm also checking the name of each LegendItem against a defined list of layer names that I want.
A LegendItem object has a read/write property visible.
Simply call "LegendItem.visible = true / false" to toggle the visibility.
Thanks for your reply @jcarlson
Not sure if I've explained myself properly.
I am not trying to make the legend invisible but the 'US_74762_SiteBoundary_ply' layer as per above screenshot so that is visible in the MapFrame but invisible in the Legend.
Could you please give me an example on how would you use the 'LegendItem.visible' to change its visibility?
A Legend and a LegendItem are related, but not the same. We can get individual LegendItem objects from the Legend itself, using the items property.
Here's an example:
aprx = arcpy.mp.ArcGISProject('CURRENT')
lyt = aprx.listLayouts()[0]
the_legend = lyt.listElements('LEGEND_ELEMENT')[0]
lyr_list = ['the', 'layers', 'i', 'want']
for i in the_legend.items:
if i.name in [l.name for l in lyr_list]:
i.visible = True
else:
i.visible = False
In the example given, I'm also checking the name of each LegendItem against a defined list of layer names that I want.
Thank you so much @jcarlson , it worked as magic!