A Discrete-Event Network Simulator
API
gnuplot-example.cc
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2011 University of Washington
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License version 2 as
6  * published by the Free Software Foundation;
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  * GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program; if not, write to the Free Software
15  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16  *
17  * Author: Mitch Watrous (watrous@u.washington.edu)
18  */
19 
20 #include "ns3/gnuplot.h"
21 
22 #include <fstream>
23 
24 using namespace ns3;
25 
26 namespace
27 {
28 
32 void
34 {
35  std::string fileNameWithNoExtension = "plot-2d";
36  std::string graphicsFileName = fileNameWithNoExtension + ".png";
37  std::string plotFileName = fileNameWithNoExtension + ".plt";
38  std::string plotTitle = "2-D Plot";
39  std::string dataTitle = "2-D Data";
40 
41  // Instantiate the plot and set its title.
42  Gnuplot plot(graphicsFileName);
43  plot.SetTitle(plotTitle);
44 
45  // Make the graphics file, which the plot file will create when it
46  // is used with Gnuplot, be a PNG file.
47  plot.SetTerminal("png");
48 
49  // Set the labels for each axis.
50  plot.SetLegend("X Values", "Y Values");
51 
52  // Set the range for the x axis.
53  plot.AppendExtra("set xrange [-6:+6]");
54 
55  // Instantiate the dataset, set its title, and make the points be
56  // plotted along with connecting lines.
57  Gnuplot2dDataset dataset;
58  dataset.SetTitle(dataTitle);
60 
61  double x;
62  double y;
63 
64  // Create the 2-D dataset.
65  for (x = -5.0; x <= +5.0; x += 1.0)
66  {
67  // Calculate the 2-D curve
68  //
69  // 2
70  // y = x .
71  //
72  y = x * x;
73 
74  // Add this point.
75  dataset.Add(x, y);
76  }
77 
78  // Add the dataset to the plot.
79  plot.AddDataset(dataset);
80 
81  // Open the plot file.
82  std::ofstream plotFile(plotFileName);
83 
84  // Write the plot file.
85  plot.GenerateOutput(plotFile);
86 
87  // Close the plot file.
88  plotFile.close();
89 }
90 
94 void
96 {
97  std::string fileNameWithNoExtension = "plot-2d-with-error-bars";
98  std::string graphicsFileName = fileNameWithNoExtension + ".png";
99  std::string plotFileName = fileNameWithNoExtension + ".plt";
100  std::string plotTitle = "2-D Plot With Error Bars";
101  std::string dataTitle = "2-D Data With Error Bars";
102 
103  // Instantiate the plot and set its title.
104  Gnuplot plot(graphicsFileName);
105  plot.SetTitle(plotTitle);
106 
107  // Make the graphics file, which the plot file will create when it
108  // is used with Gnuplot, be a PNG file.
109  plot.SetTerminal("png");
110 
111  // Set the labels for each axis.
112  plot.SetLegend("X Values", "Y Values");
113 
114  // Set the range for the x axis.
115  plot.AppendExtra("set xrange [-6:+6]");
116 
117  // Instantiate the dataset, set its title, and make the points be
118  // plotted with no connecting lines.
119  Gnuplot2dDataset dataset;
120  dataset.SetTitle(dataTitle);
122 
123  // Make the dataset have error bars in both the x and y directions.
125 
126  double x;
127  double xErrorDelta;
128  double y;
129  double yErrorDelta;
130 
131  // Create the 2-D dataset.
132  for (x = -5.0; x <= +5.0; x += 1.0)
133  {
134  // Calculate the 2-D curve
135  //
136  // 2
137  // y = x .
138  //
139  y = x * x;
140 
141  // Make the uncertainty in the x direction be constant and make
142  // the uncertainty in the y direction be a constant fraction of
143  // y's value.
144  xErrorDelta = 0.25;
145  yErrorDelta = 0.1 * y;
146 
147  // Add this point with uncertainties in both the x and y
148  // direction.
149  dataset.Add(x, y, xErrorDelta, yErrorDelta);
150  }
151 
152  // Add the dataset to the plot.
153  plot.AddDataset(dataset);
154 
155  // Open the plot file.
156  std::ofstream plotFile(plotFileName);
157 
158  // Write the plot file.
159  plot.GenerateOutput(plotFile);
160 
161  // Close the plot file.
162  plotFile.close();
163 }
164 
168 void
170 {
171  std::string fileNameWithNoExtension = "plot-3d";
172  std::string graphicsFileName = fileNameWithNoExtension + ".png";
173  std::string plotFileName = fileNameWithNoExtension + ".plt";
174  std::string plotTitle = "3-D Plot";
175  std::string dataTitle = "3-D Data";
176 
177  // Instantiate the plot and set its title.
178  Gnuplot plot(graphicsFileName);
179  plot.SetTitle(plotTitle);
180 
181  // Make the graphics file, which the plot file will create when it
182  // is used with Gnuplot, be a PNG file.
183  plot.SetTerminal("png");
184 
185  // Rotate the plot 30 degrees around the x axis and then rotate the
186  // plot 120 degrees around the new z axis.
187  plot.AppendExtra("set view 30, 120, 1.0, 1.0");
188 
189  // Make the zero for the z-axis be in the x-axis and y-axis plane.
190  plot.AppendExtra("set ticslevel 0");
191 
192  // Set the labels for each axis.
193  plot.AppendExtra("set xlabel \"X Values\"");
194  plot.AppendExtra("set ylabel \"Y Values\"");
195  plot.AppendExtra("set zlabel \"Z Values\"");
196 
197  // Set the ranges for the x and y axis.
198  plot.AppendExtra("set xrange [-5:+5]");
199  plot.AppendExtra("set yrange [-5:+5]");
200 
201  // Instantiate the dataset, set its title, and make the points be
202  // connected by lines.
203  Gnuplot3dDataset dataset;
204  dataset.SetTitle(dataTitle);
205  dataset.SetStyle("with lines");
206 
207  double x;
208  double y;
209  double z;
210 
211  // Create the 3-D dataset.
212  for (x = -5.0; x <= +5.0; x += 1.0)
213  {
214  for (y = -5.0; y <= +5.0; y += 1.0)
215  {
216  // Calculate the 3-D surface
217  //
218  // 2 2
219  // z = x * y .
220  //
221  z = x * x * y * y;
222 
223  // Add this point.
224  dataset.Add(x, y, z);
225  }
226 
227  // The blank line is necessary at the end of each x value's data
228  // points for the 3-D surface grid to work.
229  dataset.AddEmptyLine();
230  }
231 
232  // Add the dataset to the plot.
233  plot.AddDataset(dataset);
234 
235  // Open the plot file.
236  std::ofstream plotFile(plotFileName);
237 
238  // Write the plot file.
239  plot.GenerateOutput(plotFile);
240 
241  // Close the plot file.
242  plotFile.close();
243 }
244 
245 } // unnamed namespace
246 
247 int
248 main(int argc, char* argv[])
249 {
250  // Create a 2-D plot file.
252 
253  // Create a 2-D plot with error bars file.
255 
256  // Create a 3-D plot file.
258 
259  return 0;
260 }
Class to represent a 2D points plot.
Definition: gnuplot.h:118
void SetErrorBars(enum ErrorBars errorBars)
Definition: gnuplot.cc:356
void SetStyle(enum Style style)
Definition: gnuplot.cc:345
void Add(double x, double y)
Definition: gnuplot.cc:362
Class to represent a 3D points plot.
Definition: gnuplot.h:274
void AddEmptyLine()
Add an empty line in the data output sequence.
Definition: gnuplot.cc:607
void Add(double x, double y, double z)
Definition: gnuplot.cc:596
void SetStyle(const std::string &style)
Definition: gnuplot.cc:590
void SetTitle(const std::string &title)
Change line title.
Definition: gnuplot.cc:141
a simple class to generate gnuplot-ready plotting commands from a set of datasets.
Definition: gnuplot.h:373
void AddDataset(const GnuplotDataset &dataset)
Definition: gnuplot.cc:759
void SetLegend(const std::string &xLegend, const std::string &yLegend)
Definition: gnuplot.cc:739
void SetTerminal(const std::string &terminal)
Definition: gnuplot.cc:727
void AppendExtra(const std::string &extra)
Definition: gnuplot.cc:752
void GenerateOutput(std::ostream &os)
Writes gnuplot commands and data values to a single output stream.
Definition: gnuplot.cc:765
void SetTitle(const std::string &title)
Definition: gnuplot.cc:733
void Create2DPlotWithErrorBarsFile()
This function creates a 2-D plot with error bars file.
void Create3DPlotFile()
This function creates a 3-D plot file.
void Create2DPlotFile()
This function creates a 2-D plot file.
Every class exported by the ns3 library is enclosed in the ns3 namespace.