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?

  • 4
    Setting the transparency value to 0 will make the layer not transparent. What happens when you set transparency to 50? – Dan Jurgella Mar 26 '17 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! – eigenvector Mar 26 '17 at 15:35
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.