Apply Graduated Colour Classification to Any Layer in QGIS

QGIStutorialGISvisualisationPython

Manually applying a graduated colour style in QGIS means opening Layer Properties, picking a field, choosing a colour ramp, and hitting Classify. If you do this on multiple layers or regularly, it adds up. This script does it in one run.

What it does

The classify_layer.py script applies a graduated yellow → green colour classification to any numeric attribute column on the first checked vector layer. Two classification methods are supported:

  • discrete — divides the value range into equal-width steps of 0.1 (e.g. 0–0.1, 0.1–0.2, etc.)
  • percentile — divides by data distribution into four quartiles (0–25%, 25–50%, 50–75%, 75–100%)

The style is applied in-place to the layer renderer — no output file is created.

What you need

  • QGIS 3.28 or newer
  • A vector layer with a numeric attribute column, checked (ticked) in the Layers panel
  • The classify_layer.py script from the GIS Utilities Pack

Steps

  1. Load your vector layer into QGIS and check (tick) it in the Layers panel.
  2. Open Plugins → Python Console → Show Editor.
  3. Paste the script. At the bottom, find the function call and set:
    • column — the name of the numeric field to classify (default: 'ndvi_mean')
    • method'discrete' or 'percentile'
  4. Click Run. The layer updates immediately in the canvas.

When to use each method

MethodBest for
discreteWhen the data range is known and consistent (e.g. NDVI 0–1)
percentileWhen you want to show relative ranking regardless of actual values

Percentile is particularly useful for height or custom scores where the range varies per dataset — it always fills the full colour spectrum even if actual values are clustered.

Get the script

classify_layer.py is part of the GIS Utilities Pack — four everyday QGIS automation scripts for $25 USD.