开发者

how to plot a histogram in c

how do I plot a histogram in c from 2 a开发者_运维技巧rrays?


For a histogram layed out on its side...

I suggest using printf("*") for each increment, and printf("\n") to start outputting a new row. (Changing the orientation is an excercise to the reader).


You could you this character(■) to represent the count in the graph. This is a character that can be printed by

printf("%c", (char)254u);

Consider some random float_arr and hist array that hold the count.

Code

// Function generating random data
for (i = 0; i < n; i++){
    float random = ((float)rand() / (float)(RAND_MAX));
    float_arr[i] = random;
    printf("%f  ", random);
}
//Dividing float data into bins
for (i = 0; i < n; i++){
    for (j = 1; j <= bins; j++){

        float bin_max = (float)j / (float)bins;
        if (float_arr[i] <= bin_max){
            hist[j]++;
            break;
        }
    }
}
// Plotting histogram
printf("\n\nHistogram of Float data\n");
for (i = 1; i <= bins; i++)
{
    count = hist[i];
    printf("0.%d |", i - 1);
    for (j = 0; j < count; j++)
    {
        printf("%c", (char)254u);
    }
    printf("\n");
}

Output

Histogram of Float data
0.0 |■■■■■■■■■■■■■■■■■■■■■■
0.1 |■■■■■■■■■■■■■■■■
0.2 |■■■■■
0.3 |■■■■■■■■■■■■■■
0.4 |■■■■■■■■
0.5 |■■■■■■■■■■■■■■■■
0.6 |■■■■■■■■■■
0.7 |■■■■■■■
0.8 |■■■■■■■■■■■■■■■
0.9 |■■■■■■■


You can use ascii art for that


Thinking about the problem a bit I'm not convinced that the "duplicate" I identified in the comments is really responsive. So I'll say a few words.

If you've settled on a ASCII art approach, then you have only one more decision to make: vertical or horizontal bars. Horizontal is easy: just decide on the scaling and then print bin_contents*scale symbols for each bin. The code-golf link really is useful as a model of what to do, even if not a good example of how to write it.

However, many fields have an expectation of vertical bar in the presentation of histograms. That's a little harder, but consider the pseudocode

sacle = find_scale(input_array)
max_height = find_max(input_array) * scale
for (i=max_height; i>=0; i--) 
   if (some condition)
      print_in_N_digits(round(i/scale)) // to label the scale
   else
      print_in_N_digits()               // lines with no labels
   print " |"                           // set up the vertical axis
   for (j=first_bin to lat_bin)
      if (input[j]*scale >= i)
         print("#")
      else
         print(" ")
      print_new_line
print_in_N_digits(0)
print(" +")
for (j=first_bin to last_bin)
   print("-")
print_new_line
print_in_N_digits()
print(" 0")
for (j=first_bin to last_bin)
   if (some other condition)
      print_bin_label

This just walks across the page, using on column per bin and at each level prints either " " or "#" for each column. The histogram printing part is really very easy. All the complexity arises from managing the axis and labels.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