# 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 ? – image_doctor Jul 25 '12 at 8:58
– Yves Klett Jul 25 '12 at 9:33
Seems like you only solve tough problems, thanks anyway. – Praveen Jul 25 '12 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. – Yves Klett Jul 25 '12 at 9:46
– image_doctor Jul 25 '12 at 9:49
show 1 more comment

## 4 Answers

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? – Verbeia♦ Jul 27 '12 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 Jul 29 '12 at 5:00
lang-mma
{"extraction_info": {"found_math": true, "script_math_tex": 0, "script_math_asciimath": 0, "math_annotations": 0, "math_alttext": 0, "mathml": 0, "mathjax_tag": 0, "mathjax_inline_tex": 6, "mathjax_display_tex": 0, "mathjax_asciimath": 0, "img_math": 0, "codecogs_latex": 0, "wp_latex": 0, "mimetex.cgi": 0, "/images/math/codecogs": 0, "mathtex.cgi": 0}, "config": {"markdown_headings": true, "markdown_code": true, "boilerplate_config": {"ratio_threshold": 0.3, "absolute_threshold": 10, "end_threshold": 15, "enable": false}, "remove_buttons": true, "remove_image_figures": true, "remove_link_clusters": true, "table_config": {"min_rows": 2, "min_cols": 3, "format": "plain"}, "remove_chinese": true, "remove_edit_buttons": true, "extract_latex": true}, "english_confidence": 0.8881402611732483, "perplexity_flag": "middle"}