Labeling and Map Transparency: QGIS Python Programming CookBook



Learn how to label features and set map transparency in QGIS using Python from this section as part of this preview chapter, Creating Dynamic Maps from QGIS Python Programming CookBook. With 140 short, reusable recipes to automate geospatial processes in QGIS, the QGIS Python Programming CookBook teaches readers how to use Python and QGIS to create and transform data, produce appealing GIS visualizations, and build complex map layouts.

Label Features

Once your map layers are styled, the next step to creating a complete map is labeling features. We’ll explore the basics of labeling in this recipe.

Getting ready

Download the following zipped shapefile from the following URL:

Free weekly newsletter

Fill out your e-mail address to receive our newsletter!

Extract the shapefile to a directory named ms in our qgis_data shapefile.

How to do it…

We will load the point shapefile layer, then create a label object, set its properties, apply it to the layer, and add the layer to the map.

  1. First, to save space, we’ll specify the path to the shapefile:

src = “/Users/joellawhead/qgis_data/ms/MSCities_Geo_Pts.shp”

  1. Next we’ll load the layer:

lyr = QgsVectorLayer(src, “Museums”, “ogr”)

  1. Then we’ll create the labeling object:

label = QgsPalLayerSettings()

  1. Now we’ll configure the labels starting with reading the current layer settings:


label.enabled = True

  1. Then we specify the attribute for the label data:

label.fieldName = ‘NAME10’

  1. Then we can set the placement and size options:

label.placement= QgsPalLayerSettings.AroundPoint


  1. And then we commit the changes to the layer:


  1. Finally we can add the layer to the map to view the labels:


How it works…

The interesting part of labeling is the round trip read and write process to access the layer data and the assign the labeling properties. Labeling can become quite complex but this recipe covers the basics needed to get started.

Change Map Layer Transparency

Map layer transparency allows you to change the opacity of a layer so items behind it are visible to some degree. A common technique is to make a vector layer polygon partially transparent to allow underlying imagery or elevation data to add texture to the data.

Getting ready

In a directory called ms, in your qgis_data directory, download and extract the following shapefile:

How to do it…

This process is extremely simple. The transparency is just a method

  1. First we load the shapefile layer:

lyr = QgsVectorLayer(“/Users/joellawhead/qgis_data/ms/mississippi.shp”, “Mississippi”, “ogr”)

  1. Next we set the layer transparency to 50%:


  1. Finally we add the layer to the map:


How it works…

If you set the transparency to 100% the layer is completely opaque. If you set it to 0, the layer becomes completely invisible.

QGIS Python Programming CookBook

<– Previous: Real-Time Weather Data  |     Next: Working with Tools –>

Photo of author
About the author