After extracting tree heights and NDVI values across an estate, you often want to compare how different species or varieties are distributed. Are mango trees taller on average than coconut? Is NDVI higher in one block versus another? This script generates the comparison charts automatically.
What it does
The plot_attribute_histograms.py script reads numeric fields from a vector layer, groups the features by a category field, and plots one histogram per field, side by side, for each group. For a tree survey that means setting it to height and NDVI_median, grouped by tree_type:
- Left: height distribution (in metres)
- Right: NDVI_median distribution
Each chart is displayed on screen as a pop-up and saved as a PNG alongside the source file. It isn’t limited to trees: leave columns = None to plot every numeric field in the layer, or set group_by = None to plot all features in one chart.
Here’s the real output from a coconut plantation survey — the last step in a complete per-tree health workflow that also covers buffering, height extraction, canopy masking, and NDVI sampling:

What you need
- QGIS 3.28 or newer
matplotlib— not in every QGIS install; one-time setup in the QGIS setup guide- An active vector layer with numeric fields. For the tree example:
height— numeric tree height in metres (generated bytree_heights_from_dsm_dtm.py)NDVI_median— numeric NDVI value (generated bycanopy_ndvi_median_calc.py)tree_type— string field used for grouping
- The
plot_attribute_histograms.pyscript from the Attribute Histogram Plotter product page, or the Drone Mapping, Terrain & Multispectral Analysis QGIS Package
Here’s what that looks like in the attribute table before running this script — height and NDVI_median already populated per tree, tree_type set to “Coconut”:

Steps
- Load your polygon layer (with
height,NDVI_median, andtree_typefields) and click it to make it the active layer. - Open Plugins → Python Console → Show Editor and open the script.
- At the bottom of the script, set
columns = ['height', 'NDVI_median']andgroup_by = 'tree_type'. Listing the columns keeps ID-type number fields out of the charts. - Click Run.
One PNG per tree_type value ({tree_type}_distribution.png) is saved in the same directory as the active layer. The charts are also shown on screen during the run.
Installing matplotlib
If you get an import error for matplotlib, follow the one-time package setup for Windows, macOS or Linux in the QGIS setup guide.
Preparing the data
This script is designed to be run after:
- generate_canopy_mask.py — creates the canopy mask
- canopy_ndvi_median_calc.py — adds
NDVI_medianto each polygon - tree_heights_from_dsm_dtm.py — adds
heightto each polygon
If your tree polygons started as digitized points, buffer_active_layer.py is the step before that turns each point into a crown-sized polygon in the first place.
The tree_type field is expected to already be in your dataset (from field annotation or survey data).
Get the script
plot_attribute_histograms.py is available on its own from the Attribute Histogram Plotter product page for $20, or as part of the Drone Mapping, Terrain & Multispectral Analysis QGIS Package — all 16 QGIS automation scripts at $99 USD.