histogram without vertical lines in Mathematica

2020-08-17 07:49发布

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

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.

3条回答
趁早两清
2楼-- · 2020-08-17 08:08

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

Mathematica graphics

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

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

Mathematica graphics

查看更多
可以哭但决不认输i
3楼-- · 2020-08-17 08:11

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

histogram

查看更多
等我变得足够好
4楼-- · 2020-08-17 08:22

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

enter image description here

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.

查看更多
登录 后发表回答