# How to find pixel position in a graph , given two points?

I have a graph where on my Y axis , I have price and on X axis I have period/time.

My range on Y axis is from 10 (lower limit) to 200 (upper limit), so to draw the point on graph I need the Y position of the value for example if my price is 100.50 then how should I calculate Y position of this price .

Example:

If my upper limit point is (200,HeightOfGraph) that is P1, and the lower limit point is (0,0) is P2, so if height of graph is 100 so if the value is 100 it's y position will be 50 , but how should I calculate it , is there any formula to do this ?

• It isn't very clear what you are trying to achieve, ListPlot plots points at coordinates given by {x,y} pairs. In what way does this not solve your problem ? Commented Jul 25, 2012 at 8:58
• I downvoted provisionally because it is not clear if this has anything to do with Mathematica and if you have tried anything on your own yet. Please see the FAQ and consider changing your question accordingly. Commented Jul 25, 2012 at 9:33
• Seems like you only solve tough problems, thanks anyway. Commented Jul 25, 2012 at 9:41
• It is not about tough problems at all. You simply have a much better chance of quickly getting (usually top-flight) answers from users if you pose a question well. Commented Jul 25, 2012 at 9:46
• Are you talking about finding the y value for a given x coordinate when you have a straight line graph, as in regentsprep.org/regents/math/geometry/GCG1/EqLines.htm Commented Jul 25, 2012 at 9:49

You can find the y value for a given x coordinate for a straight line by creating an interpolation function in Mathematica of order 1.

For points {0,0} and {200,h} you would do the following.

f = Interpolation[{{0, 0}, {200, h}}, InterpolationOrder -> 1]


Where h is the height of your second point.

You can then find the value of y for any x coordinate using, here for the x value 50:

f[50]


You can replace h with the value for your application to get a numeric answer, as in:

f2 = Interpolation[{{0, 0}, {200, 100}}, InterpolationOrder -> 1]


Here, for a value of h of 100.

f2[50]


25

You can plot the values for the function you have created to check that it represents your desired solution:

Plot[f2[x], {x, 0, 200}]


If you have a number of points you use ListPlot with the points' coordinates as a list of arguments. You shouldn't worry about the placing on the graph, that's what Mathematica takes care of. If you want you can change the vertical range by using PlotRange.

Example:

tungsten = {{20, 5.5}, {227, 10.5}, {727, 24.3}, {1727, 55.7}, {2727, 90.4}, {3227, 108.5}}
(* resistivity in microOhm cm at x °C *)
ListPlot[tungsten, PlotRange -> {0, 80}]


If I understand your question correctly (and it isn't entirely clear), then your question is how to plot a line, given two coordinates, one at each end. This is pretty straightforward.

Graphics[Line[{{0, 0}, {200, height}}] /. height -> 100, Axes -> True]


But it seems like you are wanting the equation, so you can work out the intermediate points. The formula I learned in high school was $y = m x + b$, where $m = \frac{rise}{run}$, where $rise$ is the difference between the $y$ coordinates and $run$ is the difference between the $x$ coordinates.

riseoverrun[{x1_, y1_}, {x2_, y2_}] :=
With[{m = (y2 - y1)/(x2 - x1)}, Flatten@{m, Solve[m x1 + b == y1, b]}]


So you can write:

riseoverrun[{0, 0}, {200, height}]


Which gives:

{height/200, b -> 0}


You can then find the equation:

First@coords x /. height -> 100 + b /. (Last@coords)
(* x/2  *)


Or if you prefer, just put the above in the Plot function:

Plot[First@coords x /. height -> 100 + b /. (Last@coords), {x, 0, 100}]


I think you can get what you want from using simple ratios as follows:

p = (v-v1)/(v2-v1)*(p2-p1) + p1


where: p is the pixel position corresponding to the value v you want, and p1 is the pixel position at the bottom of the y axis corresponding to value 'v1', and p2 is the pixel position at the top of the y axis corresponding to value v2

• Hello @hay, welcome to Mathematica.SE! I fixed up your missing parenthesis and tweaked some formatting. However, you have both p1 and P1 in your answer. Mathematica is case-sensitive. Do you mean for them to be the same? Commented Jul 27, 2012 at 3:44
• yes, thanks for the parenthesis, and P1 should be p1. I was a bit sloppy with my answer. I didn't realize mathematica was case-sensitivE.
– hay
Commented Jul 29, 2012 at 5:00