
Adding labels
Labeling is an important part of cartographic visualization. It significantly enhances data readability and understanding. Note that labeling is applicable only for vector layers because they contain landmarks and attributes (usually names) to be shown on the map. You can reach the Labels section from the Layer Properties dialog or by going to Layer | Labeling menu. Also, there is a Label toolbar that you can turn on or off by right-clicking on the toolbars panel contextual menu and use for fast access to the labeling options.
If you want to add labels to your layer quickly, just turn on the Label this layer with option, select the attribute field to be used for labeling from the drop-down list beside it, and click on OK. Labels will be added immediately to the layer. While this works well for personal and temporary use, there are many more labeling options available for the following label properties:
- Text: This defines the main properties of the text style, such as Font, Size, Color, Type case, and so on
- Formatting: This is used to organize and format labels as multiple lines
- Buffer: Text buffering defines properties of the buffer halo like size, color, transparency, and so on
- Background: They contain background options for the labels like shape, size, color of the background, and many more
- Shadow: These are shadowing options for labels and backgrounds
- Placement: These are advanced placement options for arranging labels and avoiding overlapping
- Rendering: These are options for labels and features for fast and clear label rendering
Now, we will go through some examples of layer labeling using advanced labeling options.
Labeling a point layer
In this example, we will label the subway stations
point layer with station names and lines IDs. This means that in a label, we need to combine information from different fields, and to do this, we will use the "name" || '\n' || "line"
expression.
Note
Expressions give you advanced control over labeling options because with them, you can combine multiple fields, text, and functions to achieve the best result. The rules for constituting expressions are very simple: field_name
is written in double quotes, and text strings
in single. To merge them into a single expression, we use the ||
concatenation sign, and \n
is used to start a new line.
- From the Font drop-down list, choose for example OpenSans (or another desired font) and maximize its Size value to 9 points.
- In the Formatting section, we only set Alignment to Center.
- In the Buffer section, activate Draw text buffer. Set Buffer size to
1
mm and Pen join style to Round. - Don't forget to choose the color for buffering. Also, if you want to achieve more stylish effects, play with the Transparency slider and the Blend modes. We will not use any background options, so in this example, this section is omitted and we'll go right to the Shadow.
- Activate the Draw drop shadow toggle. From the Draw under drop-down list, select Lowest label component.
Tip
If you want to see the result without leaving the dialog window, click on the Apply button from time to time.
- In the Offset section, you can define the drop shadow angle. The value is entered manually or adjusted with the mouse arrow in a rotary switch beside.
- Note that the shadow's outline depends on the label rotation angle. If you want to ignore it, the Use global shadow toggle should be activated.
- Maximizing the Blur radius value softens the shadow, and you can achieve more complex effects using the Transparency, Color, and Blend modes.
- By minimizing or maximizing the Scale value, the shadow can be made more subtle or obvious.
- In the Placement section, activate the Offset from point mode and you will see the placement quadrants. Click on the lowest central quadrant button to place the label centered under the symbol marker. If the label is complex, as in our case, there is a chance that it will partially overlap the symbol. To avoid overlapping, use Offset X,Y, which are the values for the label's horizontal and vertical displacement. Enter a small positive Y value (for example,
3.0
) to move the label slightly lower. - Finally, in the Rendering section, activate Scale-based visibility and enter
10000
in the denominator for the maximum possible scale. This means that our labels will be visible only within the 1:1 to 1:10000 scale range. If you want to prevent QGIS from hiding overlapped labels, activate Show all labels for this layer (including colliding labels). Some labels may be rotated for better placement, or even the map canvas can be rotated. Then, you can decide whether or not to allow upside-down label rotation using Show upside down labels.
If the layer contains a lot of features, it is possible to use a number for Limit number of features to be labeled to. You can also use Discourage labels from covering features. As a result, your map will look neater. Depending on the labeling options you have chosen, your labeled layer may look similar to the following screenshot:

