0
$\begingroup$

I want to draw 2 functions in the same plot and I use Show for this. I am using Mathematica 10 and I still have the problem that the legend disappears for the Plot in which I use ColorFunction. The workarounds proposed in the meantime for version 9 doesn't seem to work for me...

plot1 = Plot[100*x, {x, 0, 500}, 
        PlotStyle -> {Dashed, Black, Thickness[0.007]}, 
        PlotRange -> {{0, 500*1.1}, {All, All}}, 
        PlotLegends -> {Style["   A-range", Black]}];

plot2 = Plot[100*x, {x, 200, 520}, 
        PlotStyle -> {Thickness[0.03], Opacity[0.5]}, 
        ColorFunction -> Function[{x, y}, ColorData["DarkRainbow"][y]], 
        PlotLegends -> LineLegend["DarkRainbow", "   W-range"], 
        PlotTheme -> "Scientific"];
Show[plot1, plot2]

enter image description here

Is there something I can do about this (or I am just doing (again) a simple syntax mistake)?

$\endgroup$
4
  • 1
    $\begingroup$ Simple syntax mistake LineLegend[{"DarkRainbow", " W-range"}] $\endgroup$
    – Hubble07
    Sep 17, 2015 at 13:45
  • 1
    $\begingroup$ I think Conrad wants LineLegend[{"DarkRainbow"}, {" W-range"}] to use "DarkRainbow" as the color and " W-range" as the label. $\endgroup$ Sep 17, 2015 at 14:09
  • $\begingroup$ @JackLaVigne: You're right, this was my intention. $\endgroup$
    – Conrad
    Sep 17, 2015 at 14:25
  • $\begingroup$ @Hubble07: Can you post your comment as answer, so that I will be able to mark it as well? $\endgroup$
    – Conrad
    Sep 17, 2015 at 14:26

2 Answers 2

1
$\begingroup$

The error appears due to a simple syntax error in PlotLegends for plot2. The correct syntax should be as follows:

  PlotLegends -> LineLegend[{"DarkRainbow"}, {" W-range"}]
$\endgroup$
2
$\begingroup$

The problem is not that much in the ColorFunction, but in the curly braces that are needed to be put inside the LineLegend statement. Then the PlotTheme should be equal in the both plots, or no at all. Try this:

    plot1 = Plot[100*x, {x, 0, 500}, 
   PlotStyle -> {Dashed, Black, Thickness[0.007]}, 
   PlotRange -> {{0, 500*1.1}, {All, All}}, 
   PlotLegends -> {Style["   A-range", Black]}];

plot2 = Plot[100*x, {x, 200, 520}, 
   PlotStyle -> {Thickness[0.03], Opacity[0.5]}, 
   ColorFunction -> Function[{x, y}, ColorData["DarkRainbow"][y]], 
   PlotLegends -> LineLegend[{"        W-range"}]];

Show[plot1, plot2]

enter image description here

One problem with it is that Mma does not know that you might want to show the whole rainbow, and only shows its small part.

Have fun!

$\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.