【问题标题】:how to plot graph using GNUplot in C++如何在 C++ 中使用 GNUplot 绘制图形
【发布时间】:2016-12-01 18:34:55
【问题描述】:

当我执行我的程序时,我希望我的程序绘制一个图形并显示在屏幕上。但我不确定我该怎么做。 这是示例 C++ 代码。

#include "stdafx.h"
# include <cstdlib>
# include <iostream>
# include <iomanip>
# include <cmath>

using namespace std;

# include "curve_plot.h"

int main();  
int main()


{
    int i;
    int n;
    double *x;
    double *y;

    cout << "\n";
    cout << "CURVE_PLOT_PRB:\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;
}

这是头文件。

void curve_plot(int n, double x[], double y[], string name);

curve_plot.cpp

#include "stdafx.h"
# include <cstdlib>
# include <iostream>
# include <iomanip>
# include <fstream>
#include <string>
using namespace std;

# include "curve_plot.h"

void curve_plot(int n, double x[], double y[], string name)


{
    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;
}

【问题讨论】:

    标签: c++ gnuplot


    【解决方案1】:

    您想要什么,从您的 C++ 程序运行 gnuplot 还是在您的 C++ 程序执行后运行它?如果是第一个,请添加

    system("gnuplot gnuplot_command_file");
    

    就在return 声明之前。当然,首先你必须建立一个字符串作为system语句的参数。

    否则,只需在命令提示符下执行

    gnuplot your_command_file
    

    【讨论】:

    • 从我的 C++ 程序运行 gnuplot。我只是建立一个字符串并添加 system("gnuplot command_file");我想我还必须添加 Gnu 绘图头文件?
    • gnuplot 只是一个可以从命令行或您的程序执行的程序,所以,是的。
    • 所以我按照你提到的那样做了,我运行了代码,但尽管程序执行得很好,但仍然没有显示图表。附言: 我正在使用 Visual Studio 2015 来执行我的程序
    • 图形不会显示,因为在文件curve_plot_commands.txt 中终端设置为png,所以不是显示窗口,而是创建文件curve_plot.png。注释包含set term png 的行,而不是quitpause mouse。对我来说,它有效。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-09-24
    • 1970-01-01
    • 2011-05-25
    • 1970-01-01
    • 2013-03-22
    • 1970-01-01
    相关资源
    最近更新 更多