At 3.16.3 I have added this legend to a map layout. Notice how the lowest value is at the top, with values increasing towards the bottom:

enter image description here

I would like to instead have the lowest value at the bottom, with values increasing towards the top, maintaining their associated color patches (blue = lowest value, pink = highest value). I used MS Paint to flip the image above to show the desired effect (of course this flipping also flips the text so that it is unreadable):

enter image description here

The QGIS documentation does not discuss such re-ordering the legend.

Nor have I found the ability to re-order in the Layer Styling panel, where the lowest value always is placed at the top:

enter image description here

Note that I'm not talking about inverting the color ramp. Rather, I want to invert the value order.


I believe this was addressed in the 3.18 release of QGIS but I haven't tried it yet. Feature: Color ramp legend improvements

Edit I tried the new feature with a raster DEM and it works as described. One thing to note though is that the Autoupdate needs to be turned off and you have to double click the legend item to access the additional item settings.

reverse legend


you can edit it manually, uncheck "Auto update" and use the arrows to reorder the values...yes, not that comfortable, but even better then flipping with MS Paint.

enter image description here


For now, here is a workaround in the form of a Python script. It's quite easy to use. Just paste the script below into a new editor in the Python console, open a print layout, select a legend in the layout, select a layer in the main window Table of contents, then run the script.

def invertLegendNodeOrder():
    open_layouts = iface.openLayoutDesigners()
    if not open_layouts:
        iface.messageBar().pushMessage('You have no open layouts')
    layout = open_layouts[0].layout()
    legend = [i for i in layout.selectedLayoutItems() if isinstance(i, QgsLayoutItemLegend)]
    if not legend:
        iface.messageBar().pushMessage('Please select a legend in an open layout')
    legend = legend[0]
    model = legend.model()
    layer = iface.activeLayer()
    layer_node = model.rootGroup().findLayer(layer)
    if not layer_node:
        iface.messageBar().pushMessage('Selected layer not found in layout legend')
    cat_count = model.legendRootRowCount(layer_node)
    if not layer or not cat_count:
        iface.messageBar().pushMessage('Please select a layer with legend nodes')
    order_property = layer_node.customProperty('legend/node-order')
    if order_property is not None: # A custom node order property has already been set
        current_order = [int(i) for i in order_property.split(',')]
        new_order = [i for i in reversed(current_order)]
        current_order = [i for i in range(cat_count)]
        new_order = [i for i in reversed(current_order)]

    QgsMapLayerLegendUtils.setLegendNodeOrder(layer_node, new_order)

See the gif below for a short demonstration.

enter image description here

As you can see it works with both vector and raster layers.

This answer is partly based on an answer to a question I asked a while ago here:

  • Would be nice to have this functionality natively. – lejedi76 Feb 10 at 10:30
  • @lejedi76, I am happy to have go at implementing this in the source code of my forked repo. If I get it working satisfactorily I will make a PR. It does seem like it could be handy to have in core. Otherwise it would also be fairly easy to make into a plugin. – Ben W Feb 10 at 11:34
  • look at this gist. I use the toc part of your code and I create a maplayerconfigwidget to add a tab in the styling panel. Must be better if it was in the symbology tab. gist.github.com/5b9709a70503e359d9143ff03adf14c7 – lejedi76 Feb 10 at 15:46
  • @lejedi76, nice work! I have been playing around with this on the C++ side, but from the opposite angle- adding a tool button to the layout legend widget. I have added a comment to Stu Smith's feature request on github. I would welcome your thoughts! – Ben W Feb 17 at 10:16
  • @lejedi76, I have removed the TOC part of my answer due it causing some undesirable behaviour in the legend. I have left a more detailed comment on your gist. Cheers – Ben W Feb 26 at 4:33

EDIT : QGIS 3.18 now can do it easily (see answer above)

An other solution from the layer style panel

You can export the color map to a file, reorder the lines with a text editor and import the file again.


  1. Export the color map to a file (1 in the picture)
  2. Open the file, reorder the lines and save the file
  3. Open the file again (2 in the picture) enter image description here
  • I'm totally agree with you :) – lejedi76 Feb 3 at 18:07
  • I prefer your "text edit" method, since it changes the display both in the main map table of contents as well as the map layout. Edit: Well, I thought that it changed the order in the TOC, but now I see that it doesn't, although it should! Although your "text edit" method works, QGIS should have the ability to do this inversion right out of the box. Therefore, I've submitted a feature request at: github.com/qgis/QGIS/issues/41341#event-4286728969 – Stu Smith Feb 3 at 18:08

In the main map window (ie not the layout) go to the symbology settings for the layer in question (dNBR in your case), and click in the 'Values' heading in the 'classes' section. That should allow you to order the entries ascending/descending as you wish, without changing the color ramp order. Then go back to the legend settings of the layout window and refresh the legend and it should reorder automatically.

  • Your solution only works on a vector layer. – lejedi76 Feb 3 at 12:57
  • 1
    Ah, yes of course. My apologies. – MI-to-QGIS Feb 8 at 0:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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