如何在c中绘制直方图

发布于 2024-09-25 12:18:00 字数 27 浏览 4 评论 0原文

如何在 c 中从 2 个数组绘制直方图?

how do I plot a histogram in c from 2 arrays?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

感情废物 2024-10-02 12:18:00

您可以用这个字符(■)来表示图表中的计数。这是一个可以通过

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

考虑一些保存计数的随机 float_arrhist 数组来打印的字符。

代码

// 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");
}

输出

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 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 |■■■■■■■
瑕疵 2024-10-02 12:18:00

对于侧面放置的直方图...

我建议对每个增量使用 printf("*") ,并使用 printf("\n") 开始输出新行。 (改变方向对读者来说是一种练习)。

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).

小红帽 2024-10-02 12:18:00

你可以使用 ascii art 来实现

You can use ascii art for that

死开点丶别碍眼 2024-10-02 12:18:00

稍微思考一下这个问题,我不相信我在评论中确定的“重复”确实有反应。那么我就说几句话吧。

如果您选择了 ASCII 艺术方法,那么您只需做出一个决定:垂直条还是水平条。水平很简单:只需决定缩放比例,然后为每个 bin 打印 bin_contents*scale 符号。代码高尔夫链接作为做什么的模型确实很有用,即使不是如何编写它的一个很好的例子。

然而,许多领域都期望在直方图的呈现中使用垂直条。这有点困难,但考虑伪代码

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

这只是遍历页面,在每个容器的列上使用,并在每个级别为每个列打印 " ""#" 。直方图打印部分确实非常简单。所有的复杂性都源于管理轴和标签。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文