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