# Plotting step functions with filling option

Try plotting several step functions and filling the space between the first function and the axis. Unwanted vertical lines appear. Making things worse, these vertical lines are colored in the color of the first function.
Could any expert help me remove all vertical lines?

Plot[{UnitStep[t - 0.75] + 0.1, 0.5 + UnitStep[t - 0.5],
1 + UnitStep[t - 0.25]}, {t, 0, 1}, Filling -> {1 -> Axis},
FillingStyle -> Opacity[1]]


• It seems quite stubborn, same with HeavisideTheta, SquareWave and Piecewise. – b.gates.you.know.what May 29 at 16:28

I don't understand the behavior you're seeing, but you can fix it by specifying a list as the FillingStyle:

Plot[
{
UnitStep[t-0.75]+0.1,
0.5+UnitStep[t-0.5],
1+UnitStep[t-0.25]
},
{t,0,1},
Filling->{1->Axis},
FillingStyle->{None,Opacity[1]}
]


Thank you so much, Carl. But for more complicated step functions, white vertical lines appear. Please see below. Any idea to make the plot look nicer (i.e., completely fill the space between the two functions) will be greatly appreciated. (Note: I an adjust ImageSize to get rid of some white gaps, but my work involves more complicated step functions and adjusting ImageSize cannot remove all the gaps.)

Plot[
{
0.5 UnitStep[t - 0.6] + 0.5 UnitStep[t - 0.8] + 0.1,
0.5 + UnitStep[t - 0.5],
1 + UnitStep[t - 0.25]
},
{t, 0, 1},
Filling -> {1 -> Axis, 2 -> {1}, 3 -> {2}},
FillingStyle -> {None, Opacity[1]},
ImageSize -> 460
]


• It would be better to add this to your question, although it seems that you have two accounts, so that might be difficult. At any rate, what Mathematica version and OS do you have? I tried your code on M11 and M12 on OSX, and don't see any white lines. – Carl Woll May 30 at 4:02
1. use the option Exclusions -> None to prevent extra lines for exclusions
2. post-process to remove the vertical segments of line objects

Using an example that combines OP's two examples:

plot = Plot[
{0.5 UnitStep[t - 0.6] + 0.5 UnitStep[t - 0.8] + 0.1,
0.5 + UnitStep[t - 0.5], 1 + UnitStep[t - 0.25] },
{t, 0, 1},
Filling -> {1 -> Axis, 2 -> {1}},
FillingStyle ->  Opacity[1],
ImageSize -> Large
]


You can add the option Exclusions -> None to get

If you want to get rid of the vertical lines you can post-process plot to remove vertical segments of lines:

Normal[plot] /. Line[x_, ___] :> {CapForm["Butt"], Line[SplitBy[x, Last]]}