7
$\begingroup$

To plot two lines in black on same plot, I specified

 PlotStyle -> {{Dashing[Large], Black}, {Dashing[Medium], Black}}

Is it possible be more concise? To specify the color on once for both lines? Something like

 PlotStyle -> Black, {Dashing[Large], Dashing[Medium]}

I am aware of

PlotTheme -> Monochrome

But do not want to use it because of other settings (Frame...)

$\endgroup$

1 Answer 1

11
$\begingroup$

There are many ways to what you ask. Here are two.

Plot[{2 x + 2, 4 x}, {x, 0, 2},
  PlotStyle -> ({Black, Dashing[#]}& /@ {Large, Medium})]

Plot[{2 x + 2, 4 x}, {x, 0, 2},
  ColorFunction -> (Black&),
  PlotStyle -> {{Dashing @ Large}, {Dashing @ Medium}}]

Both of the above expressions produce the following plot.

plot

Generalization

I am moved to discuss a more general case that seems to come up quite a lot. This is when there are several functions or expression to plot in one panel and all are to have a common plot style except for one which requires custom styling to focus attention on it. I also want to make it easy to experiment with both the default styling and the call-out styling.

My approach will be to generalize the first example I gave above.

Let a number of expressions be given for plotting and assume the 3rd expression is the one that needs a call-out.

I will choose four expressions for this example, but will not rely on knowing that number in the following code.

lines = # x & /@ Range[4]
 {x, 2 x, 3 x, 4 x}

The default style for the lines will be black color, absolute thickness specification of 2 pts, and absolute dashing specification {3, 5}, also in pts. The call-out style will be red, 3 pts, and {6,,10}.

 styleDefaults = {Black, 1.5, {3, 5}};
 callOut = 3 -> {Red, 3, {6, 10}};

Note I use a rule to specify the call-out. That is because I am going to insert it with ReplacePart as you will now see.

 styleData = ReplacePart[ConstantArray[styleDefaults, Length[lines]], callOut]

The plot is then made like so:

Plot[lines, {x, 0, 2},
  PlotStyle -> (
    {#[[1]], AbsoluteThickness[#[[2]]], AbsoluteDashing[#[[3]]]} & /@ styleData)]

plot

Of course, if this were working code and not a tutorial, everything would be wrapped in a Module expression to localize the auxiliary variables..

Module[{lines, styleDefaults, callOut, styleData},
  lines = # x & /@ Range[4];
  styleDefaults = {Black, 1.5, {3, 5}};
  callOut = 3 -> {Red, 3, {6, 10}};
  styleData =
    ReplacePart[ConstantArray[styleDefaults, Length[lines]], callOut];
  Plot[lines, {x, 0, 2},
    PlotStyle ->
      ({#[[1]], AbsoluteThickness[#[[2]]], AbsoluteDashing[#[[3]]]} & /@ styleData)]]
$\endgroup$
1
  • 2
    $\begingroup$ +1 I am beginning to appreciate ReplacePart which I have previously ignored. $\endgroup$ Commented Sep 16, 2015 at 12:55

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.