# How to plot a legend according to a given scale, separately?

Suppose I have plot a ListDensityPlot graph, the value of the density is between xmin and xmax, and the change of the plot color is given by Log[x/xmin]/Log[x/xmax]. Now I want to create a corresponding legend, how to achieve this?

Let xmin=0.3, xmax=15, the scaling of the color should vary according to the log scale, not linear, the effect should more or less look like this:

• A concrete example might be useful here. Is the issue primarily the scaling in the legends or simply the desire for two legends, each of which you can figure out the details yourself? Commented Mar 3, 2017 at 3:10
• @Mr.Wizard I mean the scaling. Commented Mar 3, 2017 at 6:19
• I don't see a V10-specific issue here. The question is valid in all versions, and all answers so far work in versions 9 & up (that I can test). The version-10 does not seem justified. Commented Mar 12, 2017 at 22:02

Not sure if this is what you meant, but

xmin = 0.3; xmax = 15;
f[x_] := Log[x/xmin]/Log[x/xmax]
minmax = {f[xmin], f[xmax - 10^-10]};
cf = Blend[{Red, Green}, Rescale[f[#], minmax]] &;


then

BarLegend[{cf, {0.3, 15}}, ScalingFunctions -> {Log, Exp}]


• I have no ScalingFunctions, my version is 10. Commented Mar 4, 2017 at 0:56
• @buzhidao This works in V10 & V9 for me. The automatic tick marks in V9 are a bit awkward, but BarLegend[{"SunsetColors", {0.3, 15}}, Ticks -> {0.3, 1, 3, 10, 15}, ScalingFunctions -> {Log, Exp}, LegendLayout -> "Row"] fixes that; and works in all versions V9-11. Commented Mar 12, 2017 at 22:01
• @MichaelE2 Thanks, it works with your modifications. Commented Mar 13, 2017 at 0:42
• how was this answer accepted? The function f literally returns negative values for all x, so the BarLegend comes out all solid red, and even with a fixed f the ticks won't correspond to the correct colors returned by cf at all. Commented Oct 10, 2022 at 22:29

You can also generate the contours yourself (second argument to BarLegend). Here's a way to make a function that preserves the syntax of BarLegend but uses sort-of log scale:

$scalingFactor = E/2;$scalingRange =
Rescale[
($scalingFactor^Range[1, 10, .1]),$scalingFactor^{1, 10}
];
barLeg[{cf_, {xmin_, xmax_}}, ops___] :=

BarLegend[{(cf@Rescale[#, {xmin, xmax}] &), {xmin - .01, xmax + .01}},
Round[Rescale[\$scalingRange, {0, 1}, {xmin, xmax}], .01],
ops
]


barLeg[{