How can we make this comparison more clear,visible and understandable?
Secondly, I want to add the plot legends to the inside of the plot (to a suitable place).
f[j_, k_, x_] :=Piecewise[{{1, (j - 1)/4 k <= x && x <= j/4 k}, {0, True}}]; Plot[{f[1, 2, x], f[1, 3, x], f[1, 4, x]}, {x, 0, 1.5}, PlotStyle -> { {Red, Thick}, {Blue, Thick, DotDashed}, {Green,Dashed} } , Frame -> True, GridLines -> Automatic, PlotLegends -> {"f[1,2,x]", "f[1,3,x]", "f[1,4,x]"}]
4 Answers
"How can we make this comparison more clear, visible and understandable?" Given the nature of the functions, that might be impossible. But here are two approaches:
Plot[{f[1, 2, x], f[1, 3, x], f[1, 4, x]}, {x, 0, 1.5},
PlotStyle -> {{Red, Thickness[0.03]}, {Blue,
Thickness[0.02]}, {Green, Thickness[0.01]}},
PlotRangeClipping -> False, ImageSize -> Large,
PlotLegends -> Placed[{"f[1,2,x]", "f[1,3,x]", "f[1,4,x]"}, {.5, .5}],
PlotRange -> {{-0.1, 1.5}, {-0.1, 1.1}}, AxesOrigin -> {-0.1, -0.1}]
plots = Table[Plot[f[1, i, x], {x, 0, 1.5},
PlotStyle -> {{Red, Thick}},
PlotRangeClipping -> False, ImageSize -> Large,
PlotLabel -> Style["f[1, " <> ToString[i] <> ", x]", Bold, 18],
PlotRange -> {{-0.1, 1.5}, {-0.1, 1.1}},
AxesOrigin -> {-0.1, -0.1}],
{i, {2, 3, 4, 3}}];
Export["plots.gif", plots, "DisplayDurations" -> 1, "AnimationRepetitions" -> Infinity]
Clear["Global`*"]
f[j_, k_, x_] :=
Piecewise[{{1, (j - 1)/4 k <= x && x <= j/4 k}, {0, True}}];
Plot[
{f[1, 2, x] + 0.025, f[1, 3, x], f[1, 4, x] - 0.025},
{x, 0, 1.5},
PlotStyle ->
{{Red, Thick}, {Blue, Thick, DotDashed}, {Green, Dashed}},
Frame -> True,
GridLines -> Automatic,
PlotLegends ->
Placed[{"f[1,2,x]", "f[1,3,x]", "f[1,4,x]"}, {.5, .5}],
FrameTicks -> {Automatic, {0, 1}}]
This may not be suitable for all situations, but here is an attempt to visualize this in a different manner.
n1 = NumberLinePlot[{f[1, 2, x] != 0, f[1, 3, x] != 0,
f[1, 4, x] != 0}, {x, 0, 1.5},
PlotLegends ->
Placed[{"f[1,2,x]\[NotEqual]0", "f[1,3,x]\[NotEqual]0",
"f[1,4,x]\[NotEqual]0"}, {0.9, .5}],
AspectRatio -> Automatic,
ImageSize -> Large]
n2 = NumberLinePlot[{f[1, 2, x] == 0, f[1, 3, x] == 0,
f[1, 4, x] == 0}, {x, 0, 1.5},
PlotLegends ->
Placed[{"f[1,2,x]\[Equal]0", "f[1,3,x]\[Equal]0",
"f[1,4,x]\[Equal]0"}, {0.23, .5}],
AspectRatio -> Automatic,
ImageSize -> Large
]
GraphicsGrid[{{n1}, {n2}}, ImageSize -> Large]
-
1$\begingroup$ +1. This eliminates all of the wasted "white space" in the other answers. $\endgroup$– JimBCommented Sep 13, 2021 at 17:16
-
-
$\begingroup$ +1. You can play with the ordering of the inputs and option values for
Spacings
to get everything in a singleNumberLinePlot
. $\endgroup$– kglrCommented Sep 13, 2021 at 18:02 -
$\begingroup$ E.g.,
Show[NumberLinePlot[{f[1, 2, x] == 0, f[1, 3, x] == 0, f[1, 4, x] == 0, f[1, 2, x] != 0, f[1, 3, x] != 0, f[1, 4, x] != 0}, {x, 0, 1.5}, Spacings -> {1, .25, .25, 5, .25, .25, .25}, PlotStyle -> (Directive[Thick, Arrowheads[Medium], ColorData[97]@#] & /@ Range[3]), PlotLegends -> Placed[{"f[1,2,x]", "f[1,3,x]", "f[1,4,x]"}, {.8, .5}], AspectRatio -> Automatic, ImageSize -> Large], Ticks -> {Automatic, {{1.25, Framed[Style[0, 16], FrameStyle -> None, Background -> White]}, {6.75, Framed[Style[1, 16], FrameStyle -> None, Background -> White]}}}, Axes -> True]
$\endgroup$– kglrCommented Sep 13, 2021 at 18:03
I like the answers by JimB, Bob Hanlon and Syed as they are both straightforward and visually clear.
The following is an attempt to produce lines that look like multi-colored dashing. For each subset of the input function list, we identify the subdomains where they coincide, and use MeshShading
with colors associated with the subset. With the option MeshFunctions -> {"ArcLength"}
, we get n
equal length dashing pieces using option Mesh -> n
.
f[j_, k_, x_] := Piecewise[{{1, (j - 1)/4 k <= x && x <= j/4 k}, {0, True}}];
functions = f[1, #, x] & /@ {2, 3, 4};
colors = {Red, Green, Blue};
legend = LineLegend[Directive[AbsoluteThickness[3], #] & /@ colors,
{"f[1, 2, x]", "f[1, 3, x]", "f[1, 4, x]"}];
subsetdomains = Join[Thread[{List /@ Range[3], 0 <= x <= 3/2}],
{#, Reduce[{Equal @@ functions[[#]], 0 <= x <= 3/2}, x]} & /@
Subsets[Range @ 3, {2, 3}]];
layers = Plot[ConditionalExpression[functions[[#[[1, 1]]]], #[[2]]],
{x, 0, 1.5},
ImageSize -> 500,
MeshFunctions -> {"ArcLength"},
Mesh -> 20,
MeshStyle -> None,
MeshShading -> (Directive[CapForm["Butt"], AbsoluteThickness[3], #] & /@
colors[[#[[1]]]])] & /@ subsetdomains;
show = Show[layers,
GridLines -> {MapThread[{#, Directive[#2, Dashed]} &, {{.5, .75, 1}, colors}], None},
Frame -> True, Axes -> False, PlotRange -> All];
Using LocatorPane
with Appearance -> legend
we can interactively control the position of the legend inside the plot frame:
LocatorPane[{1.25, .6}, show, Appearance -> legend]
With
f[j_, k_, x_] := Piecewise[{{Sin[5 x], (j - 1)/4 k <= x && x <= j/4 k},
{Sin[20 x]/2, True}}];
and Mesh -> 30
, LocatorPane[{.2, -.5}, show, Appearance -> legend]
gives
Exclusions -> None
would probably help make the graph more legible. $\endgroup$