I used a python script in QGIS 3.16 to generate a raster and subsequently a print layout with legends. I am trying to use the same script to build the print layout in QGIS 3.22. However, this seems to have changed in QGIS 3.22, and I get a single bar with two values - the maximum and minimum only. I wish to generate discrete labels, as shown in the first section of the code below.


I use the following code to generate the symbology.

PT_rlayer = QgsProject.instance().mapLayersByName('Depth Colour Scale (m)')[0]
PT_s = QgsRasterShader()
PT_c = QgsColorRampShader()
PT_colour_array.append(PT_c.ColorRampItem(-6.5, QColor('#08306b'), '-6.5 m'))
PT_colour_array.append(PT_c.ColorRampItem(-5.5, QColor('#0a4da9'), '-5.5 m'))
PT_colour_array.append(PT_c.ColorRampItem(-4.5, QColor('#0b6ce9'), '-4.5 m'))
PT_colour_array.append(PT_c.ColorRampItem(-3.5, QColor('#3d91f8'), '-3.5 m'))
PT_colour_array.append(PT_c.ColorRampItem(-2.5, QColor('#79b6fc'), '-2.5 m'))
PT_colour_array.append(PT_c.ColorRampItem(-1.5, QColor('#b7dafe'), '-1.5 m'))
PT_colour_array.append(PT_c.ColorRampItem(-0.5, QColor('#f7fbff'), '-0.5 m'))
# insert array with colours and depth levels into layer
PT_ps = QgsSingleBandPseudoColorRenderer(PT_rlayer.dataProvider(), 1, PT_s)

I use the following code to add the colour bar legend:

PT_legend = QgsLayoutItemLegend(PT_layout)
PT_root = QgsLayerTree()
for layer in PT_layersToAdd:
PT_legend.attemptMove(QgsLayoutPoint(220, 25, QgsUnitTypes.LayoutMillimeters))


Screenshot of the symbology generated in QGIS 3.22 Layer Symbology generated in QGIS 3.22

Screenshot of the legend generated in the print layout QGIS 3.22 Legend in print layout.

I would like the legend display more like what is shown below Print Layout in QGIS 3.16.

1 Answer 1


Looking at the Symbology menues in 3.16 and 3.22 I noteced there's a new menu-point in 3.22: under Properties > Symbology > Legend Settings ... you can now choose Use continuous legend This is the default setting for 3.22. Under 3.16, where this menue did not exist at the same location the default setting was as if this check box was not activated. You will need to reflect this in your code accordingly. enter image description here

The change it results in is just as you describe:

  • With Use continuouse legend

enter image description here

  • Without Use continuouse legend

enter image description here

In the documentation check out the option setUseContinuousLegend(). This may be, what you need.

  • Excellent, right on target! Thank you.
    – Ashish AJ
    Commented Jul 25, 2022 at 7:14
  • I attempted to use the following lines to implement it, PT_legend_ramp = QgsColorRampLegendNodeSettings() PT_legend_ramp.setUseContinuousLegend(True) PT_legend_ramp.setOrientation(1) PT_legend_ramp.setSuffix(' m') It doesn't throw an error; however, it doesn't seem to work either.
    – Ashish AJ
    Commented Jul 25, 2022 at 9:03
  • Can I please get your support here @Vincé
    – Ashish AJ
    Commented Aug 3, 2022 at 13:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.