Plotting Graphs in c
i am beginner in c programming, i am currently开发者_Python百科 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?? I appreciate your help..n thanx!!!
Medsphere has some pretty great GTK# widgets for graphing (among other things), but you'll need to be a little more clear about your input/output requirements to get more specific help.
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 |■■■■■■■
Gnuplot (http://www.gnuplot.info/) is a pretty capable and free tool for graphing data. Your question is not clear as to whether you want to plot data programmatically though.
I think MathGL can help you. It is GPL plotting library which can create a window with yours plot without knowledge of any widgets library. Also it have nice graphics for matrices and 3-ranged data.
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:
- open a new window
- draw something. That is coordinates, curves, background, etc.
In c/c++ there are some of these libraries are:
- libplot, the gnu plot utils www.gnu.org/s/plotutils. Is a very basic one and the documentation have nice examples, to plot in svg, png, and other files. Also you can plot in a window or make animations.
- mathGL is open-gl based.
Another way to make a plot is to generate your data in your c code and then plot it with an other program. It is possible to run gnuplot and pass your data trough a pipe. But you must use fork and pipes.
精彩评论