# include # include # include # include # include using namespace std; int main ( ); void curve_plot ( int n, double x[], double y[], string name ); //****************************************************************************80 int main ( ) //****************************************************************************80 // // Purpose: // // curve_plot_test() tests curve_plot(). // // Licensing: // // This code is distributed under the MIT license. // // Modified: // // 04 June 2014 // // Author: // // John Burkardt // { int i; int n; double *x; double *y; cout << "\n"; cout << "curve_plot_test()::\n"; cout << " Demonstrate how CURVE_PLOT() can be used.\n"; // // Set up some data to plot. // n = 51; x = new double[n]; y = new double[n]; for ( i = 0; i < 51; i++ ) { x[i] = ( double ) ( i ) / 10.0; y[i] = x[i] * cos ( x[i] ); } // // Send the data to curve_plot. // curve_plot ( n, x, y, "curve_plot" ); // // Free memory. // delete [] x; delete [] y; return 0; } //****************************************************************************80 void curve_plot ( int n, double x[], double y[], string name ) //****************************************************************************80 // // Purpose: // // curve_plot() creates files that allow gnuplot() to plot a curve. // // Discussion: // // Given N sets of X and Y data, and a name "NAME", this function // creates two files: // NAME_data.txt, containing the plot data; // NAME_commands.txt, containing commands to gnuplot. // Once these files are created, the command // gnuplot < NAME_commands.txt // should start up gnuplot, and cause it to create the file // NAME.png // containing a plot of the data. // // Licensing: // // This code is distributed under the MIT license. // // Modified: // // 04 June 2014 // // Author: // // John Burkardt // // Input: // // int N, the number of data points. // // double X[N], the X values. // // double Y[N], the Y values. // // string NAME, a name to use for the files to be created. // { string command_filename; ofstream command_unit; string data_filename; ofstream data_unit; int i; string plot_filename; // // Write the data file. // data_filename = name + "_data.txt"; data_unit.open ( data_filename.c_str ( ) ); for ( i = 0; i < n; i++ ) { data_unit << x[i] << " " << y[i] << "\n"; } data_unit.close ( ); cout << "\n"; cout << " Plot data written to the file \"" << data_filename << "\".\n"; // // Write the command file. // command_filename = name + "_commands.txt"; command_unit.open ( command_filename.c_str ( ) ); command_unit << "set term png\n"; plot_filename = name + ".png"; command_unit << "set output \"" << plot_filename << "\"\n"; command_unit << "set grid\n"; command_unit << "set style data lines\n"; command_unit << "unset key\n"; command_unit << "set xlabel '<---X--->'\n"; command_unit << "set ylabel '<---Y--->'\n"; command_unit << "set timestamp\n"; command_unit << "plot \"" << data_filename << "\" using 1:2 with lines lw 3\n"; command_unit << "quit\n"; command_unit.close ( ); cout << " Command data written to \"" << command_filename << "\".\n"; return; }