histogram without vertical lines in Mathematica

2020-08-17 08:11发布

问题:

I am trying to make an histogram without vertical lines. I'd like to have a plot which looks like a function. Like this:

The same question has been asked for R before ( histogram without vertical lines ) but I'm on Mathematica.

I have been looking into the ChartStyle options without success.

回答1:

There probably are ways to do this by fiddling with EdgeForm[] and FaceForm[] in Histogram, but I've found it simpler to roll one on my own, whenever I need it. Here's a very simple and quick example:

histPlot[data_, bins_, color_: Blue] := Module[{
        countBorder = 
    Partition[Riffle[Riffle[#1, #1[[2 ;;]]], Riffle[#2, #2]], 2] & @@ 
     HistogramList[data, bins, "PDF"]
    },
    ListLinePlot[countBorder, PlotStyle -> color]
    ]

Doing histPlot[RandomReal[NormalDistribution[],{1000}],{-3,3,0.1}] gives

You can then extend this to take any option instead of just "PDF", and for cases when you'd like to choose the bins automatically. I dislike automatic binning, because I like to control my bin widths and extents for predictability and easy comparison against other plots.



回答2:

You could also use ListPlot with InterpolationOrder->0:

(* example data *)
data = RandomVariate[NormalDistribution[], 10^3];

hist = HistogramList[data, {.5}];

ListPlot[Transpose[{hist[[1]], ArrayPad[hist[[2]], {0, 1}, "Fixed"]}],
  InterpolationOrder -> 0, 
  Joined -> True, 
  AxesOrigin -> {hist[[1, 1]], 0}]



回答3:

Here are two methods that work in version 7, using post-processing:

rdat = RandomReal[NormalDistribution[0, 1], 200];
MapAt[
  {Blue,
   Line[# /. {{Rectangle[{x_, y_}, {X_, Y_}]}} :> Sequence[{x, Y}, {X, Y}]] } &,
  Histogram[rdat, PerformanceGoal -> "Speed"],
  {1, 2, 2, 2}
]

Cases[
  Histogram[rdat, PerformanceGoal -> "Speed"],
  Rectangle[{x_, y_}, {X_, Y_}] :> {{x, Y}, {X, Y}},
  \[Infinity]
];

Graphics[Line[Join @@ %], AspectRatio -> 1/GoldenRatio, Axes -> True]