NS3 Gnuplot说明

作者: shawn168 | 来源:发表于2017-06-25 20:29 被阅读393次

    使用Gnuplot类做图(NS3官方文档内容翻译)

    有两种使用ns3和gnuplot(http://www.gnuplot.inifo)做图的方法:

    1. 使用ns3的gnuplot类生成一个gnuplot控制文件
    2. 使用ns3生成gnuplot的数据文件

    这一节我们使用方法一。
    如果你对方法二有兴趣,请看ns3的Tutorial的“Tracing”部分的“A Real Example”子部分。

    下面是使用ns3的Gnuplot类做图的步骤:

    1. 使用Gnuplot类和它的函数更改你的代码
    2. 运行代码,以生成gnuplot的控制文件
    3. 使用控制文件的名字调用gnuplot
    4. 使用你自己感兴趣的视图程序查看视图文件

    下面看代码,更加了解步骤一的详细信息。
    例子目录位置:
    src/stats/examples/gnuplot-example.cc

    使用下面的命令运行例子程序:

    $ ./waf shell
    $ cd build/debug/src/stats/examples
    $ ./gnuplot-example
    

    运行上面的命令过后,会在例子程序所在目录下产生gnuplot的控制文件:

    plot-2d.plt
    plot-2d-with-error-bars.plt
    plot-3d.plt

    为了查看控制文件的内容,运行下面的命令:

    $ gnuplot plot-2d.plt
    $ gnuplot plot-2d-with-error-bars.plt
    $ gnuplot plot-3d.plt
    

    运行命令过后,会在当前目录下生成图片文件:

    plot-2d.png
    plot-2d-with-error-bars.png
    plot-3d.png

    下面就可以使用你自己感兴趣的程序,查看视图文件。如果安装了gimp在电脑上,可以使用下面的命令查看:

    $ gimp plot-2d.png
    $ gimp plot-2d-with-error-bars.png
    $ gimp plot-3d.png
    

    2D视图的例子
    我们使用下面的代码生成一个gnuplot-example.cc文件(该文件路径src/stats/examples/gnuplot-example.cc):

    using namespace std;
    string fileNameWithNoExtension  ="plot-2d";
    string graphicsFileName =fileNameWithNoExtension + ".png";
    string plotFileName     =fileNameWithNoExtension + ".plt";
    string plotTitle        ="2-D Plot";
    string dataTitle        ="2-D Data";
    // Instantiate the plot and set its title.
    Gnuplot plot (graphicsFileName);
    plot.SetTitle (plotTitle);
    // Make the graphics file, which the plot file will create when it
    // is used with Gnuplot, be a PNG file.
    plot.SetTerminal ("png");
    // Set the labels for each axis.
    plot.SetLegend ("X Values", "Y Values");
    // Set the range for the x axis.
    plot.AppendExtra ("set xrange [-6:+6]");
    // Instantiate the dataset, set its title, and make the points be
    // plotted along with connecting lines.
    Gnuplot2dDataset dataset;
    dataset.SetTitle (dataTitle);
    dataset.SetStyle (Gnuplot2dDataset::LINES_POINTS);
    double x;
    double y;
    // Create the 2-D dataset.
    for (x = -5.0; x <= +5.0; x += 1.0)
    {
    // Calculate the 2-D curve
    y = x * x;
    // Add this point.
    dataset.Add (x, y);
    }
    // Add the dataset to the plot.
    plot.AddDataset (dataset);
    // Open the plot file.
    ofstream plotFile (plotFileName.c_str());
    // Write the plot file.
    plot.GenerateOutput (plotFile);
    // Close the plot file.
    plotFile.close ();
    
    这里写图片描述

    带有误差的2D视图

    使用下面的代码可以产生在x,y方向具备误差的2D视图。
    代码如下,代码生成一个gnuplot-example.cc文件(该文件路径src/stats/examples/gnuplot-example.cc):

    using namespace std;
    string fileNameWithNoExtension  ="plot-2d-with-error-bars";
    string graphicsFileName =fileNameWithNoExtension + ".png";
    string plotFileName     =fileNameWithNoExtension + ".plt";
    string plotTitle        ="2-D Plot With Error Bars";
    string dataTitle        ="2-D Data With Error Bars";
    // Instantiate the plot and set its title.
    Gnuplot plot (graphicsFileName);
    plot.SetTitle (plotTitle);
    // Make the graphics file, which the plot file will create when it
    // is used with Gnuplot, be a PNG file.
    plot.SetTerminal ("png");
    // Set the labels for each axis.
    plot.SetLegend ("X Values", "Y Values");
    // Set the range for the x axis.
    plot.AppendExtra ("set xrange [-6:+6]");
    // Instantiate the dataset, set its title, and make the points be
    // plotted with no connecting lines.
    Gnuplot2dDataset dataset;
    dataset.SetTitle (dataTitle);
    dataset.SetStyle (Gnuplot2dDataset::POINTS);
    // Make the dataset have error bars in both the x and y directions.
    dataset.SetErrorBars (Gnuplot2dDataset::XY);
    double x;
    double xErrorDelta;
    double y;
    double yErrorDelta;
    // Create the 2-D dataset.
    for (x = -5.0; x <= +5.0; x += 1.0)
    {
        // Calculate the 2-D curve
        y = x * x;
        // Make the uncertainty in the x direction be constant and make
        // the uncertainty in the y direction be a constant fraction of
        // y's value.
        xErrorDelta = 0.25;
        yErrorDelta = 0.1 * y;
        // Add this point with uncertainties in both the x and y
        // direction.
        dataset.Add (x, y, xErrorDelta, yErrorDelta);
    }
    // Add the dataset to the plot.
    plot.AddDataset (dataset);
    // Open the plot file.
    ofstream plotFile (plotFileName.c_str());
    // Write the plot file.
    plot.GenerateOutput (plotFile);
    // Close the plot file.
    plotFile.close ();
    
    这里写图片描述

    3D视图的例子程序

    代码如下,代码生成一个gnuplot-example.cc文件(该文件路径src/stats/examples/gnuplot-example.cc):

    using namespace std;
    
    string fileNameWithNoExtension  ="plot-3d";
    string graphicsFileName =fileNameWithNoExtension + ".png";
    string plotFileName     =fileNameWithNoExtension + ".plt";
    string plotTitle        ="3-D Plot";
    string dataTitle        ="3-D Data";
    
    // Instantiate the plot and set its title.
    Gnuplot plot (graphicsFileName);
    plot.SetTitle (plotTitle);
    // Make the graphics file, which the plot file will create when it
    // is used with Gnuplot, be a PNG file.
    plot.SetTerminal ("png");
    // Rotate the plot 30 degrees around the x axis and then rotate the
    // plot 120 degrees around the new z axis.
    plot.AppendExtra ("set view 30, 120, 1.0, 1.0");
    
    // Make the zero for the z-axis be in the x-axis and y-axis plane.
    plot.AppendExtra ("set ticslevel 0");
    // Set the labels for each axis.
    plot.AppendExtra ("set xlabel 'X Values'");
    plot.AppendExtra ("set ylabel 'Y Values'");
    plot.AppendExtra ("set zlabel 'Z Values'");
    // Set the ranges for the x and y axis.
    plot.AppendExtra ("set xrange [-5:+5]");
    plot.AppendExtra ("set yrange [-5:+5]");
    // Instantiate the dataset, set its title, and make the points be
    // connected by lines.
    Gnuplot3dDataset dataset;
    dataset.SetTitle (dataTitle);
    dataset.SetStyle ("with lines");
    double x;
    double y;
    double z;
    // Create the 3-D dataset.
    for (x = -5.0; x <= +5.0; x += 1.0)
    {
        for (y = -5.0; y <= +5.0; y += 1.0)
        {
            // Calculate the 3-D surface
            z = x * x * y * y;
            // Add this point.
            dataset.Add (x, y, z);
        }
        // The blank line is necessary at the end of each x value's data
        // points for the 3-D surface grid to work.
        dataset.AddEmptyLine ();
    }
    // Add the dataset to the plot.
    plot.AddDataset (dataset);
    // Open the plot file.
    ofstream plotFile (plotFileName.c_str());
    // Write the plot file.
    plot.GenerateOutput (plotFile);
    // Close the plot file.
    plotFile.close ();
    
    这里写图片描述

    运行以下命令,产生如下信息:

    root@longyin:/home/workspace/ns3# gnuplot plot-2d.plt
    

    程序 'gnuplot' 已包含在下列软件包中:

    • gnuplot-nox
    • gnuplot-qt
    • gnuplot-x11
    • gnuplot5-nox
    • gnuplot5-qt
    • gnuplot5-x11

    请尝试:apt-get install <选定的软件包>
    说明没有安装gnuplot。
    使用下面的命令安装(我的系统ubuntu 15.04):

    sudo apt-get install gnuplot-x11
    

    该命令安装的gunplot不是最新版本,我安装的版本是4.6,官方最新版本是5.0.2。
    不过我使用官方的tar压缩文件安装有问题,还是使用该命令靠谱!!!

    上面的命令安装完成之后,终端输入命令:gnuplot
    会直接进入gnuplot命令行,如下所示:

    root@xxx:/home# gnuplot
    
        G N U P L O T
        Version 4.6 patchlevel 6    last modified September 2014
        Build System: Linux i686
    
        Copyright (C) 1986-1993, 1998, 2004, 2007-2014
        Thomas Williams, Colin Kelley and many others
    
        gnuplot home:     http://www.gnuplot.info
        faq, bugs, etc:   type "help FAQ"
        immediate help:   type "help"  (plot window: hit 'h')
    
    Terminal type set to 'x11'
    gnuplot> 
    

    如果输入命令行gnuplot之后,终端还是显示没有安装的信息,需要在/etc/profile文件中把/usr/bin添加到PATH路径中。安装的gnuplot会在/usr/bin目录中。设置完/etc/profile之后,需要在终端输入命令:

    $ source /etc/profile
    

    该命令让更改生效。
    更改完成之后,在任意文件夹下输入命令gunplot就可以进入gnuplot命令行。

    相关文章

      网友评论

        本文标题:NS3 Gnuplot说明

        本文链接:https://www.haomeiwen.com/subject/kesgnttx.html