# How can I display two discrete plots into one? [closed]

I have two criteria for the domain of $$n\in\mathbb{N}$$:

For all even $$n$$, I want to use the constant function $$f(n)=5$$

For odd $$n$$, I want to use the function $$g(n)=9 \sin \left(\frac{\pi n^2}{5}\right)$$

And then I want to have one plot for all $$n\in\mathbb{N}$$, and if possible, to join the adjacent numbers.

f[n_] := (1 - Mod[n, 2]) 9 Sin[Pi n^2/5] + Mod[n, 2] 5

DiscretePlot[f[n], {n, 1, 20}, Joined -> True, Filling -> None,
PlotMarkers -> {"Point", .02}]


DiscretePlot[{f[n], f[n]}, {n, 1, 20}, Joined -> {False, True},
Filling -> 1 -> Axis, PlotMarkers -> {"Point", .02}]


DiscretePlot[{f[n], f[n]}, {n, 1, 20}, Joined -> {False, True},
Filling -> 1 -> Axis, PlotMarkers -> {"Point", .02},
FillingStyle -> Directive[Dashed, Opacity[1]],
ColorFunctionScaling -> False,
ColorFunction -> (If[OddQ[Round[#]], Red, Blue] &)]


f[n_?EvenQ] := 5;
f[n_?OddQ] := 9*Sin[(Pi*n^2)/5];
ListPlot[Table[f[n], {n, 1, 20, 1}], Joined -> True,
MeshStyle -> {Red}, Mesh -> {Range[1, 20, 2]}]


To distinguish two types of the points, one way is

f[n_?EvenQ] := 5;
f[n_?OddQ] := 9*Sin[(Pi*n^2)/5];
ListPlot[Table[f[n], {n, 1, 20, 1}],
Mesh -> {Range[1, 21, 2], Range[2, 22, 2]},
MeshFunctions -> {#1 &, #1 &},
MeshStyle -> {Directive[PointSize[Medium], Red], Blue},
Joined -> True]


Another way is

evenColor =
Table[{n, Directive[Red, PointSize[Large]]}, {n, 1, 21, 2}];
oddColor =
Table[{n, Directive[Blue, PointSize[Large]]}, {n, 2, 22, 2}];
meshColor = Join[evenColor, oddColor];
f[n_?EvenQ] := 5;
f[n_?OddQ] := 9*Sin[(Pi*n^2)/5];
ListPlot[Table[f[n], {n, 1, 20, 1}], Joined -> True,
Mesh -> {meshColor}]


Show[
DiscretePlot[5, {n, 0, 20, 2}, PlotStyle -> Blue],
DiscretePlot[9 Sin[Pi n^2/5], {n, 1, 20, 2}, PlotStyle -> Red],
ListLinePlot[
Table[
{n, Piecewise[{{5, EvenQ[n]}, {9 Sin[Pi n^2/5], True}}]}, {n, 0, 20}],
PlotStyle -> Directive[Dashed, Gray]],
PlotRange -> Full
]


• Thanks. But unfortunately, I get an error. The plot is not shown. – charmin Oct 27 at 23:32
• @charmin what is the error? I don't get any error and I'm on 12.1.1.0 on Windows10 – flinty Oct 27 at 23:33
• A red box containing "The specified setting for the option GraphicsBoxOptions, PlotRange cannot be used." – charmin Oct 27 at 23:35
• @charmin must be a bug in your version. Try this instead: Show[DiscretePlot[5, {n, 0, 20, 2}, PlotStyle -> Blue, PlotRange -> {-6, 6}], DiscretePlot[9 Sin[Pi n^2/5], {n, 1, 20, 2}, PlotStyle -> Red], ListLinePlot[ Table[{n, Piecewise[{{5, EvenQ[n]}, {9 Sin[Pi n^2/5], True}}]}, {n, 0, 20}], PlotStyle -> Directive[Dashed, Gray]]] – flinty Oct 27 at 23:37
• Thank you very much. Now, it works :) – charmin Oct 27 at 23:40