File: gnuplot.rst

package info (click to toggle)
ns3 3.46-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 105,864 kB
  • sloc: cpp: 624,863; python: 14,863; ansic: 6,772; makefile: 1,950; sh: 987; javascript: 167; perl: 102
file content (299 lines) | stat: -rw-r--r-- 8,411 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
.. include:: replace.txt
.. highlight:: cpp

Making Plots using the Gnuplot Class
------------------------------------

There are 2 common methods to make a plot using |ns3| and gnuplot (http://www.gnuplot.info):

#. Create a gnuplot control file using |ns3|'s Gnuplot class.
#. Create a gnuplot data file using values generated by |ns3|.

This section is about method 1, i.e. it is about how to make a plot using |ns3|'s Gnuplot class.  If you are interested in method 2, see the "A Real Example" subsection under the "Tracing" section in the |ns3| `Tutorial <https://www.nsnam.org/docs/tutorial/html/index.html>`_.

Creating Plots Using the Gnuplot Class
**************************************

The following steps must be taken in order to create a plot using |ns3|'s Gnuplot class:

#. Modify your code so that is uses the Gnuplot class and its functions.
#. Run your code so that it creates a gnuplot control file.
#. Call gnuplot with the name of the gnuplot control file.
#. View the graphics file that was produced in your favorite graphics viewer.

See the code from the example plots that are discussed below for details on step 1.

An Example Program that Uses the Gnuplot Class
**********************************************

An example program that uses |ns3|'s Gnuplot class can be found here:

.. sourcecode:: bash

  src/stats/examples/gnuplot-example.cc

In order to run this example, do the following:

.. sourcecode:: bash

  $ ./ns3 run src/stats/examples/gnuplot-example

This should produce the following gnuplot control files:

.. sourcecode:: text

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

In order to process these gnuplot control files, do the following:

.. sourcecode:: bash

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

This should produce the following graphics files:

.. sourcecode:: text

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

You can view these graphics files in your favorite graphics viewer.  If you have gimp installed on your machine, for example, you can do this:

.. sourcecode:: bash

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

An Example 2-Dimensional Plot
*****************************

The following 2-Dimensional plot

.. _plot-2d:

.. figure:: figures/plot-2d.*

was created using the following code from gnuplot-example.cc: ::

  std::string fileNameWithNoExtension = "plot-2d";
  std::string graphicsFileName        = fileNameWithNoExtension + ".png";
  std::string plotFileName            = fileNameWithNoExtension + ".plt";
  std::string plotTitle               = "2-D Plot";
  std::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
      //
      //            2
      //     y  =  x   .
      //
      y = x * x;

      // Add this point.
      dataset.Add(x, y);
    }

  // Add the dataset to the plot.
  plot.AddDataset(dataset);

  // Open the plot file.
  std::ofstream plotFile(plotFileName.c_str());

  // Write the plot file.
  plot.GenerateOutput(plotFile);

  // Close the plot file.
  plotFile.close();

An Example 2-Dimensional Plot with Error Bars
*********************************************

The following 2-Dimensional plot with error bars in the x and y directions

.. _plot-2d-with-error-bars:

.. figure:: figures/plot-2d-with-error-bars.*

was created using the following code from gnuplot-example.cc: ::

  std::string fileNameWithNoExtension = "plot-2d-with-error-bars";
  std::string graphicsFileName        = fileNameWithNoExtension + ".png";
  std::string plotFileName            = fileNameWithNoExtension + ".plt";
  std::string plotTitle               = "2-D Plot With Error Bars";
  std::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
      //
      //            2
      //     y  =  x   .
      //
      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.
  std::ofstream plotFile(plotFileName.c_str());

  // Write the plot file.
  plot.GenerateOutput(plotFile);

  // Close the plot file.
  plotFile.close();

An Example 3-Dimensional Plot
*****************************

The following 3-Dimensional plot

.. _plot-3d:

.. figure:: figures/plot-3d.*

was created using the following code from gnuplot-example.cc: ::

  std::string fileNameWithNoExtension = "plot-3d";
  std::string graphicsFileName        = fileNameWithNoExtension + ".png";
  std::string plotFileName            = fileNameWithNoExtension + ".plt";
  std::string plotTitle               = "3-D Plot";
  std::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
          //
          //            2      2
          //     z  =  x   *  y   .
          //
          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.
  std::ofstream plotFile(plotFileName.c_str());

  // Write the plot file.
  plot.GenerateOutput(plotFile);

  // Close the plot file.
  plotFile.close();