0

I have a text file which contains numbers like,

0.00
0.12
0.35
0.78
0.93
1.12
1.45
1.54
1.67
1.89
1.99
2.01
2.59
2.82

Now i have to construct a graph, in the following way, for the above values i should get 3 points, (1,5) , (2,6) and (3,3) as there are 5 numbers between 0.00 and 1.00 and 6 numbers between 1.01 and 2.00 similarly 3 numbers between 2.01 and 3.00, which is the best way (language, tool etc) to proceed??

Joel Coehoorn
  • 399,467
  • 113
  • 570
  • 794
Hari Krishna
  • 551
  • 6
  • 21

2 Answers2

2

The desired data could be generated with a simple awk script and subsequently plotted with any plotting utility, such as gnuplot.

Using awk to generate the data:

awk '{bins[int($1)] += 1} END {for(i in bins){print i, bins[i]}}' yourdata.dat > plotdata.dat

The data could then be easily plotted with the gnuplot command:

plot 'plotdata.dat'
telenachos
  • 884
  • 6
  • 18
1

This little example written in C# could be a good starting point:

List<double> list = new List<double>() {
     0.00,0.12,0.35,0.78,0.93,1.12,1.45,1.54,
     1.67,1.89,1.99,2.01,2.59,2.82};
int maxval = (int)list.Max();
Dictionary<int, int> dict = new Dictionary<int, int>();
for (int i = 0; i <= maxval; i++)
     dict.Add(i + 1, list.Count(item => (int)item >= i && (int)item < (i + 1)));

How does it work?
list is simply a list of the numbers you provided in your answer.
First I compute maximum integer I have in that list (maxval) to know how long must be my final array.
Then I create a new Dictionary (key is horizontal value, step 1, and value is the number of numbers between the required range).

EDITED after user comment:
To read list from file you could try:

List<double> list = new List<double>();
string[] nums = File.ReadAllLines(your_file);
foreach (string num in nums)
    list.Add(Double.Parse(num));

You should check if values are really doubles (maybe using Double.TryParse(...)).
Finally: my function works even if numbers are not sorted in list.

Marco
  • 56,740
  • 14
  • 129
  • 152
  • actually there are thousands of such values read from a text file, i just gave a sample :) its not with max value, i want the number of values present between every integer :) – Hari Krishna Nov 17 '11 at 11:26
  • @HariKrishna: first: `list` can be easily red from a file; second: `maxval` is used in `for` cycle to know where to stop, but `dict` is built on every integer between 0 and `maxval` (see `i++`) – Marco Nov 17 '11 at 11:28
  • @HariKrishna: take a look at my edited answer – Marco Nov 17 '11 at 11:32
  • @HariKrishna: did you solve your problem? Is my function correct? – Marco Nov 17 '11 at 12:31
  • ya thanks :) its correct :) i did the same, forgot to check back:) now have to plot those values to a chart :) thank you.. – Hari Krishna Nov 17 '11 at 13:07