# How to plot zeros of a function as a thin stripe?

I have this function for $$0

f[x_] := Cos[x] + 2 Cos[2 x];


and I want to graphically represent the points where this function is zero, as a thin line stripe (not circles) at $$y=4$$ with the following PlotStyle (something like the attached picture. There I have added those black lines manually, so they may not be in the correct position)

PlotStyle -> Directive[Black, CapForm["Butt"], Opacity[1], Thickness[.001]]


I wonder if someone could please show code to do that.

Define a function that draws short lines at the given positions p.

marker[p_] := Line[{p - {0, .1}, p + {0, .1}}];

f[x_] := Cos[x] + 2 Cos[2 x];
points = {#[[1, 2]], 4} & /@ NSolve[f[x] == 0 && 0 < x < 20, x];

Plot[f[x], {x, 0, 20},
PlotStyle ->
Directive[Black, CapForm["Butt"], Opacity[1], Thickness[.001]],
Prolog -> marker /@ points, PlotRange -> {Automatic, {-2.5, 4.5}}]


Use NSolve to calculate f=0 points

f[x_] := Cos[x] + 2 Cos[2 x];

points = {#[[1, 2]], 4} & /@ NSolve[f[x] == 0 && 0 < x < 20, x];
(*Out: {{0.935929, 4}, {2.57376, 4}, ...} *)


Use Prolog to draw points on plot + increase the PlotRange to make sure you can see the points:

Plot[f[x], {x, 0, 20},
PlotStyle -> Directive[Black, CapForm["Butt"], Opacity[1], Thickness[.001]],

Prolog -> Point[points],
PlotRange -> {Automatic, {-2.5, 4.5}}
]


• Thanks. But as I mentioned in the question, I need those points as thin lines, not circles.
– user76607
Feb 4, 2021 at 15:35
• @sara96 You can use any type of graphics you like in Prolog, they don’t have to be points. You could use Line or Rectangle to reproduce the shape you want. Feb 4, 2021 at 18:20

My own preference is to use the MeshFunctions option of Plot[] to get the positions of the stripes (which avoids a separate use of Solve[]/NSolve[]), and then post-process accordingly:

Show[Normal[Plot[Cos[x] + 2 Cos[2 x], {x, 0, 20}, Mesh -> {{0}},
MeshFunctions -> {#2 &},
MeshStyle -> Directive[Black, CapForm["Butt"],
Opacity[1], Thickness[0.001]]]] /.
Point[{x_, y_}] :> Line[{Scaled[{0., -0.01}, {x, 4.}], Scaled[{0., 0.01}, {x, 4.}]}],
PlotRange -> All]


f[x_] := Cos[x] + 2 Cos[2 x]


Yet another purely graphical method to identity and mark the zeros (without the need to use Solve/NSolve or post-processing) is to use ContourPlot:

Plot[f[x], {x, 0, 20},
Epilog -> ContourPlot[f[x] == 0, {x, 0, 20}, {y, 3.75, 4.25},
ContourStyle -> Black][[1]],
PlotRange -> {-2.5, 4.5}]


We get the same picture using:

Show[Plot[f[x], {x, 0, 20}],
ContourPlot[f[x] == 0, {x, 0, 20}, {y, 3.75, 4.25}, ContourStyle -> Black],
PlotRange -> All]

f[x_] := Cos[x] + 2 Cos[2 x]


You can use NumberLinePlot to identify and mark the zeros:

nlp = NumberLinePlot[f[x] == 0, {x, 0, 20}, Spacings -> 4, ImageSize -> Large,
PlotStyle -> Directive[Black, CapForm["Butt"], Opacity[1], Thickness[.001]]] /.
Point[x_] :> Line[Offset[{0, #}, x] & /@ {-5, 5}]


You can use nlp with Show

Show[Plot[f[x], {x, 0, 20}], nlp, PlotRange -> All, ImageSize -> Large]


or use nlp[[1]] as Epilog or Prolog in Plot:

Plot[f[x], {x, 0, 20}, Epilog -> nlp[[1]], PlotRange -> {-2.5, 4.5}, ImageSize -> Large]