What is the math for proportionally plotting a chart?

ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Welcome to LinuxQuestions.org, a friendly and active Linux Community.

You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!

Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.

If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.

Having a problem logging in? Please visit this page to clear all LQ-related cookies.

Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.

Exclusive for LQ members, get up to 45% off per month. Click here for more info.

What is the math for proportionally plotting a chart?

Assuming I have a chart and know exactly how many pixels I have on width and height, I know what the highest and lowest plot values are going to be, and I want to display a y axis on the side with some regular interval values. For example:

Code:

y
50 x x x
40 x x x x
30 x x x x x
20 x x x
10 x
0
x-> 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16

I can draw that, but the plot values don't match the y axis indicator.

So my problem is, what are the x/y pixel coordinates for value 25.6, or 34.7 or 44.1?

And what if the y axis changes and goes from 70 to 210? Or from 300 to 1,700?

How do I reliably calculate the x/y pixel chart coordinates across multiple scale scenarios?

Assuming I have a chart and know exactly how many pixels I have on width and height, I know what the highest and lowest plot values are going to be, and I want to display a y axis on the side with some regular interval values. For example:

Code:

y
50 x x x
40 x x x x
30 x x x x x
20 x x x
10 x
0
x-> 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16

I can draw that, but the plot values don't match the y axis indicator.

So my problem is, what are the x/y pixel coordinates for value 25.6, or 34.7 or 44.1?

And what if the y axis changes and goes from 70 to 210? Or from 300 to 1,700?

How do I reliably calculate the x/y pixel chart coordinates across multiple scale scenarios?

TIA

For the y axis you take the total number of pixels available (say 1800 after you take account of the labels on the x axis) and divide that by the maximum value of the Y axis (say 50 in your example). This gives you the number of pixels per unit on the y axis (1800/50 = 36 pixels per unit).

For the x axis you take the total number of pixels available (say 1100 after you take account of the labels on the y axis) and divide that by the maximum value of the Y axis (say 16 in your example). This gives you the number of pixels per unit on the y axis (1100/16 = 68 pixels per unit).

You can make your graph more readable if you leave a few extra pixels around all four sides to make a clean border.

You can also make a more readable graph if you only include the data range on the y axis. For example if your data was all in the range of 30 to 45 then showing a range of 25 to 50 gives a nicer presentation than a range of 0 through 50. In this case the number of pixels per unit on the y axis would be (1800/25 = 72). The formula for where to place a dot d on the y axis would be ((d-25) * 72).

A similar exercise can be done on the x axis when the label does not start at 0.

For example if your data was all in the range of 30 to 45 then showing a range of 25 to 50 gives a nicer presentation than a range of 0 through 50. In this case the number of pixels per unit on the y axis would be (1800/25 = 72). The formula for where to place a dot d on the y axis would be ((d-30) * 120).

Excellent answer, thank you. But the part I really needed is still not clear. Why 120? Where did that number come from?

Number of pixels = 1800
range = 25 to 50
1800/25 = 72
dot d = ((d-25) * 72)
Assuming dot d = 10, which is close to the bottom (pixel 0)
dot 10 = ((10-25) * 72)
dot 10 = (-15 * 72)
dot 10 = -1080.0

So dot 10 would be plotted at -1080 in the y axis? That is wrong. It shouldn't be negative.

Let's try another value for dot d.

Number of pixels = 1800
range = 25 to 50
1800/25 = 72
dot d = ((d-25) * 72)
Assuming dot d = 45, which is close to the top (pixel 1800)
dot 45 = ((45-25) * 72)
dot 10 = (20 * 72)
dot 10 = 1440

1440 seems a bit too far from 1800.

Now let's try some different figures and deal with fractions.

Number of pixels = 1800
range = 10400 to 13800
1800/13800 = 0.13043478260869565
dot d = ((d-10400) * 0.13043478260869565)
Assuming dot d = 10500, which is close to the bottom (pixel 0)
dot 10500 = ((10500-10400) * 0.13043478260869565)
dot 10 = (100 * 0.13043478260869565)
dot 10 = 13.043478260869565
OK, looks good.

Number of pixels = 1800
range = 10400 to 13800
1800/13800 = 0.13043478260869565
dot d = ((d-10400) * 0.13043478260869565)
Assuming dot d = 13700, which is close to the top (pixel 1800)
dot 13700 = ((13700-10400) * 0.13043478260869565)
dot 13700 = (3300 * 0.13043478260869565)
dot 13700 = 430.4347826086956
430 is VERY far from 1800!

Number of pixels = 1800
range = 25 to 50
1800/25 = 72
dot d = ((d-25) * 72)
Assuming dot d = 10, which is close to the bottom (pixel 0)
dot 10 = ((10-25) * 72)
dot 10 = (-15 * 72)
dot 10 = -1080.0

So dot 10 would be plotted at -1080 in the y axis? That is wrong. It shouldn't be negative.

Well, if the range of values, that is the y-axis of your graph is 25 to 50, then by definition you will have no dots with a value of 10 in the graph! The negative value tells you it is out of y-axis range.

If you have a lowest dot with a value of 10 then the lower end of the range must be 10 or less!

You must understand the formula to use it correctly.

If the range of actual values were 10 to 50 and you want to leave some pad at top and bottom then set range-min lower than 10 and the range-max greater than 50, 0 to 60 for example:

Well, if the range of values, that is the y-axis of your graph is 25 to 50, then by definition you will have no dots with a value of 10 in the graph! The negative value tells you it is out of y-axis range.

If you have a lowest dot with a value of 10 then the lower end of the range must be 10 or less!

Yes, my mistake. I'm sorry. Thank you for pointing that out.

Distribution: openSUSE, Raspbian, Slackware. Previous: MacOS, Red Hat, Coherent, Consensys SVR4.2, Tru64, Solaris

Posts: 2,820

Rep:

Quote:

Originally Posted by lucmove

It's perl, but I don't want any code. I want the math. Writing the code is how I have my fun.

You need to determine the limits in the size plot you wish to create. It seems you already have this. Establish the scaling that needs to done with each data point in both the X and Y directions: Xscale = (Xpxlmax - Xpxlmin) / (Xmax - Xmin). (Same for Y.) Apply the scaling to each point to, basically, turn the data coordinates to coordinates in the "pixel space". If either the X or Y data crosses 0.0, you'll need to apply an offset as well to get the data positioned around the X/Y axes correctly and within you plotting space. Tinkering with a small dataset should make it clear how this works.

I used to do stuff like to to get data plotted on pen plotters; things got hairy when there were multiple/stacked plots. I stopped caring about things like this when tools like Gnuplot did all of it for me. It's interesting to know how the guts of the tool works but if I'm more interested in seeing the plot and I'm no longer banging my head on the table trying to get a homegrown surface plot w/ hidden-line removal code to work, I'm more than happy.

I stopped caring about things like this when tools like Gnuplot did all of it for me. It's interesting to know how the guts of the tool works but if I'm more interested in seeing the plot and I'm no longer banging my head on the table trying to get a homegrown surface plot w/ hidden-line removal code to work, I'm more than happy.

Gnuplot is nightmarishly difficult. Coding my own plotter from scratch is easier.

Not a lot of good plotters for Windows either. Most offerings today are exclusively online.

LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.