简体   繁体   English

在 c 中绘制图形

[英]Plotting Graphs in c

i am beginner in c programming, i am currently using gedit of ubuntu 10.04 to write c prog, i want to plot a graph, but i am able to do it, can any one tell me hw it can be done or else is there any way to extract the data from the output to spreadsheet where i can plot the req, graph??我是 c 编程的初学者,我目前正在使用 ubuntu 10.04 的 gedit 来编写 c prog,我想绘制一个图形,但我能够做到,谁能告诉我它可以做到,否则有什么将数据从输出中提取到电子表格的方法,我可以在其中绘制请求、图表? I appreciate your help..n thanx!!!我感谢你的帮助..nthanx!!!

Medsphere 有一些非常棒的GTK# 小部件用于绘图(除其他外),但是您需要更清楚地了解您的输入/输出要求才能获得更具体的帮助。

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.考虑一些保存计数的随机float_arrhist数组。

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 |■■■■■■■

Your problem is the same that I have.你的问题和我一样。 To do this there are some libraries written to facilitate the work that is more or less this:为此,编写了一些库来促进或多或少这样的工作:

  1. open a new window打开一个新窗口
  2. draw something.画一些东西。 That is coordinates, curves, background, etc.即坐标、曲线、背景等。

In c/c++ there are some of these libraries are:在 c/c++ 中有一些这样的库是:

  1. libplot, the gnu plot utils www.gnu.org/s/plotutils. libplot,gnu 绘图工具 www.gnu.org/s/plotutils。 Is a very basic one and the documentation have nice examples, to plot in svg, png, and other files.是一个非常基本的文档,文档中有很好的示例,可以在 svg、png 和其他文件中绘制。 Also you can plot in a window or make animations.您也可以在窗口中绘图或制作动画。
  2. mathGL is open-gl based. mathGL 是基于 open-gl 的。

Another way to make a plot is to generate your data in your c code and then plot it with an other program.绘制绘图的另一种方法是在您的 c 代码中生成您的数据,然后使用其他程序绘制它。 It is possible to run gnuplot and pass your data trough a pipe.可以运行 gnuplot 并通过管道传递数据。 But you must use fork and pipes.但是你必须使用叉子和管道。

Gnuplot (http://www.gnuplot.info/) is a pretty capable and free tool for graphing data. Gnuplot (http://www.gnuplot.info/) 是一款功能强大且免费的数据绘图工具。 Your question is not clear as to whether you want to plot data programmatically though.您的问题不清楚是否要以编程方式绘制数据。

I think MathGL can help you.我认为MathGL可以帮助你。 It is GPL plotting library which can create a window with yours plot without knowledge of any widgets library.它是 GPL 绘图库,它可以在不了解任何小部件库的情况下用您的绘图创建一个窗口。 Also it have nice graphics for matrices and 3-ranged data.它还具有用于矩阵和 3 范围数据的精美图形。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM