Histograms in Scala

In a histogram, the height of each bar (bin) represents the number of data points that fall into its interval. They are generally used to show the distribution of a variable.

In [ ]:
// Generate some random data

val random = new scala.util.Random

val (data1, data2) = (for {
    _ <- 1 to 10000
    v1 = random.nextGaussian
    v2 = random.nextGaussian * 2 + 1
} yield (v1, v2)).unzip

println(data1(7))
OutputCell.HIDDEN
In [ ]:
new Histogram { data = data1; binCount = 25 }
In [ ]:
new Histogram { initWidth = 800; initHeight = 200
              title = "Wide Histogram with Manual Parameters"
              xLabel = "Size"
              yLabel = "Count"
              rangeMin = -8
              rangeMax = 8
              data = data1 ++ Seq.fill(200)(7.0)
              binCount = 99
              color = new Color(0, 154, 166)}
In [ ]:
new Histogram{title = "Default is Overlap"
              data = Seq(data1, data2)
              binCount = 99
              names = Seq("old and tired", "new and improved")
              color = Seq(new Color(0, 154, 166),
                      new Color(230, 50, 50, 128) // transparent!
                     )}
In [ ]:
new Histogram{title = "Stack"
              showLegend = false
              displayMode = Histogram.DisplayMode.STACK
              data = Seq(data1, data2)
              binCount = 99}
In [ ]:
new Histogram{title ="Side by Side"
              displayMode = Histogram.DisplayMode.SIDE_BY_SIDE
              data = Seq(data1,data2)
              binCount = 55}
In [ ]:
new Histogram{title = "Cumulative"
              cumulative = true
              data = data1
              binCount = 55}
In [ ]:
new Histogram{title = "Normed, Area = 1.0"
              normed = true
              data = data1
              binCount = 55}
In [ ]:
new Histogram{log = true
              data = data1
              binCount = 99}
In [ ]: