Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

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 ?

share|improve this question
    
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
    
Welcome to MSE! 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. –  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
    
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 –  image_doctor Jul 25 '12 at 9:49
show 1 more comment

4 Answers

up vote 4 down vote accepted

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]

Mathematica graphics

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}]

Mathematica graphics

share|improve this answer
add comment

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}]
share|improve this answer
add comment

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]

enter image description here

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}]

enter image description here

share|improve this answer
add comment

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

share|improve this answer
    
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
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.