Plots.jl - Combined Histogram and Distribution

On the histogram, you need to pass the kwarg normalize = true to get it onto the same scale as the pmf plotted by StatsPlots.

On the truncation, you can just pass xlim to your plot, or you can actually truncate the distribution.

Below an example where I used the Lahmann data mentioned in the SO post, downloaded from here:

julia> using CSV, DataFrames, Distributions, StatsPlots

julia> batting = CSV.read("Batting.csv", DataFrame);

julia> batting = batting[batting.AB .> 500, :];

julia> data = combine(groupby(batting, :playerID), [:H, :AB] => ((h, ab) -> sum(h)/sum(ab)) => :average);

julia> fit_result = fit(Beta, data.average);

julia> histogram(data.average, label = "Data", linecolor = "white", normalize = true);

julia> plot!(fit_result, label = "Beta(91.8, 237.4)", xlim = (minimum(data.average)-0.02, maximum(data.average)+0.02))

which gives:

image

alternatively, you could have plotted Trunacted(fit_result, minimum(data.average), maximum(data.average))

6 Likes