QGIS: How to invert the values in a layout 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


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.

Step

  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

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')
        return
        
    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')
        return
        
    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')
        return
        
    cat_count = model.legendRootRowCount(layer_node)
    if not layer or not cat_count:
        iface.messageBar().pushMessage('Please select a layer with legend nodes')
        return
        
    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)]
    else:
        current_order = [i for i in range(cat_count)]
        new_order = [i for i in reversed(current_order)]

    QgsMapLayerLegendUtils.setLegendNodeOrder(layer_node, new_order)
    model.refreshLayerLegend(layer_node)
    legend.refresh()
    
              
invertLegendNodeOrder()

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: