1

I have a blank map. And I have a shapefile. I would like to add the shapefile to the map document, change its colour (which I figured out using @AlexTereshenkov's answer to Adding a layer with specific color to map in ArcPy) and set its transparency.

My plan is to make a layer from the shapefile (MakeFeatureLayer_management), add the resulting layer to the map document (mapping.AddLayer) and then change the transparency using arcpy.transparency.

As it was explained to me in the other question, I need to reference the map layer not the feature layer. I assume the same applies for setting the layer's transparency. I tried both:

import arcpy, os
### Set directory
arcpy.env.workspace = ...
arcpy.env.overwriteOutput = 1
### Define inputs
yellow = "symbology/yellow.lyr"
city = boston
year = 2000
# Set map document
mxd_city_year = arcpy.mapping.MapDocument(r"...\blank_map.mxd")
DF = arcpy.mapping.ListDataFrames(mxd_city_year)[0]
# Add layers
arcpy.MakeFeatureLayer_management("states/continental_US.shp", "us")
basis = arcpy.mapping.Layer("us")
arcpy.mapping.AddLayer(DF, basis, "AUTO_ARRANGE")
map_lyr = arcpy.mapping.ListLayers(mxd_city_year, wildcard="us", data_frame=DF)[0]
arcpy.ApplySymbologyFromLayer_management(map_lyr, yellow)
# Set transparency (the part that does not work!)
map_lyr.transparency = 0
basis.transparency = 0
# Save map
mxd_city_year.saveACopy("thresh_" + city + "_" + year + ".mxd")

However, neither referencing the feature layer (basis.transparency = 0) nor the map layer (map_lyr.transparency = 0) seems to work. Does anyone know why?

asked Mar 26, 2017 at 14:07
2
  • 4
    Setting the transparency value to 0 will make the layer not transparent. What happens when you set transparency to 50? Commented Mar 26, 2017 at 14:30
  • Yes, thank you! I was so focused on what to reference that I did not even check whether 0 is completely transparent or whether it is completely in-transparent. Thank you! Commented Mar 26, 2017 at 15:35

1 Answer 1

3

You have to use the map layer object; you don't need to use the feature layer object (basis variable).

You are doing everything correct; you have to change the value of the transparency attribute of the map_lyr object.

From the Help page arcpy.mapping.Layer:

A layer's transparency value. This enables you to see through a layer. Use values between 0 and 100. A value of 0 is not transparent. A transparency value of more than 90% usually results in the layer not being drawn at all.

So, if you would like to make your layer slightly transparent, you would set transparency to be somewhere between 0 and 30 percent, i.e. map_lyr.transparency = 30.

answered Mar 26, 2017 at 15:32
0

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.