A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
gnuplot-example.cc
Go to the documentation of this file.
1/*
2 * Copyright (c) 2011 University of Washington
3 *
4 * SPDX-License-Identifier: GPL-2.0-only
5 *
6 * Author: Mitch Watrous (watrous@u.washington.edu)
7 */
8
9#include "ns3/gnuplot.h"
10
11#include <fstream>
12
13using namespace ns3;
14
15namespace
16{
17
18/**
19 * This function creates a 2-D plot file.
20 */
21void
23{
24 std::string fileNameWithNoExtension = "plot-2d";
25 std::string graphicsFileName = fileNameWithNoExtension + ".png";
26 std::string plotFileName = fileNameWithNoExtension + ".plt";
27 std::string plotTitle = "2-D Plot";
28 std::string dataTitle = "2-D Data";
29
30 // Instantiate the plot and set its title.
31 Gnuplot plot(graphicsFileName);
32 plot.SetTitle(plotTitle);
33
34 // Make the graphics file, which the plot file will create when it
35 // is used with Gnuplot, be a PNG file.
36 plot.SetTerminal("png");
37
38 // Set the labels for each axis.
39 plot.SetLegend("X Values", "Y Values");
40
41 // Set the range for the x axis.
42 plot.AppendExtra("set xrange [-6:+6]");
43
44 // Instantiate the dataset, set its title, and make the points be
45 // plotted along with connecting lines.
46 Gnuplot2dDataset dataset;
47 dataset.SetTitle(dataTitle);
49
50 double x;
51 double y;
52
53 // Create the 2-D dataset.
54 for (x = -5.0; x <= +5.0; x += 1.0)
55 {
56 // Calculate the 2-D curve
57 //
58 // 2
59 // y = x .
60 //
61 y = x * x;
62
63 // Add this point.
64 dataset.Add(x, y);
65 }
66
67 // Add the dataset to the plot.
68 plot.AddDataset(dataset);
69
70 // Open the plot file.
71 std::ofstream plotFile(plotFileName);
72
73 // Write the plot file.
74 plot.GenerateOutput(plotFile);
75
76 // Close the plot file.
77 plotFile.close();
78}
79
80/**
81 * This function creates a 2-D plot with error bars file.
82 */
83void
85{
86 std::string fileNameWithNoExtension = "plot-2d-with-error-bars";
87 std::string graphicsFileName = fileNameWithNoExtension + ".png";
88 std::string plotFileName = fileNameWithNoExtension + ".plt";
89 std::string plotTitle = "2-D Plot With Error Bars";
90 std::string dataTitle = "2-D Data With Error Bars";
91
92 // Instantiate the plot and set its title.
93 Gnuplot plot(graphicsFileName);
94 plot.SetTitle(plotTitle);
95
96 // Make the graphics file, which the plot file will create when it
97 // is used with Gnuplot, be a PNG file.
98 plot.SetTerminal("png");
99
100 // Set the labels for each axis.
101 plot.SetLegend("X Values", "Y Values");
102
103 // Set the range for the x axis.
104 plot.AppendExtra("set xrange [-6:+6]");
105
106 // Instantiate the dataset, set its title, and make the points be
107 // plotted with no connecting lines.
108 Gnuplot2dDataset dataset;
109 dataset.SetTitle(dataTitle);
111
112 // Make the dataset have error bars in both the x and y directions.
114
115 double x;
116 double xErrorDelta;
117 double y;
118 double yErrorDelta;
119
120 // Create the 2-D dataset.
121 for (x = -5.0; x <= +5.0; x += 1.0)
122 {
123 // Calculate the 2-D curve
124 //
125 // 2
126 // y = x .
127 //
128 y = x * x;
129
130 // Make the uncertainty in the x direction be constant and make
131 // the uncertainty in the y direction be a constant fraction of
132 // y's value.
133 xErrorDelta = 0.25;
134 yErrorDelta = 0.1 * y;
135
136 // Add this point with uncertainties in both the x and y
137 // direction.
138 dataset.Add(x, y, xErrorDelta, yErrorDelta);
139 }
140
141 // Add the dataset to the plot.
142 plot.AddDataset(dataset);
143
144 // Open the plot file.
145 std::ofstream plotFile(plotFileName);
146
147 // Write the plot file.
148 plot.GenerateOutput(plotFile);
149
150 // Close the plot file.
151 plotFile.close();
152}
153
154/**
155 * This function creates a 3-D plot file.
156 */
157void
159{
160 std::string fileNameWithNoExtension = "plot-3d";
161 std::string graphicsFileName = fileNameWithNoExtension + ".png";
162 std::string plotFileName = fileNameWithNoExtension + ".plt";
163 std::string plotTitle = "3-D Plot";
164 std::string dataTitle = "3-D Data";
165
166 // Instantiate the plot and set its title.
167 Gnuplot plot(graphicsFileName);
168 plot.SetTitle(plotTitle);
169
170 // Make the graphics file, which the plot file will create when it
171 // is used with Gnuplot, be a PNG file.
172 plot.SetTerminal("png");
173
174 // Rotate the plot 30 degrees around the x axis and then rotate the
175 // plot 120 degrees around the new z axis.
176 plot.AppendExtra("set view 30, 120, 1.0, 1.0");
177
178 // Make the zero for the z-axis be in the x-axis and y-axis plane.
179 plot.AppendExtra("set ticslevel 0");
180
181 // Set the labels for each axis.
182 plot.AppendExtra("set xlabel \"X Values\"");
183 plot.AppendExtra("set ylabel \"Y Values\"");
184 plot.AppendExtra("set zlabel \"Z Values\"");
185
186 // Set the ranges for the x and y axis.
187 plot.AppendExtra("set xrange [-5:+5]");
188 plot.AppendExtra("set yrange [-5:+5]");
189
190 // Instantiate the dataset, set its title, and make the points be
191 // connected by lines.
192 Gnuplot3dDataset dataset;
193 dataset.SetTitle(dataTitle);
194 dataset.SetStyle("with lines");
195
196 double x;
197 double y;
198 double z;
199
200 // Create the 3-D dataset.
201 for (x = -5.0; x <= +5.0; x += 1.0)
202 {
203 for (y = -5.0; y <= +5.0; y += 1.0)
204 {
205 // Calculate the 3-D surface
206 //
207 // 2 2
208 // z = x * y .
209 //
210 z = x * x * y * y;
211
212 // Add this point.
213 dataset.Add(x, y, z);
214 }
215
216 // The blank line is necessary at the end of each x value's data
217 // points for the 3-D surface grid to work.
218 dataset.AddEmptyLine();
219 }
220
221 // Add the dataset to the plot.
222 plot.AddDataset(dataset);
223
224 // Open the plot file.
225 std::ofstream plotFile(plotFileName);
226
227 // Write the plot file.
228 plot.GenerateOutput(plotFile);
229
230 // Close the plot file.
231 plotFile.close();
232}
233
234} // unnamed namespace
235
236int
237main(int argc, char* argv[])
238{
239 // Create a 2-D plot file.
241
242 // Create a 2-D plot with error bars file.
244
245 // Create a 3-D plot file.
247
248 return 0;
249}
Class to represent a 2D points plot.
Definition gnuplot.h:105
void SetErrorBars(ErrorBars errorBars)
Definition gnuplot.cc:360
void SetStyle(Style style)
Definition gnuplot.cc:348
void Add(double x, double y)
Definition gnuplot.cc:366
Class to represent a 3D points plot.
Definition gnuplot.h:261
void AddEmptyLine()
Add an empty line in the data output sequence.
Definition gnuplot.cc:622
void Add(double x, double y, double z)
Definition gnuplot.cc:611
void SetStyle(const std::string &style)
Definition gnuplot.cc:605
void SetTitle(const std::string &title)
Change line title.
Definition gnuplot.cc:137
a simple class to generate gnuplot-ready plotting commands from a set of datasets.
Definition gnuplot.h:359
void AddDataset(const GnuplotDataset &dataset)
Definition gnuplot.cc:785
void SetLegend(const std::string &xLegend, const std::string &yLegend)
Definition gnuplot.cc:765
void SetTerminal(const std::string &terminal)
Definition gnuplot.cc:753
void AppendExtra(const std::string &extra)
Definition gnuplot.cc:778
void GenerateOutput(std::ostream &os)
Writes gnuplot commands and data values to a single output stream.
Definition gnuplot.cc:791
void SetTitle(const std::string &title)
Definition gnuplot.cc:759
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.