Joel
Joel

Reputation: 895

How to find the % of a value on a Y axis of a LOG graph?

Wow, this one is though!

I'm trying to find in PHP the % of a value relative to the Y axis. If we refer to this graph : http://en.wikipedia.org/wiki/Semi-log_graph (2009 outbreak of influenza A), let's say that I want to find what % is a value "256" on the chart.

Visually, it's easy : it's a bit more than a 1/3 or 33%. If we look at the 1024 value, it's around 50% of the height of the Y axis. 131072 would be 100%.

So how do I calculate this with PHP?

Let's take this graph and take X = day 0 and Y = 256. What is 256 as a % of Y ?

Thanks a lot to anyone can compute this baby :)

Upvotes: 0

Views: 141

Answers (4)

John Alexiou
John Alexiou

Reputation: 29254

percent = 100 * ( log(y) - log(y1) ) / ( log(y2) - log(y1) )

where

y = value 
y1 = smallest value in y-axis
y2 = largest value in y-axis.

when y1 = 1.0 then you can simplify the other answers given here (since log(1)=0 by definition)

percent = 100 * log(y)/log(y2)

Note that not all log charts have 1.0 as the lowest value.

Upvotes: 2

hookd
hookd

Reputation: 465

If you take the log of your max y value (131072 in your case) and the log of your y value (256), you end up with a height and a y value which is linear in relation to your drawn axis. you can divide them to get a decimal of the height and times by 100 for %:

using log base 2 seen as it gives integers (though any base should be fine).

log(256) / log(131072) = 8/17 = 0.47 = 47%

in php:

(log(256, 2) / log(131072, 2))*100;

Upvotes: 0

jon_darkstar
jon_darkstar

Reputation: 16778

Alex got it, but to generalize for you and PHPize

logPercent = log(x) / log(top) * 100;

Upvotes: 0

Alex
Alex

Reputation: 14628

ln(131072) = 11.783 is 100%

ln(1024) = 6.931 is 58.824%

in PHP, that function is called log()

no need to set the base, as you are dividing them to find the relative value.

Upvotes: 0

Related Questions