A Discrete-Event Network Simulator
Home
Tutorials ▼
English
Portuguese
Docs ▼
Wiki
Manual
Models
Develop ▼
API
Bugs
API
Main Page
Related Pages
Modules
Namespaces
Classes
Files
File List
File Members
All
Classes
Namespaces
Files
Functions
Variables
Typedefs
Enumerations
Enumerator
Properties
Friends
Macros
Groups
Pages
gnuplot-example.cc
Go to the documentation of this file.
1
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2
/*
3
* Copyright (c) 2011 University of Washington
4
*
5
* This program is free software; you can redistribute it and/or modify
6
* it under the terms of the GNU General Public License version 2 as
7
* published by the Free Software Foundation;
8
*
9
* This program is distributed in the hope that it will be useful,
10
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
* GNU General Public License for more details.
13
*
14
* You should have received a copy of the GNU General Public License
15
* along with this program; if not, write to the Free Software
16
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17
*
18
* Author: Mitch Watrous (watrous@u.washington.edu)
19
*/
20
21
#include <fstream>
22
23
#include "ns3/gnuplot.h"
24
25
using namespace
ns3;
26
27
namespace
{
28
29
//===========================================================================
30
// Function: Create2DPlotFile
31
//
32
//
33
// This function creates a 2-D plot file.
34
//===========================================================================
35
36
void
Create2DPlotFile
()
37
{
38
using namespace
std;
39
40
string
fileNameWithNoExtension =
"plot-2d"
;
41
string
graphicsFileName = fileNameWithNoExtension +
".png"
;
42
string
plotFileName = fileNameWithNoExtension +
".plt"
;
43
string
plotTitle =
"2-D Plot"
;
44
string
dataTitle =
"2-D Data"
;
45
46
// Instantiate the plot and set its title.
47
Gnuplot
plot (graphicsFileName);
48
plot.
SetTitle
(plotTitle);
49
50
// Make the graphics file, which the plot file will create when it
51
// is used with Gnuplot, be a PNG file.
52
plot.
SetTerminal
(
"png"
);
53
54
// Set the labels for each axis.
55
plot.
SetLegend
(
"X Values"
,
"Y Values"
);
56
57
// Set the range for the x axis.
58
plot.
AppendExtra
(
"set xrange [-6:+6]"
);
59
60
// Instantiate the dataset, set its title, and make the points be
61
// plotted along with connecting lines.
62
Gnuplot2dDataset
dataset;
63
dataset.
SetTitle
(dataTitle);
64
dataset.SetStyle (
Gnuplot2dDataset::LINES_POINTS
);
65
66
double
x
;
67
double
y;
68
69
// Create the 2-D dataset.
70
for
(
x
= -5.0;
x
<= +5.0;
x
+= 1.0)
71
{
72
// Calculate the 2-D curve
73
//
74
// 2
75
// y = x .
76
//
77
y =
x
*
x
;
78
79
// Add this point.
80
dataset.Add (x, y);
81
}
82
83
// Add the dataset to the plot.
84
plot.
AddDataset
(dataset);
85
86
// Open the plot file.
87
ofstream plotFile (plotFileName.c_str());
88
89
// Write the plot file.
90
plot.
GenerateOutput
(plotFile);
91
92
// Close the plot file.
93
plotFile.close ();
94
}
95
96
97
//===========================================================================
98
// Function: Create2DPlotWithErrorBarsFile
99
//
100
//
101
// This function creates a 2-D plot with error bars file.
102
//===========================================================================
103
104
void
Create2DPlotWithErrorBarsFile
()
105
{
106
using namespace
std;
107
108
string
fileNameWithNoExtension =
"plot-2d-with-error-bars"
;
109
string
graphicsFileName = fileNameWithNoExtension +
".png"
;
110
string
plotFileName = fileNameWithNoExtension +
".plt"
;
111
string
plotTitle =
"2-D Plot With Error Bars"
;
112
string
dataTitle =
"2-D Data With Error Bars"
;
113
114
// Instantiate the plot and set its title.
115
Gnuplot
plot (graphicsFileName);
116
plot.
SetTitle
(plotTitle);
117
118
// Make the graphics file, which the plot file will create when it
119
// is used with Gnuplot, be a PNG file.
120
plot.
SetTerminal
(
"png"
);
121
122
// Set the labels for each axis.
123
plot.
SetLegend
(
"X Values"
,
"Y Values"
);
124
125
// Set the range for the x axis.
126
plot.
AppendExtra
(
"set xrange [-6:+6]"
);
127
128
// Instantiate the dataset, set its title, and make the points be
129
// plotted with no connecting lines.
130
Gnuplot2dDataset
dataset;
131
dataset.
SetTitle
(dataTitle);
132
dataset.SetStyle (
Gnuplot2dDataset::POINTS
);
133
134
// Make the dataset have error bars in both the x and y directions.
135
dataset.SetErrorBars (
Gnuplot2dDataset::XY
);
136
137
double
x
;
138
double
xErrorDelta;
139
double
y;
140
double
yErrorDelta;
141
142
// Create the 2-D dataset.
143
for
(
x
= -5.0;
x
<= +5.0;
x
+= 1.0)
144
{
145
// Calculate the 2-D curve
146
//
147
// 2
148
// y = x .
149
//
150
y =
x
*
x
;
151
152
// Make the uncertainty in the x direction be constant and make
153
// the uncertainty in the y direction be a constant fraction of
154
// y's value.
155
xErrorDelta = 0.25;
156
yErrorDelta = 0.1 * y;
157
158
// Add this point with uncertainties in both the x and y
159
// direction.
160
dataset.Add (x, y, xErrorDelta, yErrorDelta);
161
}
162
163
// Add the dataset to the plot.
164
plot.
AddDataset
(dataset);
165
166
// Open the plot file.
167
ofstream plotFile (plotFileName.c_str());
168
169
// Write the plot file.
170
plot.
GenerateOutput
(plotFile);
171
172
// Close the plot file.
173
plotFile.close ();
174
}
175
176
177
//===========================================================================
178
// Function: Create3DPlotFile
179
//
180
//
181
// This function creates a 3-D plot file.
182
//===========================================================================
183
184
void
Create3DPlotFile
()
185
{
186
using namespace
std;
187
188
string
fileNameWithNoExtension =
"plot-3d"
;
189
string
graphicsFileName = fileNameWithNoExtension +
".png"
;
190
string
plotFileName = fileNameWithNoExtension +
".plt"
;
191
string
plotTitle =
"3-D Plot"
;
192
string
dataTitle =
"3-D Data"
;
193
194
// Instantiate the plot and set its title.
195
Gnuplot
plot (graphicsFileName);
196
plot.
SetTitle
(plotTitle);
197
198
// Make the graphics file, which the plot file will create when it
199
// is used with Gnuplot, be a PNG file.
200
plot.
SetTerminal
(
"png"
);
201
202
// Rotate the plot 30 degrees around the x axis and then rotate the
203
// plot 120 degrees around the new z axis.
204
plot.
AppendExtra
(
"set view 30, 120, 1.0, 1.0"
);
205
206
// Make the zero for the z-axis be in the x-axis and y-axis plane.
207
plot.
AppendExtra
(
"set ticslevel 0"
);
208
209
// Set the labels for each axis.
210
plot.
AppendExtra
(
"set xlabel 'X Values'"
);
211
plot.
AppendExtra
(
"set ylabel 'Y Values'"
);
212
plot.
AppendExtra
(
"set zlabel 'Z Values'"
);
213
214
// Set the ranges for the x and y axis.
215
plot.
AppendExtra
(
"set xrange [-5:+5]"
);
216
plot.
AppendExtra
(
"set yrange [-5:+5]"
);
217
218
// Instantiate the dataset, set its title, and make the points be
219
// connected by lines.
220
Gnuplot3dDataset
dataset;
221
dataset.
SetTitle
(dataTitle);
222
dataset.SetStyle (
"with lines"
);
223
224
double
x
;
225
double
y;
226
double
z;
227
228
// Create the 3-D dataset.
229
for
(
x
= -5.0;
x
<= +5.0;
x
+= 1.0)
230
{
231
for
(y = -5.0; y <= +5.0; y += 1.0)
232
{
233
// Calculate the 3-D surface
234
//
235
// 2 2
236
// z = x * y .
237
//
238
z =
x
*
x
* y * y;
239
240
// Add this point.
241
dataset.Add (
x
, y, z);
242
}
243
244
// The blank line is necessary at the end of each x value's data
245
// points for the 3-D surface grid to work.
246
dataset.AddEmptyLine ();
247
}
248
249
// Add the dataset to the plot.
250
plot.
AddDataset
(dataset);
251
252
// Open the plot file.
253
ofstream plotFile (plotFileName.c_str());
254
255
// Write the plot file.
256
plot.
GenerateOutput
(plotFile);
257
258
// Close the plot file.
259
plotFile.close ();
260
}
261
262
}
// anonymous namespace
263
264
265
int
main
(
int
argc,
char
*argv[])
266
{
267
// Create a 2-D plot file.
268
Create2DPlotFile
();
269
270
// Create a 2-D plot with error bars file.
271
Create2DPlotWithErrorBarsFile
();
272
273
// Create a 3-D plot file.
274
Create3DPlotFile
();
275
276
return
0;
277
}
src
tools
examples
gnuplot-example.cc
Generated on Tue Oct 9 2012 16:45:46 for ns-3 by
1.8.1.2