Go back to Richel Bilderbeek's homepage.

Go back to Richel Bilderbeek's C++ page.






(C++) gnuplot example 4: creating and displaying a surface plot


gnuplot example 4 is a gnuplot example.







Technical facts


Application type(s)

Operating system(s) or programming environment(s)


Project type:

C++ standard:


Libraries used:






Qt project file: CppGnuplotExample4.pro


CONFIG += console
CONFIG += qt

SOURCES += main.cpp








#include <cmath>
    #include <fstream>
#include <string>
#include <cstdlib>
#include <QApplication>
#include <QLabel>

int main(int argc, char *argv[])
  QApplication a(argc,argv);

  const std::string dat_filename = "test.data";

  //Create data file
    std::ofstream f(dat_filename.c_str());
    for (double y = -10.0; y < 10.0; y+=0.2)
      for (double x = -10.0; x < 10.0; x+=0.2)
        const double z = std::cos(x) * std::sin(y);
        f << x << " " << y << " " << z << '\n';

  #ifdef WIN32
  const std::string exe = "C:\\Progra~1\\gnuplot\\bin\\gnuplot.exe";
  const std::string exe = "gnuplot";

  const std::string cmd_filename = "test.txt";
  const std::string pic_filename = "test.png";

    std::ofstream f(cmd_filename.c_str());

    f <<
      "set terminal pngcairo\n"
      "set output '" << pic_filename <<"'\n"
      "set dgrid3d 50,50,2\n"
      "set dummy u,v\n"
      "set pm3d implicit at s\n"
      "set hidden3d\n"
      "set key inside right top vertical Right noreverse enhanced autotitles box linetype -1 linewidth 1.000\n"
      "set style data lines\n"
      "set title \"Example 4\"\n"
      "set xlabel \"X coordinat\"\n"
      "set ylabel \"Y coordinat\"\n"
      "set zlabel \"Z value\"\n"
      "splot \"" << dat_filename<< "\"\n";

  const std::string cmd = exe + " " + cmd_filename;


  QLabel label;

  return a.exec();






Go back to Richel Bilderbeek's C++ page.

Go back to Richel Bilderbeek's homepage.


Valid XHTML 1.0 Strict

This page has been created by the tool CodeToHtml