JuliaPlots/UnicodePlots.jl

log transformed bins in `histogram`

Open

#348 ouverte le 10 avr. 2023

Voir sur GitHub
 (1 commentaire) (0 réactions) (0 assignés)Julia (84 forks)batch import
enhancementhelp wanted

Métriques du dépôt

Stars
 (1 528 stars)
Métriques de merge PR
 (Merge moyen 1h 29m) (1 PR mergée en 30 j)

Description

I want to create an histogram for a vector like this:

using UnicodePlots
using Random

Random.seed!(1234)
a = rand(1:5) .^ (0:0.1:5)
histogram(a)
                ┌                                        ┐
   [ 0.0,  5.0)   ┤████████████████████████████████████  24
   [ 5.0, 10.0)  ┤███████████████  10
   [10.0, 15.0) ┤█████████  6
   [15.0, 20.0) ┤██████  4
   [20.0, 25.0) ┤████▌ 3
   [25.0, 30.0) ┤████▌ 3
   [30.0, 35.0) ┤█▌ 1
                └                                        ┘
                                 Frequency

Since the variable is exponential, log transforming it would be more appropriate:

histogram(log10.(a))
              ┌                                        ┐
   [0.0, 0.5) ┤█████████████████████████████████████  8
   [0.5, 1.0) ┤████████████████████████████████▍ 7
   [1.0, 1.5) ┤████████████████████████████████▍ 7
   [1.5, 2.0) ┤████████████████████████████████▍ 7
   [2.0, 2.5) ┤█████████████████████████████████████  8
   [2.5, 3.0) ┤████████████████████████████████▍ 7
   [3.0, 3.5) ┤████████████████████████████████▍ 7
              └                                        ┘
                               Frequency

But I was wondering if there was a way to do it so I do not need to log transform the variable myself. Something that looked like this (notice the bin labels):

              ┌                                        ┐
   [10^0, 10^0.5)    ┤█████████████████████████████████████  8
   [10^0.5, 10^1.0) ┤████████████████████████████████▍ 7
   [10^1.0, 10^1.5) ┤████████████████████████████████▍ 7
   [10^1.5, 10^2.0) ┤████████████████████████████████▍ 7
   [10^2.0, 10^2.5) ┤█████████████████████████████████████  8
   [10^2.5, 10^3.0) ┤████████████████████████████████▍ 7
   [10^3.0, 10^3.5) ┤████████████████████████████████▍ 7
              └                                        ┘
                               Frequency

I tried it with

histogram(a, yscale=:log10)
histogram(a, yscale=x->log10(x))

Interestingly the plot was created as if the yscale argument was not set, which to me was misleading. I think this should either throw an error or the behaviour should be to create logarithmic bins?

This should also probably throw an error:

histogram(a, yscale=:hello)

Guide contributeur