Labeling a line layer
The process of labeling a line layer is similar to the one described previously, but in the Placement section, you can find some line-specific options. The most important options are as follows:
- Parallel: The label is adjusted along a direction parallel to the major direction of the labeled line. This type of placement is good for conveying line curvature, but can miss small details.
- Curved: The label will be curved so as to reflect an original line's curvature. This is the best choice for labeling objects with complex geometry, such as rivers and pathways.
- Horizontal: Regardless of the line orientation, the label is always placed horizontally.
Labels can be placed above, on, or below line respectively. In the case of on-line placement, the line will be partially overlapped by the label. If you select several options at once, QGIS will define the best position and even take into account the line direction with Line orientation dependent position activated. The Distance option defines how far from the line the label will be placed (it is active only for Above/ On line positions). The maximum angle between curved characters defines how far you can bend a label. In the Rendering sections, the main line-specific options are located within Feature options. For example, here you can use Merge connected lines to avoid duplicate labels, which is very useful for working with a road network. With Suppress labeling feature smaller than, you can set up the values for small features to be ignored during labeling.
Labeling a polygon layer
In this section, we will add labels to the zipocodes
layer:
- Activate labeling and select the
zipcode
field to add labels. - In the text section, set Font to Harlow Solid Italic (or any other font you like) and enter
10
under Size. The labels are very simple, so we won't use any formatting, and instead of using buffer, we will work with Background. - Activate the Draw background toggle.
- In the Shape drop-down list, there are several options available for defining the shape of the background. You can select from simple geometries (rectangle, square, ellipse, and so on) or more sophisticated shapes with SVG. Select SVG, navigate to the training
dataset/svg
folder, and selectplate.svg
. Set its type to Buffer and adjust Size to1
mm. Also, if the SVG parameters are modifiable, you can select Fill, Border color, and Border width.
In the Placement section, you will see some polygon-specific options, as follows:
- Offset from centroid: Labels will be placed in the center of polygon within a selected quadrant and with a specified offset, if any. You can also specify exactly which centroid to use, visible or whole. If you select the visible polygon centroid, then the label visibility and placement will change dynamically while zooming and panning the map. If you select the whole polygon centroid, the labels will be static. Additionally, if you activate Force point inside polygon, the labels will tend to stay inside the polygon only.
- Around centroid: Labels will be placed in the center of the polygon within a specified distance.
- Using Perimeter: Labels will be placed along the border line (the Above/ On/ Below placement options are available, and it is possible to combine them to allow QGIS to define the best position). Activate Line orientation dependent position if you want take the line direction into account. The Distance value defines how far from the line the label will be placed, and the Repeat values vary the repetition frequency. This type of labeling is very useful for boundaries.
- Horizontal (slow): All labels will be placed horizontally and will change their position dynamically with zooming and panning to stay inside the polygon. As QGIS constantly defines the optimal position, this type of labeling can work slower for large datasets.
- Free (slow): QGIS will define the optimal position (including rotation) for the label within the current map view.
Try the different options yourself for a better understanding of label placement. The Rendering section is the same as for line layers, so you can avoid labeling some small features or limit their number.
Advanced labeling
Beside any labeling option, you can see the Data defined override button, . It provides advanced control over labeling parameters. You can do this with an expression or by using a specially created attribute field that overrides the defined settings with custom parameters. Data-defined properties are very useful when you want to distinguish between labels for objects with different properties (for example, cities and towns with different populations) or apply your own placement positions.
Let's create three types of zipcode
labels depending on population. The smaller label will be for areas where the population is less than 40000, the medium-sized label will be for a population of 40000-80000, and the biggest will be for a population of over 80000.
This means that we need to adjust the Size value of the font according to the population
field values. From the Data defined override contextual menu, select Edit…, like this:

In Expression string builder, enter the expression shown in the following screenshot. In this expression, we use the population as the condition for font size value definition:

But what if we not only want to vary the size, but also want to underline the labels with the largest values? Then, we select Edit from the Underline button from the data override contextual menu and type the following expression: case when "population" > 80000 then 1 else 0 end
.
Tip
If you are confused about exactly which values to enter for the parameter definition and how to combine them to get a meaningful result, select the Description item from Data defined override. A window, with a short explanation of the expected input, will be displayed. For example, from the following screenshot, which shows an explanation of the color input, we can conclude that if we want to change the label color according to the population, we can use case when "population" > 80000 then '255,0,0,0' else '25,81,119,0' end
:
