7
$\begingroup$

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]

enter image description here

background on mathoverflow.SE

$\endgroup$
2

4 Answers 4

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

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

enter image description here

Column[{%, SmoothHistogram @ vals}]

enter image description here

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

enter image description here

$\endgroup$
6
$\begingroup$

Repository function DotPlot, with the following usage:

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

generates:

enter image description here


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.

$\endgroup$
4
$\begingroup$

When there are not hundreds of points and few ties:

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

Smooth histogram with data points

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

Smooth histogram with jittered strip plot

$\endgroup$
3
$\begingroup$

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]

enter image description here

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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