Legend for continuous raster in QGIS composer

This blogpost shows in a few steps how to add a continuous colour legend for a raster layer in a map layout in QGIS. It is a work around, but it works. tags: legend, visualisation, legend

Paulo van Breugel


June 21, 2020

The better the software, the more demanding you may get as an user. But is it really too much to ask to be able to include a continuous legend for floating point raster maps in the print layout? It certainly isn’t new on the wishlist, as you can see on this Q&A. I have resorted to various solutions, including writing the GRASS GIS add-on r.out.legend to create and export raster legend as an image in GRASS GIS, which I would than add to the map layout.

Since version 3.18 QGIS has a native gradient ramp based color legend, see the corresponding changelog. So in most cases there is not need anymore for the solution explained below.

Another solution I resorted to was creating a legend in Inkscape. But then I realized that I could probably do all of this in QGIS directly. And indeed, no third-party tools required. Let me explain the steps below. Seven steps, it may seem a lot, but it really isn’t much work, you’ll see.

Step 1

Create a legend with a limited number of steps. In the image below, I have opted for 5 equal interval classes. You can use any interval, but using equal intervals makes things easier, as you will see later.

Step 2

Next, open the layout manager, create a new layout and add the raster layer (and any other layer you want) and the legend.

Step 3

The numbers in the text refer to the numbers in the figure below. Select the legend, and open the Item properties window. Under the Symbol options, change the symbol height to 0.5 mm (1) and deselect the ‘draw stroke for raster symbol’ (2). Under the Spacing options, increase the space between symbols (3).

Step 4

Zoom in on the legend, and place horizontal guides just above the upper and below the lower symbol (1). Next, draw a rectangle next the legend (2). Make the rectangle snaps to the upper and lower guides. Now, under the * Item properties* tab, click on the Style (3). This opens the Symbol settings window.

Step 5

Select as Symbol layer type the ‘gradient fill’ (1). Below, you select the radio button next to ‘Color ramp’ (2). Next, you click on the color ramp (3) to edit the color ramp. This will open the *Select Color Ramp’ window (4).

Step 6

We have a legend with five colors at equal distances. So we have stops at 0, 25, 50, 75 and 100%. We already have the first and last stop, so we need to add three stops. You can simply add a stop by double clicking on the color ramp (1). You can precisely position the stops by setting the *relative position (2).

Now select the first stop, and the sample tab (3). Click on the sample color button (4) and sample the color of the upper color of your legend (5). Likewise, select the second, third, fourth and fifth stops and sample the colors from the corresponding legend boxes.

Step 7

Now the finishing touch. Remove the guides, move the rectangle to the right to cover the legend boxes. Or if you like, keep part of the original legend symbols uncovered, so they serve as ticks. Optionally, you can group the legend and rectangle so you can move them easily around.

All in all, not the ideal solution, but not too complicated either.