1
$\begingroup$

How to avoid plotting lines not defined in a Which function?

fa1[x_] := Which[x >= 15, 1, x >= 10, 0.55, x >= 5, 0.1]
Plot[fa1[x], {x, -1, 20}, BaseStyle -> AbsoluteThickness[4], 
 PlotLegends -> 
  LineLegend["Expressions", BaseStyle -> AbsoluteThickness[4]]]

enter image description here

$\endgroup$
  • 1
    $\begingroup$ Use option: Exclusions -> None $\endgroup$ – Coolwater Jul 6 '14 at 19:24
  • $\begingroup$ @coolwater it´s no a solution. I obtain the same plot $\endgroup$ – Mika Ike Jul 6 '14 at 19:30
  • 1
    $\begingroup$ You can manually add exclusions, e.g., Exclusions->{10,15}, but I imagine you want a more generic solution? $\endgroup$ – ciao Jul 6 '14 at 19:50
  • 1
    $\begingroup$ you can also try Piecewise. $\endgroup$ – Algohi Jul 6 '14 at 20:01
  • 6
    $\begingroup$ Specifically, fa1[x_] := Piecewise[{{1, x >= 15}, {0.55, x >= 10}, {0.1, x >= 5}}, Null] produces the desired plot without having to specify exclusions manually. $\endgroup$ – Rahul Jul 6 '14 at 20:10
2
$\begingroup$

You can post-process the plot to split the line into separate lines.

fa1[x_] := Which[x >= 15, 1, x >= 10, 0.55, x >= 5, 0.1]
plot = Plot[fa1[x], {x, -1, 20},
   BaseStyle -> AbsoluteThickness[4],
   PlotLegends -> LineLegend["Expressions",
     BaseStyle -> AbsoluteThickness[4]]];

linepos = Position[plot, Line];
a = Extract[plot, ReplacePart[First@linepos, -1 -> 1]];
b = Last /@ a;
c = Partition[b, 2, 1];
d = #1 == #2 & @@@ c;
e = DeleteCases[Split[d], {False}];
f = Length /@ e + 1;
g = # + {1, 0} & /@ Partition[Prepend[Accumulate[f], 0], 2, 1];
h = Take[a, #] & /@ g;
plot[[Sequence @@ ReplacePart[First@linepos, -1 -> 1]]] = h;

plot

enter image description here

$\endgroup$
  • $\begingroup$ Thank you. It´s a solution, but I think that it´s a little complicate for a Mathematica user without huge knowledge of the software. I prefer use easier solutions. $\endgroup$ – Mika Ike Jul 7 '14 at 10:29
  • $\begingroup$ @MikaIke - yes, I would use Piecewise in retrospect. Nevertheless, sometimes editing the plot output is expedient. In this particular case there may be a shorter way to subdivide the line; I just took fairly basic steps. $\endgroup$ – Chris Degnen Jul 7 '14 at 12:14

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