5
$\begingroup$

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"}]

enter image description here

$\endgroup$

2 Answers 2

7
$\begingroup$

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}]]

enter image description here

$\endgroup$
5
$\begingroup$
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]

enter image description here

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]

enter image description here

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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