3
$\begingroup$

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]]

enter image description here

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

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]}
]

enter image description here

$\endgroup$
1
$\begingroup$

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
]

enter image description here

$\endgroup$
  • $\begingroup$ 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. $\endgroup$ – Carl Woll May 30 at 4:02
1
$\begingroup$
  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
  ]

enter image description here

You can add the option Exclusions -> None to get

enter image description here

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]]}

enter image description here

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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