how to plot a histogram in c

心已入冬 提交于 2019-12-23 02:19:05

问题


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


回答1:


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




回答2:


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.




回答3:


You can use ascii art for that



来源:https://stackoverflow.com/questions/3836987/how-to-plot-a-histogram-in-c

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!