A Discrete-Event Network Simulator
API
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 
37 {
38  std::string fileNameWithNoExtension = "plot-2d";
39  std::string graphicsFileName = fileNameWithNoExtension + ".png";
40  std::string plotFileName = fileNameWithNoExtension + ".plt";
41  std::string plotTitle = "2-D Plot";
42  std::string dataTitle = "2-D Data";
43 
44  // Instantiate the plot and set its title.
45  Gnuplot plot (graphicsFileName);
46  plot.SetTitle (plotTitle);
47 
48  // Make the graphics file, which the plot file will create when it
49  // is used with Gnuplot, be a PNG file.
50  plot.SetTerminal ("png");
51 
52  // Set the labels for each axis.
53  plot.SetLegend ("X Values", "Y Values");
54 
55  // Set the range for the x axis.
56  plot.AppendExtra ("set xrange [-6:+6]");
57 
58  // Instantiate the dataset, set its title, and make the points be
59  // plotted along with connecting lines.
60  Gnuplot2dDataset dataset;
61  dataset.SetTitle (dataTitle);
63 
64  double x;
65  double y;
66 
67  // Create the 2-D dataset.
68  for (x = -5.0; x <= +5.0; x += 1.0)
69  {
70  // Calculate the 2-D curve
71  //
72  // 2
73  // y = x .
74  //
75  y = x * x;
76 
77  // Add this point.
78  dataset.Add (x, y);
79  }
80 
81  // Add the dataset to the plot.
82  plot.AddDataset (dataset);
83 
84  // Open the plot file.
85  std::ofstream plotFile (plotFileName.c_str());
86 
87  // Write the plot file.
88  plot.GenerateOutput (plotFile);
89 
90  // Close the plot file.
91  plotFile.close ();
92 }
93 
94 
95 //===========================================================================
96 // Function: Create2DPlotWithErrorBarsFile
97 //
98 //
99 // This function creates a 2-D plot with error bars file.
100 //===========================================================================
101 
103 {
104  std::string fileNameWithNoExtension = "plot-2d-with-error-bars";
105  std::string graphicsFileName = fileNameWithNoExtension + ".png";
106  std::string plotFileName = fileNameWithNoExtension + ".plt";
107  std::string plotTitle = "2-D Plot With Error Bars";
108  std::string dataTitle = "2-D Data With Error Bars";
109 
110  // Instantiate the plot and set its title.
111  Gnuplot plot (graphicsFileName);
112  plot.SetTitle (plotTitle);
113 
114  // Make the graphics file, which the plot file will create when it
115  // is used with Gnuplot, be a PNG file.
116  plot.SetTerminal ("png");
117 
118  // Set the labels for each axis.
119  plot.SetLegend ("X Values", "Y Values");
120 
121  // Set the range for the x axis.
122  plot.AppendExtra ("set xrange [-6:+6]");
123 
124  // Instantiate the dataset, set its title, and make the points be
125  // plotted with no connecting lines.
126  Gnuplot2dDataset dataset;
127  dataset.SetTitle (dataTitle);
129 
130  // Make the dataset have error bars in both the x and y directions.
132 
133  double x;
134  double xErrorDelta;
135  double y;
136  double yErrorDelta;
137 
138  // Create the 2-D dataset.
139  for (x = -5.0; x <= +5.0; x += 1.0)
140  {
141  // Calculate the 2-D curve
142  //
143  // 2
144  // y = x .
145  //
146  y = x * x;
147 
148  // Make the uncertainty in the x direction be constant and make
149  // the uncertainty in the y direction be a constant fraction of
150  // y's value.
151  xErrorDelta = 0.25;
152  yErrorDelta = 0.1 * y;
153 
154  // Add this point with uncertainties in both the x and y
155  // direction.
156  dataset.Add (x, y, xErrorDelta, yErrorDelta);
157  }
158 
159  // Add the dataset to the plot.
160  plot.AddDataset (dataset);
161 
162  // Open the plot file.
163  std::ofstream plotFile (plotFileName.c_str());
164 
165  // Write the plot file.
166  plot.GenerateOutput (plotFile);
167 
168  // Close the plot file.
169  plotFile.close ();
170 }
171 
172 
173 //===========================================================================
174 // Function: Create3DPlotFile
175 //
176 //
177 // This function creates a 3-D plot file.
178 //===========================================================================
179 
181 {
182  std::string fileNameWithNoExtension = "plot-3d";
183  std::string graphicsFileName = fileNameWithNoExtension + ".png";
184  std::string plotFileName = fileNameWithNoExtension + ".plt";
185  std::string plotTitle = "3-D Plot";
186  std::string dataTitle = "3-D Data";
187 
188  // Instantiate the plot and set its title.
189  Gnuplot plot (graphicsFileName);
190  plot.SetTitle (plotTitle);
191 
192  // Make the graphics file, which the plot file will create when it
193  // is used with Gnuplot, be a PNG file.
194  plot.SetTerminal ("png");
195 
196  // Rotate the plot 30 degrees around the x axis and then rotate the
197  // plot 120 degrees around the new z axis.
198  plot.AppendExtra ("set view 30, 120, 1.0, 1.0");
199 
200  // Make the zero for the z-axis be in the x-axis and y-axis plane.
201  plot.AppendExtra ("set ticslevel 0");
202 
203  // Set the labels for each axis.
204  plot.AppendExtra ("set xlabel \"X Values\"");
205  plot.AppendExtra ("set ylabel \"Y Values\"");
206  plot.AppendExtra ("set zlabel \"Z Values\"");
207 
208  // Set the ranges for the x and y axis.
209  plot.AppendExtra ("set xrange [-5:+5]");
210  plot.AppendExtra ("set yrange [-5:+5]");
211 
212  // Instantiate the dataset, set its title, and make the points be
213  // connected by lines.
214  Gnuplot3dDataset dataset;
215  dataset.SetTitle (dataTitle);
216  dataset.SetStyle ("with lines");
217 
218  double x;
219  double y;
220  double z;
221 
222  // Create the 3-D dataset.
223  for (x = -5.0; x <= +5.0; x += 1.0)
224  {
225  for (y = -5.0; y <= +5.0; y += 1.0)
226  {
227  // Calculate the 3-D surface
228  //
229  // 2 2
230  // z = x * y .
231  //
232  z = x * x * y * y;
233 
234  // Add this point.
235  dataset.Add (x, y, z);
236  }
237 
238  // The blank line is necessary at the end of each x value's data
239  // points for the 3-D surface grid to work.
240  dataset.AddEmptyLine ();
241  }
242 
243  // Add the dataset to the plot.
244  plot.AddDataset (dataset);
245 
246  // Open the plot file.
247  std::ofstream plotFile (plotFileName.c_str());
248 
249  // Write the plot file.
250  plot.GenerateOutput (plotFile);
251 
252  // Close the plot file.
253  plotFile.close ();
254 }
255 
256 } // unnamed namespace
257 
258 
259 int main (int argc, char *argv[])
260 {
261  // Create a 2-D plot file.
263 
264  // Create a 2-D plot with error bars file.
266 
267  // Create a 3-D plot file.
269 
270  return 0;
271 }
void AppendExtra(const std::string &extra)
Definition: gnuplot.cc:749
Class to represent a 3D points plot.
Definition: gnuplot.h:272
Class to represent a 2D points plot.
Definition: gnuplot.h:117
void SetTitle(const std::string &title)
Change line title.
Definition: gnuplot.cc:141
void Add(double x, double y, double z)
Definition: gnuplot.cc:593
void AddDataset(const GnuplotDataset &dataset)
Definition: gnuplot.cc:756
void SetErrorBars(enum ErrorBars errorBars)
Definition: gnuplot.cc:353
a simple class to generate gnuplot-ready plotting commands from a set of datasets.
Definition: gnuplot.h:371
void SetTitle(const std::string &title)
Definition: gnuplot.cc:730
void AddEmptyLine()
Add an empty line in the data output sequence.
Definition: gnuplot.cc:604
void GenerateOutput(std::ostream &os)
Writes gnuplot commands and data values to a single output stream.
Definition: gnuplot.cc:762
void Add(double x, double y)
Definition: gnuplot.cc:359
void SetLegend(const std::string &xLegend, const std::string &yLegend)
Definition: gnuplot.cc:736
Every class exported by the ns3 library is enclosed in the ns3 namespace.
void SetStyle(enum Style style)
Definition: gnuplot.cc:342
void SetStyle(const std::string &style)
Definition: gnuplot.cc:587
void SetTerminal(const std::string &terminal)
Definition: gnuplot.cc:724