QGIS Labeling – Setting Different Colors Based on Value

colorlabelingqgis

I want to label my values in a specific color. They are all in the same shapefile which makes it complicated.

For example:

  • if value is 10, it should appear in blue
  • if value is 15, it should appear in green
  • if value is 25, it should appear in red

How can I do that?

Best Answer

If you right-click your layer and go to:

Properties > Labels

Use the Show labels for this layer option, select the field to label with and then click the data-defined button for the Color section as shown below:

Properties for labels

Then enter an expression like:

CASE 
  WHEN "Field" = 10 THEN color_rgb(0, 0, 255) 
  WHEN "Field" = 15 THEN color_rgb(0, 255, 0)
  WHEN "Field" = 25 THEN color_rgb(255, 0, 0)
  ELSE color_rgb(0, 0, 0)
END

or

CASE
  WHEN "Field" = 10 THEN '#0000ff'
  WHEN "Field" = 15 THEN '#00ff00'
  WHEN "Field" = 25 THEN '#ff0000'
  ELSE '#000000'
END

Click Apply then OK and hopefully you should see your labels with different colours:

Result


Tested on QGIS 2.16.1-Nødebo.

Related Question