# Stacked NumberLinePlot?

Is there some built-in I can reuse to get a "stacked" number line plot? In other words, equivalent to NumberLinePlot, but some numbers occur more than once, so that should be reflected by stacking them.

Neither NumberLinePlot nor Histogram quite do it

(* visualize values of Tr(ABAAB...) for all permutations of ABAB.. *)
SeedRandom[1];
d = 3;  (* dimensions *)
s = 5; (* length of product *)
eigs1 = 1./Range[d];
eigs2 = 1./Range[d];
{b1, b2} = RandomVariate[CircularRealMatrixDistribution[d], 2];
A = b1 . DiagonalMatrix[eigs1] . b1\[Transpose];
B = b2 . DiagonalMatrix[eigs2] . b2\[Transpose];
Clear[a, b, singleTrajectory];
singleTrajectory[s_] := RandomChoice[{a, b}, s];
sub := {a -> A, b -> B};

numSamples = 100;
orders = Table[RandomChoice[{a, b}, s], {numSamples}];
evalOrder[order_] := Log@Tr[Dot @@ (order /. sub)];
vals = evalOrder /@ orders;
NumberLinePlot[vals]
SmoothHistogram[vals]


background on mathoverflow.SE

• Maybe a rug plot? mathematica.stackexchange.com/questions/86568/…
– JimB
Commented Mar 12, 2023 at 16:40
• Relevant link for DotPlot from Wolfram MathWorld. Can't say if it is related to the Repository function by the same name.
– Syed
Commented Mar 12, 2023 at 17:15

stack = Join[Apply[Sequence] @ Gather @ Map[List] @ #, 2] &;

NumberLinePlot[stack @ vals, PlotStyle -> ColorData[97]@1]


Column[{%, SmoothHistogram @ vals}]


Alternatively, specify the Spacings in NumberLinePlot using the maximum pdf value of SmoothKernelDistribution of vals and maximum of counts to get desired vertical scaling:

spacings = Max[SmoothKernelDistribution[vals]["PDFValues"]]/Max[Counts @ vals]/5;

Show[SmoothHistogram @ vals,
NumberLinePlot[stack @ vals,
PlotStyle -> Directive[ColorData[97]@2, AbsolutePointSize[3]],
Spacings -> spacings]]


Repository function DotPlot, with the following usage:

Show[DotPlot[vals]
, SmoothHistogram[vals]]


generates:

I had to download the source notebook and run it as ResourceFunction functionality is not working on my system. This function takes the same options as the NumberLinePlot.

When there are not hundreds of points and few ties:

SmoothHistogram[vals, Epilog -> {PointSize[Medium], Point[{#, 0} & /@ vals]}]


When there are many points (irrespective of the number of ties) a strip plot using jittering gives the reader a good view of the density of points:

(* Generate data *)
data = Join[RandomVariate[NormalDistribution[0, 1], 100],
RandomVariate[NormalDistribution[10, 3], 200]];

(* Get smooth histogram *)
sh = SmoothHistogram[data, Automatic, "PDF"];

(* Get maximum vertical value from PlotRange *)
ymax = Cases[FullForm[sh],
Rule[PlotRange, List[List[xmin_, xmax_], List[ymin_, ymax_]]] :> ymax, Infinity][[1]];

(* Jitter the data *)
dataJittered = # + 0.05 (Max[data] - Min[data]) RandomReal[{-0.5, 0.5}] & /@ data;

(* Random heights *)
y = 0.05*ymax + 0.1 ymax RandomReal[{-0.5, 0.5}, Length[data]];

(* Show resulting figure *)
Show[sh, ListPlot[Transpose[{dataJittered, y}]]]


We may first count the multiplicity of every value. Then we may create a range of points with given x value and y values from 1,2,.. until the multiplicity:

dat = Table[{#[[1]], i}, {i, #[[2]]}] & /@ Tally[vals]


Finally we may plot these points:

ListPlot[dat]