sciplot  0.3.1
A modern C++ plotting library powered by gnuplot
Plot.hpp
1 // sciplot - a modern C++ scientific plotting library powered by gnuplot
2 // https://github.com/sciplot/sciplot
3 //
4 // Licensed under the MIT License <http://opensource.org/licenses/MIT>.
5 //
6 // Copyright (c) 2018-2022 Allan Leal, Bim Overbohm
7 //
8 // Permission is hereby granted, free of charge, to any person obtaining a copy
9 // of this software and associated documentation files (the "Software"), to deal
10 // in the Software without restriction, including without limitation the rights
11 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 // copies of the Software, and to permit persons to whom the Software is
13 // furnished to do so, subject to the following conditions:
14 //
15 // The above copyright notice and this permission notice shall be included in all
16 // copies or substantial portions of the Software.
17 //
18 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24 // SOFTWARE.
25 
26 #pragma once
27 
28 // C++ includes
29 #include <sstream>
30 #include <vector>
31 
32 // sciplot includes
33 #include <sciplot/Constants.hpp>
34 #include <sciplot/Default.hpp>
35 #include <sciplot/Enums.hpp>
36 #include <sciplot/Palettes.hpp>
37 #include <sciplot/StringOrDouble.hpp>
38 #include <sciplot/Utils.hpp>
39 #include <sciplot/specs/AxisLabelSpecs.hpp>
40 #include <sciplot/specs/BorderSpecs.hpp>
41 #include <sciplot/specs/DrawSpecs.hpp>
42 #include <sciplot/specs/FillStyleSpecs.hpp>
43 #include <sciplot/specs/FontSpecsOf.hpp>
44 #include <sciplot/specs/GridSpecs.hpp>
45 #include <sciplot/specs/HistogramStyleSpecs.hpp>
46 #include <sciplot/specs/LegendSpecs.hpp>
47 #include <sciplot/specs/LineSpecsOf.hpp>
48 #include <sciplot/specs/TicsSpecs.hpp>
49 #include <sciplot/specs/TicsSpecsMajor.hpp>
50 #include <sciplot/specs/TicsSpecsMinor.hpp>
51 
52 namespace sciplot
53 {
54 
56 class Plot
57 {
58  public:
60  Plot();
61 
63  virtual ~Plot() = default;
64 
67  auto palette(const std::string& name) -> Plot&;
68 
70  auto size(std::size_t width, std::size_t height) -> Plot&;
71 
73  auto fontName(const std::string& name) -> Plot&;
74 
76  auto fontSize(std::size_t size) -> Plot&;
77 
79  auto border() -> BorderSpecs& { return m_border; }
80 
82  auto grid() -> GridSpecs& { return m_grid; }
83 
85  auto xlabel(const std::string& label) -> AxisLabelSpecs&;
86 
88  auto ylabel(const std::string& label) -> AxisLabelSpecs&;
89 
91  auto xrange(const StringOrDouble& min, const StringOrDouble& max) -> Plot&;
92 
94  auto yrange(const StringOrDouble& min, const StringOrDouble& max) -> Plot&;
95 
98  auto boxWidthAbsolute(double val) -> Plot&;
99 
102  auto boxWidthRelative(double val) -> Plot&;
103 
104  //======================================================================
105  // METHODS FOR CUSTOMIZATION OF TICS
106  //======================================================================
107 
109  auto tics() -> TicsSpecs& { return m_tics; }
110 
112  auto xtics() -> TicsSpecsMajor& { return xticsMajorBottom(); }
113 
115  auto ytics() -> TicsSpecsMajor& { return yticsMajorLeft(); }
116 
118  auto ztics() -> TicsSpecsMajor& { return zticsMajor(); }
119 
121  auto rtics() -> TicsSpecsMajor& { return rticsMajor(); }
122 
125 
128 
131 
134 
137 
140 
143 
146 
148  auto zticsMajor() -> TicsSpecsMajor& { return m_ztics_major; }
149 
151  auto zticsMinor() -> TicsSpecsMinor& { return m_ztics_minor; }
152 
154  auto rticsMajor() -> TicsSpecsMajor& { return m_rtics_major; }
155 
157  auto rticsMinor() -> TicsSpecsMinor& { return m_rtics_minor; }
158 
159  //======================================================================
160  // METHODS FOR CUSTOMIZATION OF STYLES
161  //======================================================================
162 
164  auto styleFill() -> FillStyleSpecs& { return m_style_fill; }
165 
168 
169  //======================================================================
170  // METHODS FOR DRAWING PLOT ELEMENTS
171  //======================================================================
172 
174  auto draw(const std::string& what, const std::string& use, const std::string& with) -> DrawSpecs&;
175 
176  //======================================================================
177  // MISCElLANEOUS METHODS
178  //======================================================================
179 
181  auto legend() -> LegendSpecs&;
182 
184  auto samples(std::size_t value) -> void;
185 
187  auto gnuplot(const std::string& command) -> void;
188 
190  auto savePlotData() const -> void;
191 
194  auto autoclean(bool enable = true) -> void;
195 
197  auto cleanup() const -> void;
198 
201  auto clear() -> void;
202 
204  virtual auto repr() const -> std::string = 0;
205 
206  protected:
207  static std::size_t m_counter;
208  std::size_t m_id = 0;
209  bool m_autoclean = true;
210  std::string m_palette;
211  std::size_t m_width = 0;
212  std::size_t m_height = 0;
213  std::string m_datafilename;
214  std::string m_data;
215  std::size_t m_numdatasets = 0;
219  std::string m_xrange;
220  std::string m_yrange;
238  std::string m_boxwidth;
240  std::string m_samples;
242  std::vector<DrawSpecs> m_drawspecs;
243  std::vector<std::string> m_customcmds;
244 };
245 
246 // Initialize the counter of plot objects
247 inline std::size_t Plot::m_counter = 0;
248 
249 inline Plot::Plot()
251 {
252  // Show only major and minor xtics and ytics
253  xticsMajorBottom().show();
254  xticsMinorBottom().show();
255  yticsMajorLeft().show();
256  yticsMinorLeft().show();
257  // Hide all other tics
258  xticsMajorTop().hide();
259  xticsMinorTop().hide();
260  yticsMajorRight().hide();
261  yticsMinorRight().hide();
262  zticsMajor().hide();
263  zticsMinor().hide();
264  rticsMajor().hide();
265  rticsMinor().hide();
266  // Default options for fill style
267  styleFill().solid();
268  styleFill().borderHide();
269  // Set all other default options
270  boxWidthRelative(internal::DEFAULT_FIGURE_BOXWIDTH_RELATIVE);
271  // This is needed because of how drawHistogram works. Using `with histograms` don't work as well.
272  gnuplot("set style data histogram");
273 }
274 
275 inline auto Plot::palette(const std::string& name) -> Plot&
276 {
277  m_palette = name;
278  return *this;
279 }
280 
281 inline auto Plot::size(std::size_t width, std::size_t height) -> Plot&
282 {
283  m_width = width;
284  m_height = height;
285  return *this;
286 }
287 
288 inline auto Plot::fontName(const std::string& name) -> Plot&
289 {
290  m_font.fontName(name);
291  m_legend.fontName(name);
292  return *this;
293 }
294 
295 inline auto Plot::fontSize(std::size_t size) -> Plot&
296 {
299  return *this;
300 }
301 
302 inline auto Plot::xlabel(const std::string& label) -> AxisLabelSpecs&
303 {
304  m_xlabel.text(label);
305  return m_xlabel;
306 }
307 
308 inline auto Plot::ylabel(const std::string& label) -> AxisLabelSpecs&
309 {
310  m_ylabel.text(label);
311  return m_ylabel;
312 }
313 
314 inline auto Plot::xrange(const StringOrDouble& min, const StringOrDouble& max) -> Plot&
315 {
316  m_xrange = "[" + min.value + ":" + max.value + "]";
317  return *this;
318 }
319 
320 inline auto Plot::yrange(const StringOrDouble& min, const StringOrDouble& max) -> Plot&
321 {
322  m_yrange = "[" + min.value + ":" + max.value + "]";
323  return *this;
324 }
325 
326 inline auto Plot::boxWidthAbsolute(double val) -> Plot&
327 {
328  m_boxwidth = internal::str(val) + " absolute";
329  return *this;
330 }
331 
332 inline auto Plot::boxWidthRelative(double val) -> Plot&
333 {
334  m_boxwidth = internal::str(val) + " relative";
335  return *this;
336 }
337 
338 //======================================================================
339 // METHODS FOR DRAWING PLOT ELEMENTS
340 //======================================================================
341 
342 inline auto Plot::draw(const std::string& what, const std::string& use, const std::string& with) -> DrawSpecs&
343 {
344  // Save the draw arguments for this x,y data
345  m_drawspecs.emplace_back(what, use, with);
346  // Return the just created drawing object in case the user wants to customize it
347  return m_drawspecs.back();
348 }
349 
350 //======================================================================
351 // MISCELLANEOUS METHODS
352 //======================================================================
353 
354 inline auto Plot::legend() -> LegendSpecs&
355 {
356  return m_legend;
357 }
358 
359 inline auto Plot::samples(std::size_t value) -> void
360 {
361  m_samples = internal::str(value);
362 }
363 
364 inline auto Plot::gnuplot(const std::string& command) -> void
365 {
366  m_customcmds.push_back(command);
367 }
368 
369 inline auto Plot::savePlotData() const -> void
370 {
371  // Open data file, truncate it and write all current plot data to it
372  if (!m_data.empty())
373  {
374  std::ofstream data(m_datafilename);
375  data << m_data;
376  }
377 }
378 
379 inline auto Plot::autoclean(bool enable) -> void
380 {
381  m_autoclean = enable;
382 }
383 
384 inline auto Plot::cleanup() const -> void
385 {
386  std::remove(m_datafilename.c_str());
387 }
388 
389 inline auto Plot::clear() -> void
390 {
391  m_drawspecs.clear();
392  m_customcmds.clear();
393 }
394 
395 } // namespace sciplot
auto yticsMajorLeft() -> TicsSpecsMajor &
Return the specifications of the grid lines along major ytics on the left axis.
Definition: Plot.hpp:136
auto xrange(const StringOrDouble &min, const StringOrDouble &max) -> Plot &
Set the x-range of the plot (also possible with empty values or autoscale options (e....
Definition: Plot.hpp:314
An auxiliary type used to store a string value, while the input can also be a double.
Definition: StringOrDouble.hpp:35
TicsSpecsMajor m_ytics_major_left
The specs for the major ytics at the left.
Definition: Plot.hpp:227
AxisLabelSpecs m_rlabel
The label of the r-axis.
Definition: Plot.hpp:237
auto autoclean(bool enable=true) -> void
Toggle automatic cleaning of temporary files (enabled by default).
Definition: Plot.hpp:379
auto xtics() -> TicsSpecsMajor &
Return the specifications of the grid lines along major xtics on the bottom axis.
Definition: Plot.hpp:112
auto yticsMinorLeft() -> TicsSpecsMinor &
Return the specifications of the grid lines along minor ytics on the left axis.
Definition: Plot.hpp:142
auto rtics() -> TicsSpecsMajor &
Return the specifications of the grid lines along major rtics.
Definition: Plot.hpp:121
auto boxWidthAbsolute(double val) -> Plot &
Set the default width of boxes in plots containing boxes (in absolute mode).
Definition: Plot.hpp:326
std::string m_xrange
The x-range of the plot as a gnuplot formatted string (e.g., "set xrange [0:1]")
Definition: Plot.hpp:219
auto tics() -> TicsSpecs &
Set the tics of the plot and return a reference to the corresponding specs object.
Definition: Plot.hpp:109
auto yticsMajorRight() -> TicsSpecsMajor &
Return the specifications of the grid lines along major ytics on the right axis.
Definition: Plot.hpp:139
auto styleHistogram() -> HistogramStyleSpecs &
Return an object that permits histogram style to be customized.
Definition: Plot.hpp:167
virtual ~Plot()=default
Virtual destructor to avoid memory leaks.
auto cleanup() const -> void
Delete all files used to store plot data or scripts.
Definition: Plot.hpp:384
LegendSpecs m_legend
The legend specs of the plot.
Definition: Plot.hpp:241
The class used to specify options for minor tics of a specific axis.
Definition: TicsSpecsMinor.hpp:41
auto rticsMinor() -> TicsSpecsMinor &
Return the specifications of the grid lines along minor rtics.
Definition: Plot.hpp:157
std::string m_samples
The number of sample points for functions.
Definition: Plot.hpp:240
TicsSpecs m_tics
The specs of the tics of the plot.
Definition: Plot.hpp:222
auto yticsMinorRight() -> TicsSpecsMinor &
Return the specifications of the grid lines along minor ytics on the right axis.
Definition: Plot.hpp:145
std::size_t m_id
The Plot id derived from m_counter upon construction (must be the first member due to constructor ini...
Definition: Plot.hpp:208
auto rticsMajor() -> TicsSpecsMajor &
Return the specifications of the grid lines along minor rtics.
Definition: Plot.hpp:154
std::string m_palette
The name of the gnuplot palette to be used.
Definition: Plot.hpp:210
auto xticsMajorBottom() -> TicsSpecsMajor &
Return the specifications of the grid lines along major xtics on the bottom axis.
Definition: Plot.hpp:124
auto fontSize(std::size_t size) -> Plot &
Set the font size for the plot (e.g., 10, 12, 16).
Definition: Plot.hpp:295
auto gnuplot(const std::string &command) -> void
Use this method to provide gnuplot commands to be executed before the plotting calls.
Definition: Plot.hpp:364
The class used to specify options for grid.
Definition: GridSpecs.hpp:37
auto draw(const std::string &what, const std::string &use, const std::string &with) -> DrawSpecs &
Draw plot object with given what, using and with expressions (e.g., plot.draw("sin(x)*cos(x)",...
Definition: Plot.hpp:342
std::size_t m_width
The size of the plot in x.
Definition: Plot.hpp:211
auto legend() -> LegendSpecs &
Set the legend of the plot and return a reference to the corresponding specs object.
Definition: Plot.hpp:354
GridSpecs m_grid
The vector of grid specs for the major and minor grid lines in the plot (for xtics,...
Definition: Plot.hpp:218
auto fontSize(std::size_t size) -> DerivedSpecs &
Set the point size of the font (e.g., 10, 12, 16).
Definition: FontSpecsOf.hpp:74
FillStyleSpecs m_style_fill
The specs for the fill style of the plot elements in the plot that can be painted.
Definition: Plot.hpp:239
The specifications for an axis label (e.g., xlabel, ylabel, etc.)
Definition: AxisLabelSpecs.hpp:37
TicsSpecsMajor m_xtics_major_top
The specs for the major xtics at the top.
Definition: Plot.hpp:224
auto samples(std::size_t value) -> void
Set the number of sample points for analytical plots.
Definition: Plot.hpp:359
The class used to specify options for major tics of a specific axis.
Definition: TicsSpecsMajor.hpp:40
virtual auto repr() const -> std::string=0
Convert this plot object into a gnuplot formatted string.
auto palette(const std::string &name) -> Plot &
Set the palette of colors for the plot.
Definition: Plot.hpp:275
AxisLabelSpecs m_xlabel
The label of the x-axis.
Definition: Plot.hpp:235
TicsSpecsMajor m_ztics_major
The specs for the major ztics.
Definition: Plot.hpp:231
auto boxWidthRelative(double val) -> Plot &
Set the default width of boxes in plots containing boxes (in relative mode).
Definition: Plot.hpp:332
std::size_t m_numdatasets
The current number of data sets in the data file.
Definition: Plot.hpp:215
auto xlabel(const std::string &label) -> AxisLabelSpecs &
Set the label of the x-axis and return a reference to the corresponding specs object.
Definition: Plot.hpp:302
static std::size_t m_counter
Counter of how many plot / singleplot objects have been instanciated in the application.
Definition: Plot.hpp:207
auto zticsMinor() -> TicsSpecsMinor &
Return the specifications of the grid lines along minor ztics.
Definition: Plot.hpp:151
The class used to attach color or pattern fill options to a type.
Definition: FillStyleSpecs.hpp:38
auto xticsMinorBottom() -> TicsSpecsMinor &
Return the specifications of the grid lines along minor xtics on the bottom axis.
Definition: Plot.hpp:130
The class used to specify options for legend.
Definition: LegendSpecs.hpp:40
AxisLabelSpecs m_ylabel
The label of the y-axis.
Definition: Plot.hpp:236
std::string m_data
The current plot data as a string.
Definition: Plot.hpp:214
std::string m_boxwidth
The default width of boxes in plots containing boxes without given widths.
Definition: Plot.hpp:238
The class used to specify histogram style options.
Definition: HistogramStyleSpecs.hpp:36
auto text(std::string text) -> AxisLabelSpecs &
Set the text of the axis label.
Definition: AxisLabelSpecs.hpp:73
auto zticsMajor() -> TicsSpecsMajor &
Return the specifications of the grid lines along major ztics.
Definition: Plot.hpp:148
TicsSpecsMinor m_xtics_minor_bottom
The specs for the minor xtics at the bottom.
Definition: Plot.hpp:225
auto clear() -> void
Clear all draw and gnuplot commands.
Definition: Plot.hpp:389
auto xticsMinorTop() -> TicsSpecsMinor &
Return the specifications of the grid lines along minor xtics on the top axis.
Definition: Plot.hpp:133
TicsSpecsMinor m_ytics_minor_left
The specs for the minor ytics at the left.
Definition: Plot.hpp:229
auto xticsMajorTop() -> TicsSpecsMajor &
Return the specifications of the grid lines along major xtics on the top axis.
Definition: Plot.hpp:127
auto grid() -> GridSpecs &
Set the grid of the plot and return a reference to the corresponding specs object.
Definition: Plot.hpp:82
TicsSpecsMinor m_xtics_minor_top
The specs for the minor xtics at the top.
Definition: Plot.hpp:226
auto fontName(std::string name) -> DerivedSpecs &
Set the name of the font (e.g., Helvetica, Georgia, Times).
Definition: FontSpecsOf.hpp:67
TicsSpecsMinor m_ztics_minor
The specs for the minor ztics.
Definition: Plot.hpp:232
TicsSpecsMajor m_ytics_major_right
The specs for the major ytics at the right.
Definition: Plot.hpp:228
auto ztics() -> TicsSpecsMajor &
Return the specifications of the grid lines along major ztics.
Definition: Plot.hpp:118
bool m_autoclean
Toggle automatic cleaning of temporary files (enabled by default)
Definition: Plot.hpp:209
std::string m_datafilename
The multi data set file where data given to plot (e.g., vectors) are saved.
Definition: Plot.hpp:213
TicsSpecsMajor m_xtics_major_bottom
The specs for the major xtics at the bottom.
Definition: Plot.hpp:223
FontSpecs m_font
The font name and size in the plot.
Definition: Plot.hpp:216
The class used to create a plot containing graphical elements.
Definition: Plot.hpp:57
Plot()
Construct a default Plot object.
Definition: Plot.hpp:249
The class used to specify options for plot border.
Definition: BorderSpecs.hpp:41
auto fontName(const std::string &name) -> Plot &
Set the font name for the plot (e.g., Helvetica, Georgia, Times).
Definition: Plot.hpp:288
The class used to specify options for tics.
Definition: TicsSpecs.hpp:38
std::string m_yrange
The y-range of the plot as a gnuplot formatted string (e.g., "set yrange [0:1]")
Definition: Plot.hpp:220
HistogramStyleSpecs m_style_histogram
The specs for the histogram style of the plot.
Definition: Plot.hpp:221
std::vector< std::string > m_customcmds
The strings containing gnuplot custom commands.
Definition: Plot.hpp:243
The class used to specify font options.
Definition: FontSpecsOf.hpp:60
The class where options for the plotted element can be specified.
Definition: DrawSpecs.hpp:41
auto border() -> BorderSpecs &
Set the border of the plot and return a reference to the corresponding specs object.
Definition: Plot.hpp:79
TicsSpecsMinor m_rtics_minor
The specs for the minor rtics.
Definition: Plot.hpp:234
auto yrange(const StringOrDouble &min, const StringOrDouble &max) -> Plot &
Set the y-range of the plot (also possible with empty values or autoscale options (e....
Definition: Plot.hpp:320
auto ylabel(const std::string &label) -> AxisLabelSpecs &
Set the label of the y-axis and return a reference to the corresponding specs object.
Definition: Plot.hpp:308
TicsSpecsMajor m_rtics_major
The specs for the major rtics.
Definition: Plot.hpp:233
std::size_t m_height
The size of the plot in y.
Definition: Plot.hpp:212
auto size(std::size_t width, std::size_t height) -> Plot &
Set the size of the plot (in unit of points, with 1 inch = 72 points).
Definition: Plot.hpp:281
auto savePlotData() const -> void
Write the current plot data to the data file.
Definition: Plot.hpp:369
auto ytics() -> TicsSpecsMajor &
Return the specifications of the grid lines along major ytics on the left axis.
Definition: Plot.hpp:115
std::vector< DrawSpecs > m_drawspecs
The plot specs for each call to gnuplot plot function.
Definition: Plot.hpp:242
BorderSpecs m_border
The border style of the plot.
Definition: Plot.hpp:217
TicsSpecsMinor m_ytics_minor_right
The specs for the minor ytics at the right.
Definition: Plot.hpp:230
auto styleFill() -> FillStyleSpecs &
Return an object that permits fill style to be customized.
Definition: Plot.hpp:164