Go back to Richel Bilderbeek's homepage.

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

 

 

 

 

 

(C++) gnuplot example 5: creating and displaying a contour plot

 

gnuplot example 5 is a gnuplot example.

 

 

 

 

 

 

Technical facts

 

Application type(s)

Operating system(s) or programming environment(s)

IDE(s):

Project type:

C++ standard:

Compiler(s):

Libraries used:

 

 

 

 

 

Qt project file: CppGnuplotExample5.pro

 

TEMPLATE = app
CONFIG += console
CONFIG += qt

SOURCES += main.cpp


 

 

 

 

 

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());
    const int maxx = 100;
    const int maxy = 100;
    for (int y = 0; y!=maxx; ++y)
    {
      for (int x = 0; x!=maxy; ++x)
      {
        const double f_x = static_cast<double>(x) / static_cast<double>(maxx);
        const double f_y = static_cast<double>(y) / static_cast<double>(maxy);
        const double x_co = f_x * 2.0 * M_PI;
        const double y_co = f_y * 3.0 * M_PI;
        const double z = (std::cos(x_co) * std::sin(y_co));
        f << x << " " << y << " " << z << '\n';
      }
    }
  }

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

  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 100,100,1\n"
      "set pm3d map\n"
      "set title \"Example 5\"\n"
      "set xlabel \"X coordinat\"\n"
      "set ylabel \"Y coordinat\"\n"
      "splot \"" << dat_filename<< "\"\n";
  }

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

  std::system(cmd.c_str());

  QLabel label;
  label.setPixmap(QPixmap(pic_filename.c_str()));
  label.show();

  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