An old question, but as I couldn't get the plugin mentioned above to work, here another work-around, tested on QGIS 3.12.3. From Ecodiv.earth.
- Add a legend with limited number of discrete classes (enough to capture the gradient).
- In the composer, add the raster layer and legend.
- Under symbol, reduce the height of the symbols (1), deselect the ‘draw stroke for raster symbol’ (2), and under spacing, increase the spacing between symbols (3).
- Place a rectangle next to the legend, and select as style gradient fill.
- Select the colour ramp, and then the option to edit it.
- Now, add as many stops, minus two, as you have colours (1). Place the stops at the right relative distances (2) and use the sample colour function (3, 4) to define the colours.
- Now you can move the rectangle on top of the legend symbols to complete it. Group the two together to make it easier to freely move the legend around. More details at Ecodiv.earth