4
$\begingroup$

Suppose, I have the following list with {x, f(x)} tuples:

fdata = {{2, 100}, {6, 90}, {10, 80}, {14, 65}, {18, 40}},

that can be plotted as:

ListPlot[fdata, Joined -> True, Frame -> True, FrameLabel -> {"x", "f(x)"}].

However, there is another list of data corresponding to the same values of 'x' containing {x, g(x)} tuples:

gdata={{2, 0.1}, {6, 0.3}, {10, 0.37}, {14, 0.26}, {18, 0.05}}.

I want to ListPlot fdata such that the color density of the plot depends on gdata. I also want to add a BarLegend to the plot to indicate the variation of ‘g(x)’. Could you please help?

$\endgroup$

1 Answer 1

5
$\begingroup$
Clear["Global`*"]

fdata = {{2, 100}, {6, 90}, {10, 80}, {14, 65}, {18, 40}};

gdata = {{2, 0.1}, {6, 0.3}, {10, 0.37}, {14, 0.26}, {18, 0.05}};

g = Interpolation[gdata];

gmin = MinValue[{g[x], 2 <= x <= 18}, x]

(* 0.05 *)

gmax = MaxValue[{g[x], 2 <= x <= 18}, x]

(* 0.37038 *)

Legended[
 ListPlot[fdata,
  Joined -> True,
  Frame -> True,
  FrameLabel -> (Style[#, 14] & /@ {"x", "f(x)"}),
  ColorFunction -> Function[{x, y},
    ColorData["Rainbow"][Rescale[g[x], {gmin, gmax}]]],
  ColorFunctionScaling -> False],
 BarLegend[{"Rainbow", {gmin, gmax}},
  LegendLabel -> HoldForm[g[x]]]]

enter image description here

$\endgroup$

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.