histrange(v, n)

Compute nice bin ranges for the edges of a histogram of v, using approximately n bins. The resulting step sizes will be 1, 2 or 5 multiplied by a power of 10. Note: Julia does not ignore NaN values in the computation.


  1. Generate histogram bin ranges:

    julia> v = [1.5, 2.4, 3.1, 4.7, 5.9, 6.2, 7.8, 8.5, 9.6, 10.3];
    julia> histrange(v, 5)
    (1.0:2.0:9.0, 1.0, 9.0)

    This example generates approximately 5 bins for the provided vector v and returns the bin ranges (1.0:2.0:9.0) with a step size of 1.0.

  2. Generate histogram bin ranges with larger number of bins:

    julia> v = [1.5, 2.4, 3.1, 4.7, 5.9, 6.2, 7.8, 8.5, 9.6, 10.3];
    julia> histrange(v, 10)
    (1.0:1.0:10.0, 1.0, 10.0)

    In this example, n is set to 10 to generate more bins. The resulting bin ranges (1.0:1.0:10.0) have a step size of 1.0.

  3. Handle NaN values in the computation:
    julia> v = [1.5, NaN, 3.1, 4.7, NaN, 6.2, 7.8, NaN, 9.6, 10.3];
    julia> histrange(v, 4)
    (1.0:3.0:10.0, 1.0, 10.0)

    Even if the vector v contains NaN values, the histrange function includes them in the computation while generating bin ranges.

Common mistake example:

julia> v = [];
julia> histrange(v, 5)
ERROR: MethodError: no method matching histrange(::Array{Any,1}, ::Int64)

In this example, an empty array v is provided, which results in a MethodError. Make sure to pass a non-empty vector to the histrange function to avoid such errors.

See Also

cummax, eigmax, findmax, hist, hist!, hist2d, hist2d!, histrange, indmax, maxabs, maxabs!, maximum!, mean, mean!, median, median!, minabs, minabs!, minimum!, minmax, quantile!, realmax, std, stdm,

User Contributed Notes

Add a Note

The format of note supported is markdown, use triple backtick to start and end a code block.

*Required Field

Checking you are not a robot: