3
$\begingroup$

I am struggling to label curves in my Plot while using Manipulate and specific function already mentioned here.

Manipulate[Grid[{{Plot[{(-2 m \[Beta] - Sqrt[
       p^4 + 4 m^2 \[CapitalDelta]^2 - 4 m p^2 \[Mu] + 
        4 m^2 \[Mu]^2])/(2 m), (
      2 m \[Beta] - Sqrt[
       p^4 + 4 m^2 \[CapitalDelta]^2 - 4 m p^2 \[Mu] + 
        4 m^2 \[Mu]^2])/(
      2 m), (-2 m \[Beta] + Sqrt[
       p^4 + 4 m^2 \[CapitalDelta]^2 - 4 m p^2 \[Mu] + 
        4 m^2 \[Mu]^2])/(2 m), (
      2 m \[Beta] + Sqrt[
       p^4 + 4 m^2 \[CapitalDelta]^2 - 4 m p^2 \[Mu] + 
        4 m^2 \[Mu]^2])/(2 m)}, {p, -20, 30}, 
     MultiColorFunction[
      Function[p, 
       ColorData["Rainbow"][
        Rescale[(-p^2 + 2 m \[Mu] + Sqrt[
           p^4 + 4 m^2 \[CapitalDelta]^2 - 4 m p^2 \[Mu] + 
            4 m^2 \[Mu]^2])^2/(
         4 m^2 \[CapitalDelta]^2 (1 + 
            1/4 Abs[(-p^2 + 2 m \[Mu] + Sqrt[
               p^4 + 4 m^2 \[CapitalDelta]^2 - 4 m p^2 \[Mu] + 
                4 m^2 \[Mu]^2])/(m \[CapitalDelta])]^2)), {-1, 1}]]], 
      Function[p, 
       ColorData["Rainbow"][
        Rescale[-((p^2 - 2 m \[Mu] - Sqrt[
            p^4 + 4 m^2 \[CapitalDelta]^2 - 4 m p^2 \[Mu] + 
             4 m^2 \[Mu]^2])^2/(
          4 m^2 \[CapitalDelta]^2 (1 + 
             1/4 Abs[(
               p^2 - 2 m \[Mu] - Sqrt[
                
                p^4 + 4 m^2 \[CapitalDelta]^2 - 4 m p^2 \[Mu] + 
                 4 m^2 \[Mu]^2])/(m \[CapitalDelta])]^2))), {-1, 
          1}]]], Function[p, 
       ColorData["Rainbow"][
        Rescale[(-p^2 + 2 m \[Mu] - Sqrt[
           p^4 + 4 m^2 \[CapitalDelta]^2 - 4 m p^2 \[Mu] + 
            4 m^2 \[Mu]^2])^2/(
         4 m^2 \[CapitalDelta]^2 (1 + 
            1/4 Abs[(-p^2 + 2 m \[Mu] - Sqrt[
               p^4 + 4 m^2 \[CapitalDelta]^2 - 4 m p^2 \[Mu] + 
                4 m^2 \[Mu]^2])/(m \[CapitalDelta])]^2)), {-1, 1}]]], 
      Function[p, 
       ColorData["Rainbow"][
        Rescale[-((p^2 - 2 m \[Mu] + Sqrt[
            p^4 + 4 m^2 \[CapitalDelta]^2 - 4 m p^2 \[Mu] + 
             4 m^2 \[Mu]^2])^2/(
          4 m^2 \[CapitalDelta]^2 (1 + 
             1/4 Abs[(
               p^2 - 2 m \[Mu] + Sqrt[
                p^4 + 4 m^2 \[CapitalDelta]^2 - 4 m p^2 \[Mu] + 
                 4 m^2 \[Mu]^2])/(m \[CapitalDelta])]^2))), {-1, 
          1}]]]], PlotRange -> {-300, 300}, AxesLabel -> {p, H[p]}, 
     LabelStyle -> "ItemNumbered", 
     PlotLabels -> Placed[{"a", "b", "c", "d"}, {Scaled[1], Top}], 
     Background -> GrayLevel[.8]], 
    Plot[{(-p^2 + 2 m \[Mu] + Sqrt[
        p^4 + 4 m^2 \[CapitalDelta]^2 - 4 m p^2 \[Mu] + 
         4 m^2 \[Mu]^2])^2/(
      4 m^2 \[CapitalDelta]^2 (1 + 
         1/4 Abs[(-p^2 + 2 m \[Mu] + Sqrt[
            p^4 + 4 m^2 \[CapitalDelta]^2 - 4 m p^2 \[Mu] + 
             4 m^2 \[Mu]^2])/(m \[CapitalDelta])]^2)), -((p^2 - 
         2 m \[Mu] - Sqrt[
         p^4 + 4 m^2 \[CapitalDelta]^2 - 4 m p^2 \[Mu] + 
          4 m^2 \[Mu]^2])^2/(
       4 m^2 \[CapitalDelta]^2 (1 + 
          1/4 Abs[(
            p^2 - 2 m \[Mu] - Sqrt[
             p^4 + 4 m^2 \[CapitalDelta]^2 - 4 m p^2 \[Mu] + 
              4 m^2 \[Mu]^2])/(m \[CapitalDelta])]^2))), (-p^2 + 
        2 m \[Mu] - Sqrt[
        p^4 + 4 m^2 \[CapitalDelta]^2 - 4 m p^2 \[Mu] + 
         4 m^2 \[Mu]^2])^2/(
      4 m^2 \[CapitalDelta]^2 (1 + 
         1/4 Abs[(-p^2 + 2 m \[Mu] - Sqrt[
            p^4 + 4 m^2 \[CapitalDelta]^2 - 4 m p^2 \[Mu] + 
             4 m^2 \[Mu]^2])/(m \[CapitalDelta])]^2)), -((p^2 - 
         2 m \[Mu] + Sqrt[
         p^4 + 4 m^2 \[CapitalDelta]^2 - 4 m p^2 \[Mu] + 
          4 m^2 \[Mu]^2])^2/(
       4 m^2 \[CapitalDelta]^2 (1 + 
          1/4 Abs[(
            p^2 - 2 m \[Mu] + Sqrt[
             p^4 + 4 m^2 \[CapitalDelta]^2 - 4 m p^2 \[Mu] + 
              4 m^2 \[Mu]^2])/(m \[CapitalDelta])]^2)))}, {p, -10, 
      10}, MultiColorFunction[
      Function[p, 
       ColorData["Rainbow"][
        Rescale[(-p^2 + 2 m \[Mu] + Sqrt[
           p^4 + 4 m^2 \[CapitalDelta]^2 - 4 m p^2 \[Mu] + 
            4 m^2 \[Mu]^2])^2/(
         4 m^2 \[CapitalDelta]^2 (1 + 
            1/4 Abs[(-p^2 + 2 m \[Mu] + Sqrt[
               p^4 + 4 m^2 \[CapitalDelta]^2 - 4 m p^2 \[Mu] + 
                4 m^2 \[Mu]^2])/(m \[CapitalDelta])]^2)), {-1, 1}]]], 
      Function[p, 
       ColorData["Rainbow"][
        Rescale[-((p^2 - 2 m \[Mu] - Sqrt[
            p^4 + 4 m^2 \[CapitalDelta]^2 - 4 m p^2 \[Mu] + 
             4 m^2 \[Mu]^2])^2/(
          4 m^2 \[CapitalDelta]^2 (1 + 
             1/4 Abs[(
               p^2 - 2 m \[Mu] - Sqrt[
                p^4 + 4 m^2 \[CapitalDelta]^2 - 4 m p^2 \[Mu] + 
                 4 m^2 \[Mu]^2])/(m \[CapitalDelta])]^2))), {-1, 
          1}]]], Function[p, 
       ColorData["Rainbow"][
        Rescale[(-p^2 + 2 m \[Mu] - Sqrt[
           p^4 + 4 m^2 \[CapitalDelta]^2 - 4 m p^2 \[Mu] + 
            4 m^2 \[Mu]^2])^2/(
         4 m^2 \[CapitalDelta]^2 (1 + 
            1/4 Abs[(-p^2 + 2 m \[Mu] - Sqrt[
               p^4 + 4 m^2 \[CapitalDelta]^2 - 4 m p^2 \[Mu] + 
                4 m^2 \[Mu]^2])/(m \[CapitalDelta])]^2)), {-1, 1}]]], 
      Function[p, 
       ColorData["Rainbow"][
        Rescale[-((p^2 - 2 m \[Mu] + Sqrt[
            p^4 + 4 m^2 \[CapitalDelta]^2 - 4 m p^2 \[Mu] + 
             4 m^2 \[Mu]^2])^2/(
          4 m^2 \[CapitalDelta]^2 (1 + 
             1/4 Abs[(
               p^2 - 2 m \[Mu] + Sqrt[
                p^4 + 4 m^2 \[CapitalDelta]^2 - 4 m p^2 \[Mu] + 
                 4 m^2 \[Mu]^2])/(m \[CapitalDelta])]^2))), {-1, 
          1}]]]], PlotRange -> {-1, 1}, AxesLabel -> {p, S_ {z}[p]}, 
     AxesLabel -> {p, H[p]}, LabelStyle -> "ItemNumbered"]}}, 
  PerformanceGoal -> "Quality"], {\[Beta], 0, 100}, {\[Mu], -5, 
  100}, {m, 0.51099895}, {\[CapitalDelta], 0.1, 100}]

