Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I'm padding peaks in my graphs. In order to show the entire peak, not just the tip. purely aesthetics.
my $maxYaxis = $peak + ( $peak * .1 );
My problem: as the peaks grow larger, so does the padding. And when they shrink, so does the padding. I'm seeking help with an algorithm to do granular padding.

Replies are listed 'Best First'.
Re: Granular padding peaks in graphs
by haukex (Archbishop) on Jul 31, 2023 at 21:04 UTC

    Sounds like you might want to calculate the height of the padding based on the height of the plotting area instead of the data. But you haven't really given us enough information to give a lot of advice - for example, what plotting library you're using. Typically such styling would be the job of that library. Personally I really like Plotly.js.

        I'm using gnuplot. It has an offset parameter.. but leaves me with the same problem. PS: I like Ploty.js too! But I tend to make heavy use of fenceplots

        I see... well perhaps the plot could be emulated with Plotly's 3D Mesh plot. But an SSCCE with Gnuplot would still be helpful to have as a basis for something to play around with.

Re: Granular padding peaks in graphs
by jdporter (Paladin) on Jul 31, 2023 at 18:58 UTC

    Why not something as simple as

    my $maxYaxis = $peak + 10;
Re: Granular padding peaks in graphs
by LanX (Saint) on Aug 01, 2023 at 12:52 UTC
    Graphs are normally scaled to absolute dimensions of the user's perspective. So just add the absolute padding at the end, which fits the user.

    If you can only add relative paddings before scaling, then calculate it by dividing the absolute one thru the scaling factor.

    Cheers Rolf
    (addicted to the 𐍀𐌴𐍂𐌻 Programming Language :)
    Wikisyntax for the Monastery

Re: Granular padding peaks in graphs
by ForgotPasswordAgain (Vicar) on Aug 01, 2023 at 19:08 UTC
    If you want to round up to one less than the magnitude like this:
    $ perl -Mstrict -MPOSIX=ceil,floor -wE' for my $peak (0.1,0.9,2.1,9.9,11,22.22,35,111,265,999,1020,120010) { my $base = 10**( floor(log($peak)/log(10)) - 1 ); my $h = ceil($peak/$base) * $base; $h += $base if $h == $peak; # avoid peak = max printf "%s\t%s\n", $peak, $h; } ' 0.1 0.11 0.9 0.91 2.1 2.2 9.9 10 11 12 22.22 23 35 36 111 120 265 270 999 1000 1020 1100 120010 130000