GD::Graph and log scale
lima1
created: 2006-04-05 06:57:55
Hi! I wrote a Histogram module based on GD::Graph::bar / mixed. I need for some datasets a log scale for the y-axes. Works fine with:
 # transform data
 sub _log_scale {
        my ( $self, @y ) = @_;

        foreach my $y (@y) {
            foreach my $data (@{$y}) {
                if (!defined $data || $data <= 0) {
                    $data = undef;
                }
                else {
                    $data = log($data);
                }
            }
        }
        return @y;
    }

  # now "fix" the y labels
  sub _y_exp_format {
        my ( $v ) = @_;
        return 0 if !defined $v;
        return sprintf( "%.0f", exp($v));
    }

 if ($self->get_logscale) {
            $graph->set('y_number_format' =>          \&_y_exp_format,
                 # 'y_min_value'    => -1   );
        }

My only problem is now how to display bars of size 1 (they are of course 0 in logscale and I have a lot of them). Starting y-values at -1 does not work. Some log scaled example y values:
VAR1 = [
          [
            1,
            2,
            3,
            4,
            5,
            6,
            7,
            8,
            9,
            10,
            11,
            12,
            13,
            14,
            15,
            16,
            17,
            18,
            19,
            20,
            21,
            22,
            23
          ],
          [
            '8.51499076786104',
            '5.79301360838414',
            '5.26269018890489',
            '4.00733318523247',
            '3.04452243772342',
            '1.94591014905531',
            '2.19722457733622',
            '1.6094379124341',
            '2.30258509299405',
            '0',
            '1.09861228866811',
            '0.693147180559945',
            '1.09861228866811',
            '0',
            undef,
            undef,
            undef,
            undef,
            undef,
            undef,
            undef,
            undef,
            undef
          ],
I want undef values not drawn, but 0 drawn somehow. Is this possible?
Re: GD::Graph and log scale
created: 2006-04-05 07:54:05
A full working snippet would be helpful, showing how you read DATA, and how you draw the graph. But as a guess you could try to use "length" to filter the undef from the 0.

"length undef" will = 0, while "length 0" will be 1.


I'm not really a human, but I play one on earth. flash japh
Re^2: GD::Graph and log scale
created: 2006-04-05 08:40:09
argh, perlmonks.org rule #1: first coffee, then post.

adding 1 to scaled log values and using exp($v - 1) seems to work fine.

perlmonks.org content © perlmonks.org and lima1, zentara

prlmnks.org © 2006 edmund von der burg (eccles & toad)

v 0.03