The main aim of this manipulate is to create two plots, each possessing 4 curves. The colour of the curves on the first graph are related to the values of the curves at the second one by function:

MultiColorFunction /: (h : (Plot | Plot3D))[{fs__}, before___, 
  MultiColorFunction[cf__], after___] := 
 Show[h[#1, before, ColorFunctionScaling -> False, 
     ColorFunction -> #2, after] & @@@ Transpose[{{fs}, {cf}}]]

It looks like this:enter image description here Now I am struggling with function PlotLables, it seems there is a mismatch between labeling and colouring the function. I would like to label each curve/function in the first plot like E_1,E_2,E_3,E_4, but when I write

PlotLabels->{"E_1","E_2","E_3","E_4"}

it tooks just the first element and assigns it to each curve/function. Thank you for your help :)

$\endgroup$
2
  • 2
    $\begingroup$ I think the example you are providing is quite large; I have some trouble reproducing your pictures with it. Since I think we can agree that in the simplest cases the PlotLabels work as expected, could you reduce the code to a minimum working example? $\endgroup$ – Gravifer Mar 2 at 11:26
  • 2
    $\begingroup$ a simpler example showing the same issue: Manipulate[ Plot[{Sin[a x], Cos[a x]}, {x, 0, 2 Pi}, MultiColorFunction[Red &, Green &], PlotLabels -> Placed[{"Sin", "Cos"}, {Scaled[1/2], Top}]], {a, 1, 5}] $\endgroup$ – kglr Mar 2 at 11:59
5
$\begingroup$

Given the way UpValues for MultiColorFunction are defined, options (such as PlotLabels, PlotStyle, Filling and possibly others) that might set values for multiple functions will cause the same issue as illustrated below using a simpler example:

Manipulate[Plot[{Sin[a x], Cos[a x]}, {x, 0, 2 Pi}, 
  PlotLabels -> Placed[{"Sin", "Cos"}, {Scaled[1/2], Top}]],
{{a, 3}, 1, 5}]

enter image description here

Manipulate[Plot[{Sin[a x], Cos[a x]}, {x, 0, 2 Pi}, 
  MultiColorFunction[Red &, Green &], 
  PlotLabels -> Placed[{"Sin", "Cos"}, {Scaled[1/2], Top}]], 
{{a, 3}, 1, 5}]

enter image description here

A work-around: Without changing MultiColorFunction we can fix the PlotLabels issue easily: Remove the option PlotLabels and wrap input functions with Callout or with Labeled to inject the labels:

Manipulate[Plot[Evaluate @ MapThread[Callout[#, #2, {Scaled[1/2], Top}] &, 
    {{Sin[a x], Cos[a x]}, {"Sin", "Cos"}}], {x, 0, 2 Pi}, 
  MultiColorFunction[Red &, Green &]], {{a, 3}, 1, 5}]

enter image description here

Additional issues illustrated in the following examples can only be fixed with a complete re-write of MultiColorFunction:

Manipulate[Plot[{Sin[a x], Cos[a x]}, {x, 0, 2 Pi}, 
  PlotStyle -> {Directive[Thick, Dotted], Directive[Thick, Dashing[Large]]}, 
  Filling -> {1 -> {Top, Yellow}, 2 -> {Bottom, Cyan}}, 
  PlotLabels -> Placed[{"Sin", "Cos"}, {Scaled[1/2], Top}]], 
 {{a, 3}, 1, 5}]

enter image description here

Manipulate[Plot[{Sin[a x], Cos[a x]}, {x, 0, 2 Pi}, 
  MultiColorFunction[Red &, Green &], 
  PlotStyle -> {Directive[Thick, Dotted], Directive[Thick, Dashing[Large]]}, 
  Filling -> {1 -> {Top, Yellow}, 2 -> {Bottom, Cyan}}, 
  PlotLabels -> Placed[{"Sin", "Cos"}, {Scaled[1/2], Top}]],
{{a, 3}, 1, 5}]

enter image description here

$\endgroup$
2
  • 1
    $\begingroup$ Thanks a lot, it seems working properly even in my a little bit more complex example :) $\endgroup$ – Timon Moško Mar 2 at 16:04
  • $\begingroup$ @TimonMoško, my pleasure. Thank you for the accept. $\endgroup$ – kglr Mar 2 at 16:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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