Adding color legend to plot with colored markers

I have a plot below which plots values with extra information encoded in marker colors. What's a good pattern to add a color legend to this plot?

IE, to give user a sense what "green" means.

It's meant to plot values of $$h$$ (which are a function of $$i$$), and use $$\sqrt{\exp(-h) - (1 - h)}$$ for its color. Red when its large, and green when its small.

ClearAll["Global*"];
h = (1 + i)^-1;
d = 1000;
DiscretePlot[h, {i, 0, d/10, 2}, PlotRange -> {h /. i -> d, 1.2},
ScalingFunctions -> "Log", PlotStyle -> PointSize[Large],
ColorFunction ->
Function[{x, y}, Hue[0.4 - (Sqrt[Exp[-y] - (1 - y)])]],
PlotLabel ->
"Approximating (1-\!$$\*SubscriptBox[\(\[Lambda]$$, $$i$$]\)) with \
exp(-\!$$\*SubscriptBox[\(\[Lambda]$$, $$i$$]\))",
AxesLabel -> {"i", "\!$$\*SubscriptBox[\(\[Lambda]$$, $$i$$]\)"},
PlotLegends -> {"approx. error"}]


EDIT: Corrected error scale

Using Plot instead of DiscretePlot

\$Version

(* "13.2.1 for Mac OS X ARM (64-bit) (January 27, 2023)" *)

ClearAll["Global*"];

h = (1 + i)^-1;
d = 1000;

{errmin, errmax} = #[{0.4 - (Sqrt[Exp[-h] - (1 - h)]), 0 <= i <= d/10},
i] & /@ {MinValue, MaxValue}

(* {-0.206531, 0.39301} *)

Plot[h, {i, 0, d/10},
ScalingFunctions -> "Log",
PlotStyle -> Thick,
ColorFunction ->
Function[{x, y}, Hue[0.4 - (Sqrt[Exp[-y] - (1 - y)])]],
PlotLabel ->
"Approximating (1-\!$$\*SubscriptBox[\(λ$$, $$i$$]\)) with \
exp(-\!$$\*SubscriptBox[\(λ$$, $$i$$]\))",
AxesLabel -> {"i", "\!$$\*SubscriptBox[\(λ$$, $$i$$]\)"},
PlotLegends -> Placed[
BarLegend[{Hue[0.4 - (Sqrt[Exp[-#] - (1 - #)])] &,
{errmin, errmax}},
LegendLabel -> "approx. error"],
{.75, .55}]]


error = Sqrt[Exp[-#] - (1 - #)] &;

dp = DiscretePlot[h, {i, 0, d/10, 2}, PlotRange -> {h /. i -> d, 1.2},
ScalingFunctions -> "Log",
PlotStyle -> {FaceForm[], PointSize[Large]},
ColorFunction -> (Hue[.4 - error@#2] &),
PlotLabel ->
"Approximating (1-\!$$\*SubscriptBox[\(λ$$, $$i$$]\)) with
exp(-\!$$\*SubscriptBox[\(λ$$, $$i$$]\))",
AxesLabel -> {"i", "\!$$\*SubscriptBox[\(λ$$, $$i$$]\)"}];


You can construct the bar legend outside DiscretePlot using the option ColorFunctionScaling -> True:

barlegend = BarLegend[{Hue[.4 - error @ #] &,
.4 - {error[h /. i -> 0], error[h /. i -> 100]}},
ColorFunctionScaling -> True,
LabelingFunction -> (NumberForm[.4 + First[#3, #3], {∞, 1}] &),
LegendLabel -> Sqrt[Exp[-λ] - HoldForm[(1 - λ)]]]

Legended[dp, barlegend]


Add the option ScalingFunctions -> {Log[.4 + #] &, Exp[#] - .4 &} to BarLegend[...] above to get:

logbarlegend = BarLegend[{Hue[.4 - error @ #] &,
.4 - {error[h /. i -> 0], error[h /. i -> 100]}},
ScalingFunctions -> {Log[.4 + #] &, Exp[#] - .4 &},
ColorFunctionScaling -> True,
LabelingFunction -> (NumberForm[.4 + First[#3, #3], {∞, 1}] &),
LegendLabel -> Sqrt[Exp[-λ] - HoldForm[(1 - λ)]]];

Legended[dp, logbarlegend]


• Hm, that plot makes it look like there are some negative square roots May 10, 2023 at 9:38
• @YaroslavBulatov, please see the updated version.
– kglr
May 10, 2023 at 9:56