# How to avoid plotting lines not defined in a Which function? [duplicate]

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,
PlotLegends ->
LineLegend["Expressions", BaseStyle -> AbsoluteThickness]] • Use option: Exclusions -> None – Coolwater Jul 6 '14 at 19:24
• @coolwater it´s no a solution. I obtain the same plot – Mika Ike Jul 6 '14 at 19:30
• You can manually add exclusions, e.g., Exclusions->{10,15}, but I imagine you want a more generic solution? – ciao Jul 6 '14 at 19:50
• you can also try Piecewise. – Algohi Jul 6 '14 at 20:01
• 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. – user484 Jul 6 '14 at 20:10

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,
PlotLegends -> LineLegend["Expressions",
BaseStyle -> AbsoluteThickness]];

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 • 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. – Mika Ike Jul 7 '14 at 10:29
• @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. – Chris Degnen Jul 7 '14 at 12:14