12

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.

Community
  • 1
  • 1
tos
  • 996
  • 1
  • 10
  • 20

3 Answers3

6

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

Heike
  • 24,102
  • 2
  • 31
  • 45
  • @tos For versions older than 8, you can use `BinCounts` (needs a little extra work) – Szabolcs Jan 12 '12 at 19:01
  • Thank you Heike for your input. @Szabolcs, I didn't mention I am using version 8, but the tip is handy for users of pre-8 versions. – tos Jan 12 '12 at 21:55
5

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.

abcd
  • 41,765
  • 7
  • 81
  • 98
  • Thank you yoda and Heike for your help. Yoda, Your self-contained function works very well. – tos Jan 12 '12 at 21:56
1

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

Mr.Wizard
  • 24,179
  • 5
  • 44
  • 125
  • That's pretty close to what I was using in v7 before I upgraded. I don't think these will work in v8, as there have been changes to `Histogram` – abcd Jan 13 '12 at 02:02