Is Toggle Visibility accessible in arcpy ArcGIS Pro

623
4
Jump to solution
03-09-2021 07:48 AM
anamanvil
New Contributor II

Hi All,

Is the below 'Toggle Visibility' accessible from arcpy for ArcGIS Pro?

anamanvil_0-1615304249359.png

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

0 Kudos
1 Solution

Accepted Solutions
jcarlson
MVP Notable Contributor

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.

- Josh Carlson
Kendall County GIS

View solution in original post

4 Replies
jcarlson
MVP Notable Contributor

A LegendItem object has a read/write property visible.

Simply call "LegendItem.visible = true / false" to toggle the visibility.

- Josh Carlson
Kendall County GIS
anamanvil
New Contributor II

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?

0 Kudos
jcarlson
MVP Notable Contributor

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.

- Josh Carlson
Kendall County GIS
anamanvil
New Contributor II

Thank you so much @jcarlson , it worked as magic!