SNAP Library 2.4, User Reference  2015-05-11 19:40:56
SNAP, a general purpose, high performance system for analysis and manipulation of large networks
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
gnuplot.cpp
Go to the documentation of this file.
1 #include "stdafx.h"
2 #include "gnuplot.h"
3 
5 // GNU-Plot-Chart
6 
7 // Set path to gnuplot
8 #if defined(GLib_WIN)
9  TStr TGnuPlot::GnuPlotPath = "C:\\gnuplot";
10  TStr TGnuPlot::GnuPlotFNm = "wgnuplot.exe";
11 #elif defined(GLib_CYGWIN)
12  TStr TGnuPlot::GnuPlotPath = "/usr/bin";
13  TStr TGnuPlot::GnuPlotFNm = "gnuplot.exe";
14 #elif defined(GLib_MACOSX)
15  TStr TGnuPlot::GnuPlotPath = "/opt/local/bin";
16  TStr TGnuPlot::GnuPlotFNm = "gnuplot";
17 #else
18  TStr TGnuPlot::GnuPlotPath = "/usr/bin";
19  TStr TGnuPlot::GnuPlotFNm = "gnuplot";
20 #endif
21 
22 // Determines the gnuplot version and the tics command syntax.
23 // Gnuplot changed the syntax with version 4.2:
24 // - before 4.2: set ticscale 2 1
25 // - 4.2 and later: set tics 2
27 #ifdef GLib_WIN
28  return -1;
29 #else
30  FILE* p;
31  char Buf[1024];
32  char Version[1024];
33  size_t n;
34 
35  // get gnuplot version
36  p = popen(TStr::Fmt("%s -V", TGnuPlot::GnuPlotFNm.CStr()).CStr(), "r");
37  if (p == NULL) { // try running using the path
38  p = popen(TStr::Fmt("%s/%s -V", TGnuPlot::GnuPlotPath.CStr(), TGnuPlot::GnuPlotFNm.CStr()).CStr(), "r");
39  if (p == NULL) { return -1; }
40  }
41  n = fread(Buf, 1, 100, p);
42  if (n <= 0) { return -1; }
43  Buf[n] = '\0';
44  pclose(p);
45  //printf("Buf %d .%s.\n", n, Buf);
46  n = sscanf(Buf, "gnuplot %s", Version);
47  if (n <= 0) { return -1; }
48  // printf("Version %d .%s.\n", n, Version);
49  if ((strlen(Version) < 3) || (Version[1] != '.')) { return -1; }
50  // test version < 4.2
51  if ((Version[0] < '4') || ((Version[0] == '4') && (Version[2] < '2'))) {
52  // printf("TGnuPlot::GetTics42 0\n");
53  return 0;
54  }
55  // printf("TGnuPlot::GetTics42 1\n");
56  return 1;
57 #endif
58 }
59 
60 int TGnuPlot::Tics42 = -2;
61 TStr TGnuPlot::DefPlotFNm = "GnuPlot.plt";
62 TStr TGnuPlot::DefDataFNm = "GnuPlot.tab";
63 
65  SeriesTy(Gps.SeriesTy), XYValV(Gps.XYValV), ZValV(Gps.ZValV),
66  Label(Gps.Label), WithStyle(Gps.WithStyle), DataFNm(Gps.DataFNm),
67  XCol(Gps.XCol), YCol(Gps.YCol), ZCol(Gps.ZCol) {
68 }
69 
71  if(this != &Gps) {
72  SeriesTy = Gps.SeriesTy;
73  XYValV = Gps.XYValV; ZValV = Gps.ZValV;
74  Label = Gps.Label;
75  DataFNm = Gps.DataFNm;
76  WithStyle = Gps.WithStyle;
77  XCol = Gps.XCol; YCol = Gps.YCol; ZCol = Gps.ZCol;
78  }
79  return *this;
80 }
81 
83  return (XYValV < Gps.XYValV) || ((XYValV == Gps.XYValV) && (Label < Gps.Label));
84 }
85 
86 TGnuPlot::TGnuPlot(const TStr& FileNm, const TStr& PlotTitle, const bool& Grid) :
87  DataFNm(FileNm+".tab"), PlotFNm(FileNm+".plt"), Title(PlotTitle), LblX(), LblY(), ScaleTy(gpsAuto),
88  YRange(0, 0), XRange(0, 0), SetGrid(Grid), SetPause(true),
89  SeriesV(), MoreCmds() {
90  IAssert(! FileNm.Empty());
91 }
92 
93 TGnuPlot::TGnuPlot(const TStr& DataFileNm, const TStr& PlotFileNm, const TStr& PlotTitle, const bool& Grid) :
94  DataFNm(DataFileNm.Empty() ? DefDataFNm : DataFileNm),
95  PlotFNm(PlotFileNm.Empty() ? DefPlotFNm : PlotFileNm),
96  Title(PlotTitle), LblX(), LblY(), ScaleTy(gpsAuto),
97  YRange(0, 0), XRange(0, 0), SetGrid(Grid), SetPause(true), SeriesV(), MoreCmds() {
98 }
99 
100 TGnuPlot::TGnuPlot(const TGnuPlot& GnuPlot) : DataFNm(GnuPlot.DataFNm), PlotFNm(GnuPlot.PlotFNm),
101  Title(GnuPlot.Title), LblX(GnuPlot.LblX), LblY(GnuPlot.LblY), ScaleTy(GnuPlot.ScaleTy), YRange(GnuPlot.YRange),
102  XRange(GnuPlot.XRange), SetGrid(GnuPlot.SetGrid), SetPause(GnuPlot.SetPause), SeriesV(GnuPlot.SeriesV),
103  MoreCmds(GnuPlot.MoreCmds) {
104 }
105 
107  if (this != &GnuPlot) {
108  DataFNm = GnuPlot.DataFNm;
109  PlotFNm = GnuPlot.PlotFNm;
110  Title = GnuPlot.Title;
111  LblX = GnuPlot.LblX;
112  LblY = GnuPlot.LblY;
113  ScaleTy = GnuPlot.ScaleTy;
114  YRange = GnuPlot.YRange;
115  XRange = GnuPlot.XRange;
116  SetGrid = GnuPlot.SetGrid;
117  SetPause = GnuPlot.SetPause;
118  SeriesV = GnuPlot.SeriesV;
119  MoreCmds = GnuPlot.MoreCmds;
120  }
121  return *this;
122 }
123 
124 TStr TGnuPlot::GetSeriesPlotStr(const int& SeriesId) {
125  TChA PlotStr;
126  TGpSeries& Series = SeriesV[SeriesId];
127  if (SeriesId != 0) PlotStr += ",\\\n\t";
128  if (Series.XCol >= 0) {
129  PlotStr += "\"" + Series.DataFNm + "\" using " + TInt::GetStr(Series.XCol);
130  if (Series.YCol != 0) { PlotStr += ":" + TInt::GetStr(Series.YCol); }
131  if (Series.ZCol != 0) { PlotStr += ":" + TInt::GetStr(Series.ZCol); }
132  else if (Series.SeriesTy==gpwFilledCurves) { PlotStr += ":(0)"; } // filled curves requres 3rd column
133  } else {
134  // function
135  //IAssertR(Series.DataFNm.SearchCh('=') != -1, TStr::Fmt("Expression %s is not a function", Series.DataFNm.CStr()));
136  PlotStr += Series.DataFNm;
137  }
138  PlotStr += " title \"" + Series.Label + "\"";
139  // hard coded line style
140  if (Series.WithStyle.Empty()) {
141  if (Series.SeriesTy == gpwLines) Series.WithStyle = "lw 1";
142  if (Series.SeriesTy == gpwPoints) Series.WithStyle = "pt 6"; // circles
143  if (Series.SeriesTy == gpwLinesPoints) Series.WithStyle = "pt 6"; // circles
144  if (Series.SeriesTy == gpwBoxes) Series.WithStyle = "fill solid 0.3";
145  }
146  PlotStr += " with " + GetSeriesTyStr(Series.SeriesTy) + " " + Series.WithStyle;
147  return PlotStr;
148 }
149 
150 //GP.AddFunc("2*x**-2+4", gpwLines, "2*x^-2+4");
151 int TGnuPlot::AddFunc(const TStr& FuncStr, const TGpSeriesTy& SeriesTy, const TStr& Label, const TStr& Style) {
152  const int Id = SeriesV.Len();
153  TGpSeries Plot;
154  Plot.SeriesTy = SeriesTy;
155  Plot.Label = Label;
156  if (! FuncStr.Empty()) { Plot.DataFNm = TStr::Fmt("f%d(x)=%s, f%d(x)", Id, FuncStr.CStr(), Id); }
157  else { Plot.DataFNm = TStr::Fmt("f%d(x)", Id); }
158  Plot.XCol = -1;
159  Plot.WithStyle = Style;
160  SeriesV.Add(Plot);
161  return Id;
162 }
163 
164 int TGnuPlot::AddPlot(const TStr& DataFNm, const int& ColY,
165  const TGpSeriesTy& SeriesTy, const TStr& Label, const TStr& Style) {
166  return AddPlot(DataFNm, 0, ColY, SeriesTy, Label, Style);
167 }
168 
169 int TGnuPlot::AddPlot(const TStr& DataFNm, const int& ColX, const int& ColY,
170  const TGpSeriesTy& SeriesTy, const TStr& Label, const TStr& Style) {
171  IAssert(ColY > 0); IAssert(ColX >= 0);
172  TGpSeries Plot;
173  Plot.SeriesTy = SeriesTy;
174  Plot.Label = Label;
175  Plot.DataFNm = DataFNm; Plot.DataFNm.ChangeStrAll("\\", "\\\\");
176  Plot.XCol = ColX; Plot.YCol = ColY; Plot.ZCol = 0;
177  Plot.WithStyle = Style;
178  SeriesV.Add(Plot);
179  return SeriesV.Len() - 1;
180 }
181 
182 int TGnuPlot::AddPlot(const TIntV& YValV, const TGpSeriesTy& SeriesTy, const TStr& Label, const TStr& Style) {
183  TFltKdV XYValV(YValV.Len(), 0);
184  for (int i = 0; i < YValV.Len(); i++) {
185  XYValV.Add(TFltKd(TFlt(i+1), TFlt(YValV[i])));
186  }
187  return AddPlot(XYValV, SeriesTy, Label, Style);
188 }
189 
190 int TGnuPlot::AddPlot(const TFltV& YValV, const TGpSeriesTy& SeriesTy, const TStr& Label, const TStr& Style) {
191  TFltKdV XYValV(YValV.Len(), 0);
192  for (int i = 0; i < YValV.Len(); i++) {
193  XYValV.Add(TFltKd(TFlt(i+1), TFlt(YValV[i])));
194  }
195  return AddPlot(XYValV, SeriesTy, Label, Style);
196 }
197 
198 int TGnuPlot::AddPlot(const TFltV& XValV, const TFltV& YValV, const TGpSeriesTy& SeriesTy, const TStr& Label, const TStr& Style) {
199  IAssert(XValV.Len() == YValV.Len());
200  TFltKdV XYValV(XValV.Len(), 0);
201  for (int i = 0; i < YValV.Len(); i++) {
202  XYValV.Add(TFltKd(TFlt(XValV[i]), TFlt(YValV[i])));
203  }
204  return AddPlot(XYValV, SeriesTy, Label, Style);
205 }
206 
207 int TGnuPlot::AddPlot(const TIntPrV& XYValV, const TGpSeriesTy& SeriesTy, const TStr& Label, const TStr& Style) {
208  TFltKdV XYFltValV(XYValV.Len(), 0);
209  for (int i = 0; i < XYValV.Len(); i++) {
210  XYFltValV.Add(TFltKd(TFlt(XYValV[i].Val1), TFlt(XYValV[i].Val2)));
211  }
212  return AddPlot(XYFltValV, SeriesTy, Label, Style);
213 }
214 
215 int TGnuPlot::AddPlot(const TFltPrV& XYValV, const TGpSeriesTy& SeriesTy, const TStr& Label, const TStr& Style) {
216  TFltKdV XYFltValV(XYValV.Len(), 0);
217  for (int i = 0; i < XYValV.Len(); i++) {
218  XYFltValV.Add(TFltKd(XYValV[i].Val1, XYValV[i].Val2));
219  }
220  return AddPlot(XYFltValV, SeriesTy, Label, Style);
221 }
222 
223 int TGnuPlot::AddPlot(const TIntKdV& XYValV, const TGpSeriesTy& SeriesTy, const TStr& Label, const TStr& Style) {
224  TFltKdV XYFltValV(XYValV.Len(), 0);
225  for (int i = 0; i < XYValV.Len(); i++) {
226  XYFltValV.Add(TFltKd(TFlt(XYValV[i].Key), TFlt(XYValV[i].Dat)));
227  }
228  return AddPlot(XYFltValV, SeriesTy, Label, Style);
229 }
230 
231 int TGnuPlot::AddPlot(const TIntFltKdV& XYValV, const TGpSeriesTy& SeriesTy, const TStr& Label, const TStr& Style) {
232  TFltKdV XYFltValV(XYValV.Len(), 0);
233  for (int i = 0; i < XYValV.Len(); i++) {
234  XYFltValV.Add(TFltKd(TFlt(XYValV[i].Key), TFlt(XYValV[i].Dat)));
235  }
236  return AddPlot(XYFltValV, SeriesTy, Label, Style);
237 }
238 
239 int TGnuPlot::AddPlot(const TIntFltPrV& XYValV, const TGpSeriesTy& SeriesTy, const TStr& Label, const TStr& Style) {
240  TFltKdV XYFltValV(XYValV.Len(), 0);
241  for (int i = 0; i < XYValV.Len(); i++) {
242  XYFltValV.Add(TFltKd(TFlt(XYValV[i].Val1), TFlt(XYValV[i].Val2)));
243  }
244  return AddPlot(XYFltValV, SeriesTy, Label, Style);
245 }
246 
247 int TGnuPlot::AddPlot(const TFltKdV& XYValV, const TGpSeriesTy& SeriesTy, const TStr& Label, const TStr& Style) {
248  if (XYValV.Empty()) {
249  printf("***AddPlot: empty plot (%s) %s\n", DataFNm.CStr(), Title.CStr());
250  return -1;
251  }
252  TGpSeries Plot;
253  Plot.SeriesTy = SeriesTy;
254  Plot.Label = Label;
255  Plot.XYValV = XYValV;
256  Plot.WithStyle = Style;
257  SeriesV.Add(Plot);
258  return SeriesV.Len() - 1;
259 }
260 
261 int TGnuPlot::AddErrBar(const TFltTrV& XYDValV, const TStr& Label) {
262  TFltKdV XYFltValV(XYDValV.Len(), 0);
263  TFltV DeltaV(XYDValV.Len(), 0);
264  for (int i = 0; i < XYDValV.Len(); i++) {
265  XYFltValV.Add(TFltKd(XYDValV[i].Val1, XYDValV[i].Val2));
266  DeltaV.Add(XYDValV[i].Val3);
267  }
268  return AddErrBar(XYFltValV, DeltaV, Label);
269 }
270 
271 int TGnuPlot::AddErrBar(const TFltTrV& XYDValV, const TStr& DatLabel, const TStr& ErrLabel) {
272  TFltKdV XYFltValV(XYDValV.Len(), 0);
273  TFltV DeltaV(XYDValV.Len(), 0);
274  for (int i = 0; i < XYDValV.Len(); i++) {
275  XYFltValV.Add(TFltKd(XYDValV[i].Val1, XYDValV[i].Val2));
276  DeltaV.Add(XYDValV[i].Val3);
277  }
278  const int PlotId = AddPlot(XYFltValV, gpwLinesPoints, DatLabel);
279  AddErrBar(XYFltValV, DeltaV, ErrLabel);
280  return PlotId;
281 }
282 
283 int TGnuPlot::AddErrBar(const TFltV& YValV, const TFltV& DeltaYV, const TStr& Label) {
284  IAssert(YValV.Len() == DeltaYV.Len());
285  TFltKdV XYFltValV(YValV.Len(), 0);
286  for (int i = 0; i < YValV.Len(); i++) {
287  XYFltValV.Add(TFltKd(TFlt(i+1), YValV[i]));
288  }
289  return AddErrBar(XYFltValV, DeltaYV, Label);
290 }
291 
292 int TGnuPlot::AddErrBar(const TFltV& XValV, const TFltV& YValV, const TFltV& DeltaYV, const TStr& Label) {
293  IAssert(XValV.Len() == YValV.Len());
294  IAssert(XValV.Len() == DeltaYV.Len());
295  TFltKdV XYFltValV(XValV.Len(), 0);
296  for (int i = 0; i < XValV.Len(); i++) {
297  XYFltValV.Add(TFltKd(XValV[i], YValV[i]));
298  }
299  return AddErrBar(XYFltValV, DeltaYV, Label);
300 }
301 
302 int TGnuPlot::AddErrBar(const TFltPrV& XYValV, const TFltV& DeltaYV, const TStr& Label) {
303  TFltKdV XYFltValV(XYValV.Len(), 0);
304  for (int i = 0; i < XYValV.Len(); i++) {
305  XYFltValV.Add(TFltKd(XYValV[i].Val1, XYValV[i].Val2));
306  }
307  return AddErrBar(XYFltValV, DeltaYV, Label);
308 }
309 
310 int TGnuPlot::AddErrBar(const TFltPrV& XYValV, const TFltV& DeltaV, const TStr& DatLabel, const TStr& ErrLabel) {
311  TFltKdV XYFltValV(XYValV.Len(), 0);
312  for (int i = 0; i < XYValV.Len(); i++) {
313  XYFltValV.Add(TFltKd(XYValV[i].Val1, XYValV[i].Val2));
314  }
315  const int PlotId = AddPlot(XYFltValV, gpwLinesPoints, DatLabel);
316  AddErrBar(XYFltValV, DeltaV, ErrLabel);
317  return PlotId;
318 }
319 
320 int TGnuPlot::AddErrBar(const TFltKdV& XYValV, const TFltV& DeltaYV, const TStr& Label) {
321  if (XYValV.Empty()) {
322  printf("***AddErrBar: empty plot (%s) %s\n", DataFNm.CStr(), Title.CStr());
323  return -1;
324  }
325  IAssert(XYValV.Len() == DeltaYV.Len());
326  TGpSeries Plot;
327  Plot.SeriesTy = gpwErrBars;
328  Plot.Label = Label;
329  Plot.XYValV = XYValV;
330  Plot.ZValV = DeltaYV;
331  SeriesV.Add(Plot);
332  return SeriesV.Len() - 1;
333 }
334 
335 int TGnuPlot::AddLinFit(const int& PlotId, const TGpSeriesTy& SeriesTy, const TStr& Style) {
336  if (PlotId < 0 || PlotId >= SeriesV.Len()) return -1;
337  const TGpSeries& Plot = SeriesV[PlotId];
338  if(Plot.XYValV.Empty()) return -1;
339  const TFltKdV& XY = Plot.XYValV;
340  double A, B, R2, SigA, SigB, Chi2;
341  // linear fit
342  TFltPrV XYPr;
343  int s;
344  for (s = 0; s < XY.Len(); s++) {
345  XYPr.Add(TFltPr(XY[s].Key, XY[s].Dat));
346  }
347  TSpecFunc::LinearFit(XYPr, A, B, SigA, SigB, Chi2, R2);
348  TStr StyleStr=Style;
349  if (StyleStr.Empty()) { StyleStr = "linewidth 3"; }
350  const int FitId = AddFunc(TStr::Fmt("%f+%f*x", A, B),
351  SeriesTy, TStr::Fmt("%.4g + %.4g x R^2:%.2g", A, B, R2), StyleStr);
352  return FitId;
353  /*SeriesV.Add();
354  TGpSeries& NewPlot = SeriesV.Last();
355  TFltKdV& EstXY = NewPlot.XYValV;
356  for (s = 0; s < XY.Len(); s++) {
357  EstXY.Add(TFltKd(XY[s].Key, A + B*XYPr[s].Val1));
358  }
359  NewPlot.Label = TStr::Fmt("%.4g + %.4g x R^2:%.2g", A, B, R2);
360  NewPlot.SeriesTy = SeriesTy;
361  if (Style.Empty()) { NewPlot.WithStyle = "linewidth 3"; }
362  else { NewPlot.WithStyle = Style; }
363  return SeriesV.Len() - 1;*/
364 }
365 
366 int TGnuPlot::AddPwrFit(const int& PlotId, const TGpSeriesTy& SeriesTy, const TStr& Style) {
367  const int PlotId1 = AddPwrFit3(PlotId, SeriesTy);
368  AddPwrFit2(PlotId, SeriesTy, 5.0);
369  return PlotId1;
370 }
371 
372 // linear fit on log-log scales{%
373 int TGnuPlot::AddPwrFit1(const int& PlotId, const TGpSeriesTy& SeriesTy, const TStr& Style) {
374  if (PlotId < 0 || PlotId >= SeriesV.Len()) return -1;
375  const TGpSeries& Plot = SeriesV[PlotId];
376  if(Plot.XYValV.Empty()) return -1;
377  const TFltKdV& XY = Plot.XYValV;
378  double A, B, R2, SigA, SigB, Chi2, MinY = TFlt::Mx, MinX = TFlt::Mx;
379  // power fit
380  TFltPrV XYPr;
381  int s;
382  for (s = 0; s < XY.Len(); s++) {
383  if (XY[s].Key > 0) {
384  XYPr.Add(TFltPr(XY[s].Key, XY[s].Dat));
385  MinX = TMath::Mn(MinX, XY[s].Key());
386  MinY = TMath::Mn(MinY, XY[s].Dat());
387  }
388  }
389  MinY = TMath::Mn(1.0, MinY);
390  TSpecFunc::PowerFit(XYPr, A, B, SigA, SigB, Chi2, R2);
391  TStr StyleStr=Style;
392  if (StyleStr.Empty()) { StyleStr = "linewidth 3"; }
393  const int FitId = AddFunc(TStr::Fmt("%f*x**%f", A, B),
394  SeriesTy, TStr::Fmt("%.1g * x^{%.4g} R^2:%.2g", A, B, R2), StyleStr);
395  return FitId;
396  /*SeriesV.Add();
397  TGpSeries& NewPlot = SeriesV.Last();
398  const int FitId = SeriesV.Len() - 1;
399  NewPlot.DataFNm = ;
400  TFltKdV& EstXY = NewPlot.XYValV;
401  for (s = 0; s < XYPr.Len(); s++) {
402  const double YVal = A*pow(XYPr[s].Val1(), B);
403  if (YVal < MinY) continue;
404  EstXY.Add(TFltKd(XYPr[s].Val1, YVal));
405  }
406  NewPlot.Label = ;
407  NewPlot.SeriesTy = SeriesTy;
408  if (Style.Empty()) { NewPlot.WithStyle = "linewidth 3"; }
409  else { NewPlot.WithStyle = Style; }
410  //if (MinX < 5.0) MinX = 5.0;
411  //AddPwrFit2(PlotId, SeriesTy, MinX);*/
412 }
413 
414 // MLE power-coefficient
415 int TGnuPlot::AddPwrFit2(const int& PlotId, const TGpSeriesTy& SeriesTy, const double& MinX, const TStr& Style) {
416  const TGpSeries& Plot = SeriesV[PlotId];
417  if(Plot.XYValV.Empty()) return -1;
418  const TFltKdV& XY = Plot.XYValV;
419  // power fit
420  TFltPrV XYPr;
421  double MinY = TFlt::Mx;
422  for (int s = 0; s < XY.Len(); s++) {
423  if (XY[s].Key > 0.0) {
424  XYPr.Add(TFltPr(XY[s].Key, XY[s].Dat));
425  MinY = TMath::Mn(MinY, XY[s].Dat());
426  }
427  }
428  if (XYPr.Empty()) return -1;
429  MinY = TMath::Mn(1.0, MinY);
430  // determine the sign of power coefficient
431  double CoefSign = 0.0;
432  { double A, B, R2, SigA, SigB, Chi2;
433  TSpecFunc::PowerFit(XYPr, A, B, SigA, SigB, Chi2, R2);
434  CoefSign = B > 0.0 ? +1.0 : -1.0; }
435  const double PowerCf = CoefSign * TSpecFunc::GetPowerCoef(XYPr, MinX);
436  int Mid = (int) exp(log((double)XYPr.Len())/2.0);
437  if (Mid >= XYPr.Len()) { Mid = XYPr.Len()-1; }
438  const double MidX = XYPr[Mid].Val1();
439  const double MidY = XYPr[Mid].Val2();
440  const double B = MidY / pow(MidX, PowerCf);
441  TStr StyleStr=Style;
442  if (StyleStr.Empty()) { StyleStr = "linewidth 3"; }
443  const int FitId = AddFunc(TStr::Fmt("%f*x**%f", B, PowerCf),
444  SeriesTy, TStr::Fmt("MLE = x^{%.4g}", PowerCf), StyleStr);
445  return FitId;
446  /*SeriesV.Add();
447  TGpSeries& NewPlot = SeriesV.Last();
448  TFltKdV& XYFit = NewPlot.XYValV;
449  XYFit.Gen(XYPr.Len(), 0);
450  for (int s = 0; s < XYPr.Len(); s++) {
451  const double XVal = XYPr[s].Val1;
452  const double YVal = B * pow(XYPr[s].Val1(), PowerCf);
453  if (YVal < MinY || XVal < MinX) continue;
454  XYFit.Add(TFltKd(XVal, YVal));
455  }
456  NewPlot.Label = TStr::Fmt("PowerFit: %g", PowerCf);
457  NewPlot.SeriesTy = SeriesTy;
458  if (Style.Empty()) { NewPlot.WithStyle = "linewidth 3"; }
459  else { NewPlot.WithStyle = Style; }
460  return SeriesV.Len() - 1;*/
461 }
462 
463 int TGnuPlot::AddPwrFit3(const int& PlotId, const TGpSeriesTy& SeriesTy, const double& MinX, const TStr& Style) {
464  double Intercept, Slope, R2;
465  return AddPwrFit3(PlotId, SeriesTy, MinX, Style, Intercept, Slope, R2);
466 }
467 
468 // some kind of least squares power-law fitting that cutts the tail until the fit is good
469 int TGnuPlot::AddPwrFit3(const int& PlotId, const TGpSeriesTy& SeriesTy, const double& MinX, const TStr& Style, double& Intercept, double& Slope, double& R2) {
470  if (PlotId < 0 || PlotId >= SeriesV.Len()) return -1;
471  const TGpSeries& Plot = SeriesV[PlotId];
472  if(Plot.XYValV.Empty()) return -1;
473  double A, B, SigA, SigB, Chi2, MinY=TFlt::Mx;
474  const TFltKdV& XY = Plot.XYValV;
475  //SeriesV.Add();
476  //TGpSeries& NewPlot = SeriesV.Last();
477  //TFltKdV& EstXY = NewPlot.XYValV;
478  TFltPrV FitXY, NewFitXY;
479  for (int s = 0; s < XY.Len(); s++) {
480  if (XY[s].Key > 0 && XY[s].Key >= MinX) {
481  FitXY.Add(TFltPr(XY[s].Key, XY[s].Dat));
482  MinY = TMath::Mn(MinY, XY[s].Dat());
483  }
484  }
485  MinY = TMath::Mn(1.0, MinY);
486  // power fit (if tail is too fat, cut everything where
487  // extrapolation sets the value < MinY
488  while (true) {
489  TSpecFunc::PowerFit(FitXY, A, B, SigA, SigB, Chi2, R2);
490  NewFitXY.Clr(false);
491  //EstXY.Clr(false);
492  for (int s = 0; s < FitXY.Len(); s++) {
493  const double YVal = A*pow(FitXY[s].Val1(), B);
494  if (YVal < MinY) continue;
495  //EstXY.Add(TFltKd(FitXY[s].Val1, YVal));
496  NewFitXY.Add(TFltPr(FitXY[s].Val1, FitXY[s].Val2));
497  }
498  if (NewFitXY.Len() < 10 || FitXY.Last().Val1 < 1.2 * NewFitXY.Last().Val1) { break; }
499  else { FitXY.Swap(NewFitXY); }
500  }
501  TStr StyleStr=Style;
502  if (StyleStr.Empty()) { StyleStr = "linewidth 3"; }
503  const int FitId = AddFunc(TStr::Fmt("%f*x**%f", A, B),
504  SeriesTy, TStr::Fmt("%.1g * x^{%.4g} R^2:%.2g", A, B, R2), StyleStr);
505  return FitId;
506  /*NewPlot.Label = TStr::Fmt("%.1g * x^{%.4g} R^2:%.2g", A, B, R2);
507  Intercept = A;
508  Slope = B;
509  NewPlot.SeriesTy = SeriesTy;
510  if (Style.Empty()) { NewPlot.WithStyle = "linewidth 3"; }
511  else { NewPlot.WithStyle = Style; }
512  return SeriesV.Len() - 1;*/
513 }
514 
515 int TGnuPlot::AddLogFit(const int& PlotId, const TGpSeriesTy& SeriesTy, const TStr& Style) {
516  const TGpSeries& Plot = SeriesV[PlotId];
517  if(Plot.XYValV.Empty()) return -1;
518  const TFltKdV& XY = Plot.XYValV;
519  double A, B, R2, SigA, SigB, Chi2;
520  // power fit
521  TFltPrV XYPr;
522  int s;
523  for (s = 0; s < XY.Len(); s++) {
524  if (XY[s].Key > 0) {
525  XYPr.Add(TFltPr(XY[s].Key, XY[s].Dat)); }
526  }
527  TSpecFunc::LogFit(XYPr, A, B, SigA, SigB, Chi2, R2);
528  TStr StyleStr=Style;
529  if (StyleStr.Empty()) { StyleStr = "linewidth 3"; }
530  const int FitId = AddFunc(TStr::Fmt("%f+%f*log(x)", A, B),
531  SeriesTy, TStr::Fmt("%.4g + %.4g log(x) R^2:%.2g", A, B, R2), StyleStr);
532  return FitId;
533  /*SeriesV.Add();
534  TGpSeries& NewPlot = SeriesV.Last();
535  TFltKdV& EstXY = NewPlot.XYValV;
536  for (s = 0; s < XYPr.Len(); s++) {
537  EstXY.Add(TFltKd(XYPr[s].Val1, A+B*log((double)XYPr[s].Val1)));
538  }
539  NewPlot.Label = TStr::Fmt("%.4g + %.4g log(x) R^2:%.2g", A, B, R2);
540  NewPlot.SeriesTy = SeriesTy;
541  if (Style.Empty()) { NewPlot.WithStyle = "linewidth 3"; }
542  else { NewPlot.WithStyle = Style; }
543  return SeriesV.Len() - 1;*/
544 }
545 
546 int TGnuPlot::AddExpFit(const int& PlotId, const TGpSeriesTy& SeriesTy, const double& FitXOffset, const TStr& Style) {
547  const TGpSeries& Plot = SeriesV[PlotId];
548  if(Plot.XYValV.Empty()) return -1;
549  const TFltKdV& XY = Plot.XYValV;
550  double A, B, R2, SigA, SigB, Chi2;
551  // power fit
552  TFltPrV XYPr;
553  int s;
554  for (s = 0; s < XY.Len(); s++) {
555  if (XY[s].Key-FitXOffset > 0) {
556  XYPr.Add(TFltPr(XY[s].Key-FitXOffset, XY[s].Dat)); }
557  }
558  TSpecFunc::ExpFit(XYPr, A, B, SigA, SigB, Chi2, R2);
559  TStr Label, StyleStr=Style;
560  if (FitXOffset == 0) { Label = TStr::Fmt("%.4g exp(%.4g x) R^2:%.2g", A, B, R2); }
561  else { Label = TStr::Fmt("%.4g exp(%.4g x - %g) R^2:%.2g", A, B, FitXOffset, R2); }
562  if (StyleStr.Empty()) { StyleStr = "linewidth 3"; }
563  const int FitId = AddFunc(TStr::Fmt("%f*exp(%f*x-%f)", A, B, FitXOffset),
564  SeriesTy, Label, StyleStr);
565  return FitId;
566  /*SeriesV.Add();
567  TGpSeries& NewPlot = SeriesV.Last();
568  TFltKdV& EstXY = NewPlot.XYValV;
569  for (s = 0; s < XYPr.Len(); s++) {
570  EstXY.Add(TFltKd(XYPr[s].Val1+FitXOffset, A*exp(B*XYPr[s].Val1)));
571  }
572  NewPlot.SeriesTy = SeriesTy;
573  if (Style.Empty()) { NewPlot.WithStyle = "linewidth 3"; }
574  else { NewPlot.WithStyle = Style; }
575  return SeriesV.Len() - 1;*/
576 }
577 
578 void TGnuPlot::SavePng(const TStr& FNm, const int& SizeX, const int& SizeY, const TStr& Comment, const TStr& Terminal) {
579  if (Terminal.Empty()) {
580  //#ifdef GLib_WIN
581  //#ifndef GLib_MACOSX // The standard GNUPlot for MacOS does not support PNG (Jure: actually version 4.6 DOES!)
582  // RS 2014/06/17 standard GNUPlot is tricky to configure for PNG on MacOS
583  AddCmd(TStr::Fmt("set terminal png size %d,%d", SizeX, SizeY));
584  AddCmd(TStr::Fmt("set output '%s'", FNm.CStr()));
585  //#else // EPS
586  //AddCmd("set terminal postscript eps 10 enhanced color");
587  //AddCmd(TStr::Fmt("set output '%s%s.eps'", FNm.GetFPath().CStr(), FNm.GetFMid().CStr()));
588  //#endif
589  } else {
590  AddCmd(Terminal);
591  AddCmd(TStr::Fmt("set output '%s'", FNm.CStr()));
592  }
593  Pause(false);
594  CreatePlotFile(Comment.Empty()? Title : Comment);
595  RunGnuPlot();
596  MoreCmds.DelLast();
597  MoreCmds.DelLast();
598 }
599 
600 void TGnuPlot::SaveEps(const TStr& FNm, const int& FontSz, const TStr& Comment) {
601  AddCmd(TStr::Fmt("set terminal postscript enhanced eps %d color", FontSz));
602  AddCmd(TStr::Fmt("set output '%s'", FNm.CStr()));
603  Pause(false);
604  CreatePlotFile(Comment.Empty()? Title : Comment);
605  RunGnuPlot();
606  MoreCmds.DelLast();
607  MoreCmds.DelLast();
608 }
609 
610 void TGnuPlot::MakeExpBins(const TFltPrV& XYValV, TFltPrV& ExpXYValV, const double& BinFactor, const double& MinYVal) {
611  TFltKdV KdV(XYValV.Len(), 0), OutV;
612  for (int i = 0; i < XYValV.Len(); i++) {
613  KdV.Add(TFltKd(XYValV[i].Val1, XYValV[i].Val2)); }
614  KdV.Sort();
615  TGnuPlot::MakeExpBins(KdV, OutV, BinFactor, MinYVal);
616  ExpXYValV.Gen(OutV.Len(), 0);
617  for (int i = 0; i < OutV.Len(); i++) {
618  ExpXYValV.Add(TFltPr(OutV[i].Key, OutV[i].Dat)); }
619 }
620 
621 void TGnuPlot::MakeExpBins(const TFltKdV& XYValV, TFltKdV& ExpXYValV, const double& BinFactor, const double& MinYVal) {
622  if (XYValV.Empty()) { ExpXYValV.Clr(false); return; }
623  IAssert(! XYValV.Empty());
624  IAssert(XYValV.IsSorted());
625  const TFlt MxX = XYValV.Last().Key;
626  // find buckets
627  TFltV BucketEndV; BucketEndV.Add(1);
628  double PrevBPos = 1, BPos = 1;
629  while (BPos <= MxX) {
630  PrevBPos = (uint) floor(BPos);
631  BPos *= BinFactor;
632  if (floor(BPos) == PrevBPos) {
633  BPos = PrevBPos + 1; }
634  BucketEndV.Add(floor(BPos));
635  }
636  //printf("buckets:\n"); for (int i = 0; i < BucketEndV.Len(); i++) { printf("\t%g\n", BucketEndV[i]);}
637  ExpXYValV.Gen(BucketEndV.Len(), 0);
638  int CurB = 0;
639  double AvgPos=0, Cnt=0, AvgVal=0;
640  for (int v = 0; v < XYValV.Len(); v++) {
641  if (XYValV[v].Key() == 0.0) { continue; }
642  AvgPos += XYValV[v].Key ;//* XYValV[v].Dat; // x
643  AvgVal += XYValV[v].Dat; // y
644  Cnt++;
645  if (v+1 == XYValV.Len() || XYValV[v+1].Key > BucketEndV[CurB]) {
646  if (Cnt != 0) {
647  //AvgPos /= AvgVal;
648  //AvgVal /= (BucketEndV[CurB]-BucketEndV[CurB-1]);
649  AvgPos /= (double) Cnt;
650  AvgVal /= (double) Cnt;
651  if (AvgVal < MinYVal) { AvgVal = MinYVal; }
652  ExpXYValV.Add(TFltKd(AvgPos, AvgVal));
653  //printf("b: %6.2f\t%6.2f\n", AvgPos, AvgVal);
654  AvgPos = 0; AvgVal = 0; Cnt = 0;
655  }
656  CurB++;
657  }
658  }
659 }
660 
661 void TGnuPlot::LoadTs(const TStr& FNm, TStrV& ColNmV, TVec<TFltKdV>& ColV) {
662  PSs Ss = TSs::LoadTxt(ssfTabSep, FNm);
663  int row = 0;
664  ColNmV.Clr();
665  while (Ss->At(0, row)[0] == '#') { row++; }
666  for (int c = 1; c < Ss->GetXLen(row); c+=2) {
667  ColNmV.Add(Ss->At(c, row));
668  }
669  row++;
670  ColV.Gen(ColNmV.Len(), ColNmV.Len());
671  for (; row < Ss->GetYLen(); row++) {
672  for (int c = 0; c < Ss->GetXLen(row); c+=2) {
673  if (Ss->At(c,row).Empty()) break;
674  ColV[c/2].Add(TFltKd(Ss->At(c,row).GetFlt(), Ss->At(c+1,row).GetFlt()));
675  }
676  }
677 }
678 
680  switch(ScaleTy){
681  case gpsNoAuto: return TStr("set noautoscale");
682  case gpsAuto: return TStr("set autoscale");
683  case gpsLog: return TStr("set logscale");
684  case gpsLog2X: return TStr("set logscale x 2");
685  case gpsLog2Y: return TStr("set logscale y 2");
686  case gpsLog2XY: return TStr("set logscale xy 2");
687  case gpsLog10X: return TStr("set logscale x 10");
688  case gpsLog10Y: return TStr("set logscale y 10");
689  case gpsLog10XY: return TStr("set logscale xy 10");
690  default: Fail;
691  }
692  return TStr();
693 }
694 
696  switch(SeriesTy) {
697  case gpwLines: return TStr("lines");
698  case gpwPoints: return TStr("points");
699  case gpwLinesPoints: return TStr("linespoints");
700  case gpwImpulses: return TStr("impulses");
701  case gpwDots: return TStr("dots");
702  case gpwSteps: return TStr("steps");
703  case gpwFSteps: return TStr("fsteps");
704  case gpwHiSteps: return TStr("histeps");
705  case gpwBoxes: return TStr("boxes");
706  case gpwErrBars: return TStr("errorbars");
707  case gpwFilledCurves: return TStr("filledcurves");
708  default: Fail;
709  }
710  return TStr();
711 }
712 
713 void TGnuPlot::SaveTs(const TIntKdV& KdV, const TStr& FNm, const TStr& HeadLn) {
714  FILE *F = fopen(FNm.CStr(), "wt");
715  EAssert(F);
716  if (! HeadLn.Empty()) fprintf(F, "# %s\n", HeadLn.CStr());
717  for (int i = 0; i < KdV.Len(); i++) {
718  fprintf(F, "%d\t%d\n", KdV[i].Key(), KdV[i].Dat()); }
719  fclose(F);
720 }
721 
722 
723 void TGnuPlot::SaveTs(const TIntFltKdV& KdV, const TStr& FNm, const TStr& HeadLn) {
724  FILE *F = fopen(FNm.CStr(), "wt");
725  EAssert(F);
726  if (! HeadLn.Empty()) fprintf(F, "# %s\n", HeadLn.CStr());
727  for (int i = 0; i < KdV.Len(); i++)
728  fprintf(F, "%d\t%g\n", KdV[i].Key(), KdV[i].Dat());
729  fclose(F);
730 }
731 
733  TFltV DeltaY;
734  TFltPrV ValV1, ValV2, ValV3;
735  for (int i = 1; i < 30; i++) {
736  ValV1.Add(TFltPr(i, pow(double(i), 1.2)));
737  DeltaY.Add(5*TInt::Rnd.GetUniDev());
738  ValV2.Add(TFltPr(i, 5*i-1));
739  }
740  for (int i = -10; i < 20; i++) {
741  ValV3.Add(TFltPr(i, 2*i + 2 + TInt::Rnd.GetUniDev()));
742  }
743  TGnuPlot GnuPlot("testDat", "TestPlot", true);
744  GnuPlot.SetXYLabel("X", "Y");
745  const int id2 = GnuPlot.AddPlot(ValV2, gpwPoints, "y=5*x-1");
746  const int id3 = GnuPlot.AddPlot(ValV3, gpwPoints, "y=2*x+2");
747  GnuPlot.AddErrBar(ValV1, DeltaY, "y=x^2", "Error bar");
748  GnuPlot.AddLinFit(id2, gpwLines);
749  GnuPlot.AddLinFit(id3, gpwLines);
750  GnuPlot.Plot();
751  GnuPlot.SavePng("testPlot.png");
752 }
753 
754 int TGnuPlot::IsSameXCol(const int& CurId, const int& PrevId) const {
755  //if (SerId < 1) { return -1; }
756  if (SeriesV[CurId].XYValV.Len() != SeriesV[PrevId].XYValV.Len()) { return -1; }
757  for (int x = 0; x < SeriesV[CurId].XYValV.Len(); x++) {
758  if (SeriesV[CurId].XYValV[x] != SeriesV[PrevId].XYValV[x]) { return -1; }
759  }
760  IAssert(SeriesV[PrevId].XCol > 0);
761  return SeriesV[PrevId].XCol;
762 }
763 
764 void TGnuPlot::CreatePlotFile(const TStr& Comment) {
765  time_t ltime; time(&ltime);
766  char* TimeStr = ctime(&ltime); TimeStr[strlen(TimeStr) - 1] = 0;
767  // rearrange columns so that longest are on the left
768  //SeriesV.Sort(false);
769  TIntV SerIdV(SeriesV.Len(), 0);
770  for (int i = 0; i < SeriesV.Len(); i++) { SerIdV.Add(i); }
771  SerIdV.SortCmp(TGpSeriesCmp(SeriesV));
772  // set columns
773  int ColCnt = 1;
774  bool SaveData = false;
775  for (int s = 0; s < SeriesV.Len(); s++) {
776  TGpSeries& Plt = SeriesV[SerIdV[s]];
777  if (Plt.XYValV.Empty()) { continue; }
778  Plt.DataFNm = DataFNm;
779  // plots use same X column
780  const int PrevCol = s > 0 ? IsSameXCol(SerIdV[s], SerIdV[s-1]) : -1;
781  if (PrevCol != -1) { Plt.XCol = PrevCol; }
782  else { Plt.XCol = ColCnt; ColCnt++; }
783  Plt.YCol = ColCnt; ColCnt++;
784  if (! Plt.ZValV.Empty()) { Plt.ZCol = ColCnt; ColCnt++; }
785  if (! Plt.XYValV.Empty()) { SaveData=true; }
786  }
787  // save data file (skip duplicate X columns)
788  if (SaveData) {
789  FILE *F = fopen(DataFNm.CStr(), "wt");
790  EAssertR(F != NULL, TStr("Can not open data file ")+DataFNm);
791  fprintf(F, "#\n");
792  fprintf(F, "# %s (%s)\n", Comment.CStr(), TimeStr);
793  fprintf(F, "#\n");
794  // column names
795  for (int i = 0; i < SerIdV.Len(); i++) {
796  const TGpSeries& Ser = SeriesV[SerIdV[i]];
797  if (Ser.XYValV.Empty()) { continue; }
798  if (i == 0) { fprintf(F, "# "); } else { fprintf(F, "\t"); }
799  if (Ser.SaveXVals()) {
800  if (! LblX.Empty()) { fprintf(F, "%s\t", LblX.CStr()); }
801  else { fprintf(F, "XVals\t"); }
802  }
803  if (Ser.Label.Empty()) { fprintf(F, "%s", LblY.CStr()); }
804  else { fprintf(F, "%s", SeriesV[SerIdV[i]].Label.CStr()); }
805  if (Ser.ZCol > 0) fprintf(F, "\tDeltaY");
806  }
807  fprintf(F, "\n");
808  // data
809  for (int row = 0; row < SeriesV[SerIdV[0]].XYValV.Len(); row++) {
810  for (int i = 0; i < SeriesV.Len(); i++) {
811  const TGpSeries& Ser = SeriesV[SerIdV[i]];
812  if (row < Ser.XYValV.Len()) {
813  if (i > 0) { fprintf(F, "\t"); }
814  if (Ser.SaveXVals()) { fprintf(F, "%g\t%g", Ser.XYValV[row].Key(), Ser.XYValV[row].Dat()); }
815  else { fprintf(F, "%g", Ser.XYValV[row].Dat()); }
816  if (! Ser.ZValV.Empty()) { fprintf(F, "\t%g", Ser.ZValV[row]()); }
817  }
818  }
819  fprintf(F, "\n");
820  }
821  fclose(F);
822  }
823  // save plot file
824  FILE *F = fopen(PlotFNm.CStr(), "wt");
825  EAssertR(F != 0, TStr("Can not open plot file ")+PlotFNm);
826  TStr CurDir = TDir::GetCurDir();
827  CurDir.ChangeStrAll("\\", "\\\\");
828  fprintf(F, "#\n");
829  fprintf(F, "# %s (%s)\n", Comment.CStr(), TimeStr);
830  fprintf(F, "#\n\n");
831  if (! Title.Empty()) fprintf(F, "set title \"%s\"\n", Title.CStr());
832  fprintf(F, "set key bottom right\n");
833  fprintf(F, "%s\n", GetScaleStr(ScaleTy).CStr());
835  fprintf(F, "set format x \"10^{%%L}\"\n");
836  fprintf(F, "set mxtics 10\n"); }
838  fprintf(F, "set format y \"10^{%%L}\"\n");
839  fprintf(F, "set mytics 10\n"); }
840  if (ScaleTy==gpsLog2X || ScaleTy==gpsLog2XY) { fprintf(F, "set format x \"2^{%%L}\"\n"); }
841  if (ScaleTy==gpsLog2Y || ScaleTy==gpsLog2XY) { fprintf(F, "set format y \"2^{%%L}\"\n"); }
842  if (SetGrid) fprintf(F, "set grid\n");
843  if (XRange.Val1 != XRange.Val2) fprintf(F, "set xrange [%g:%g]\n", XRange.Val1(), XRange.Val2());
844  if (YRange.Val1 != YRange.Val2) fprintf(F, "set yrange [%g:%g]\n", YRange.Val1(), YRange.Val2());
845  if (! LblX.Empty()) fprintf(F, "set xlabel \"%s\"\n", LblX.CStr());
846  if (! LblY.Empty()) fprintf(F, "set ylabel \"%s\"\n", LblY.CStr());
847  if (Tics42 < -1) {
848  Tics42 = GetTics42();
849  }
850  if (Tics42) {
851  fprintf(F, "set tics scale 2\n"); // New in version 4.2
852  } else {
853  fprintf(F, "set ticscale 2 1\n"); // Old (deprecated)
854  }
855  // custom commands
856  for (int i = 0; i < MoreCmds.Len(); i++) {
857  fprintf(F, "%s\n", MoreCmds[i].CStr()); }
858  // plot
859  if (! SeriesV.Empty()) {
860  fprintf(F, "plot \t");
861  for (int i = 0; i < SeriesV.Len(); i++) {
862  fprintf(F, "%s", GetSeriesPlotStr(i).CStr()); }
863  fprintf(F, "\n");
864  }
865  if (SetPause) fprintf(F, "pause -1 \"Hit return to exit. %s\"\n", PlotFNm.CStr());
866  fclose(F);
867 }
868 
869 void TGnuPlot::RunGnuPlot() const {
870  // try running gnuplot
871  if (system(TStr::Fmt("%s %s", GnuPlotFNm.CStr(), PlotFNm.CStr()).CStr())==0) { return; }
872  if (! GnuPlotPath.Empty()) {
873  #if defined(GLib_WIN)
874  if (system(TStr::Fmt("%s\\%s %s", GnuPlotPath.CStr(), GnuPlotFNm.CStr(), PlotFNm.CStr()).CStr())==0) { return; }
875  #else
876  if (system(TStr::Fmt("%s/%s %s", GnuPlotPath.CStr(), GnuPlotFNm.CStr(), PlotFNm.CStr()).CStr())==0) { return; }
877  #endif
878  }
879  //Old
880  //#if defined(GLib_WIN)
881  //if (system(TStr::Fmt(".\\%s %s", GpFNm.CStr(), PlotFNm.CStr()).CStr())==0) { return; }
882  //#else
883  //if (system(TStr::Fmt("./%s %s", GpFNm.CStr(), PlotFNm.CStr()).CStr())==0) { return; }
884  //#endif
885  //if (system(TStr::Fmt("%s%s %s", GpPath.CStr(), GpFNm.CStr(), PlotFNm.CStr()).CStr())==0) { return; }
886  //FailR(TStr::Fmt("Cannot find GnuPlot (%s) for plot %s. Set the PATH.", GpFNm.CStr(), PlotFNm.CStr()).CStr());
887  //ErrNotify(TStr::Fmt("Cannot find GnuPlot (%s) for plot %s. Set the PATH.", GpFNm.CStr(), PlotFNm.CStr()).CStr());
888  fprintf(stderr, "[%s:%d] Cannot find GnuPlot (%s) for plot %s. Set the $$PATH variable or TGnuPlot::GnuPlotPath. (%s)\n", __FILE__, __LINE__, GnuPlotFNm.CStr(), PlotFNm.CStr(), TGnuPlot::GnuPlotPath.CStr());
889 }
890 
TGnuPlot & operator=(const TGnuPlot &GnuPlot)
Definition: gnuplot.cpp:106
#define IAssert(Cond)
Definition: bd.h:262
static const T & Mn(const T &LVal, const T &RVal)
Definition: xmath.h:36
static TStr GetCurDir()
Definition: xfl.cpp:233
int AddLinFit(const int &PlotId, const TGpSeriesTy &SeriesTy=gpwLines, const TStr &Style=TStr())
Definition: gnuplot.cpp:335
TFltPr XRange
Definition: gnuplot.h:53
TStr GetStr() const
Definition: dt.h:1105
int AddExpFit(const int &PlotId, const TGpSeriesTy &SeriesTy=gpwLines, const double &FitXOffset=0.0, const TStr &Style=TStr())
Definition: gnuplot.cpp:546
TStr Title
Definition: gnuplot.h:51
bool operator<(const TGpSeries &Gps) const
Definition: gnuplot.cpp:82
TGpSeriesTy SeriesTy
Definition: gnuplot.h:27
static PSs LoadTxt(const TSsFmt &SsFmt, const TStr &FNm, const PNotify &Notify=NULL, const bool &IsExcelEoln=true, const int &MxY=-1, const TIntV &AllowedColNV=TIntV(), const bool &IsQStr=true)
Definition: ss.cpp:100
static void MakeExpBins(const TFltPrV &XYValV, TFltPrV &ExpXYValV, const double &BinFactor=2, const double &MinYVal=1)
Definition: gnuplot.cpp:610
void SavePng(const int &SizeX=1000, const int &SizeY=800, const TStr &Comment=TStr())
Definition: gnuplot.h:120
TStr DataFNm
Definition: gnuplot.h:50
static void PowerFit(const TVec< TFltPr > &XY, double &A, double &B, double &SigA, double &SigB, double &Chi2, double &R2)
Definition: xmath.cpp:193
unsigned int uint
Definition: bd.h:11
TGnuPlot(const TStr &FileNm="gplot", const TStr &PlotTitle=TStr(), const bool &Grid=true)
Definition: gnuplot.cpp:86
#define Fail
Definition: bd.h:238
bool Empty() const
Definition: bd.h:501
static void ExpFit(const TVec< TFltPr > &XY, double &A, double &B, double &SigA, double &SigB, double &Chi2, double &R2)
Definition: xmath.cpp:218
TSizeTy Len() const
Returns the number of elements in the vector.
Definition: ds.h:535
static void LogFit(const TVec< TFltPr > &XY, double &A, double &B, double &SigA, double &SigB, double &Chi2, double &R2)
Definition: xmath.cpp:208
void SetXYLabel(const TStr &XLabel, const TStr &YLabel)
Definition: gnuplot.h:73
static int GetTics42()
Definition: gnuplot.cpp:26
int AddPwrFit3(const int &PlotId, const TGpSeriesTy &SeriesTy=gpwLines, const double &MinX=-1.0, const TStr &Style=TStr())
Definition: gnuplot.cpp:463
TKeyDat< TFlt, TFlt > TFltKd
Definition: ds.h:386
TGpSeries & operator=(const TGpSeries &Gps)
Definition: gnuplot.cpp:70
void Plot(const TStr &Comment=TStr())
Definition: gnuplot.h:126
TStr LblY
Definition: gnuplot.h:51
TVec< TGpSeries > SeriesV
Definition: gnuplot.h:55
Definition: gnuplot.h:7
static TRnd Rnd
Definition: dt.h:1051
static const double Mx
Definition: dt.h:1296
int AddPwrFit(const int &PlotId, const TGpSeriesTy &SeriesTy=gpwLines, const TStr &Style=TStr())
Definition: gnuplot.cpp:366
Definition: dt.h:1291
bool Empty() const
Tests whether the vector is empty.
Definition: ds.h:530
static void LoadTs(const TStr &FNm, TStrV &ColNmV, TVec< TFltKdV > &ColV)
Definition: gnuplot.cpp:661
void Swap(TVec< TVal, TSizeTy > &Vec)
Swaps the contents of the vector with Vec.
Definition: ds.h:1011
static void SaveTs(const TIntKdV &KdV, const TStr &FNm, const TStr &HeadLn=TStr())
Definition: gnuplot.cpp:713
void Clr(const bool &DoDel=true, const TSizeTy &NoDelLim=-1)
Clears the contents of the vector.
Definition: ds.h:953
int ChangeStrAll(const TStr &SrcStr, const TStr &DstStr, const bool &FromStartP=false)
Definition: dt.cpp:1141
void AddCmd(const TStr &Cmd)
Definition: gnuplot.h:81
Definition: gnuplot.h:7
TFltKdV XYValV
Definition: gnuplot.h:28
static TStr GetSeriesTyStr(const TGpSeriesTy &SeriesTy)
Definition: gnuplot.cpp:695
bool SetPause
Definition: gnuplot.h:54
int AddLogFit(const int &PlotId, const TGpSeriesTy &SeriesTy=gpwLines, const TStr &Style=TStr())
Definition: gnuplot.cpp:515
bool SetGrid
Definition: gnuplot.h:54
void Pause(const bool &DoPause)
Definition: gnuplot.h:77
const TVal & Last() const
Returns a reference to the last element of the vector.
Definition: ds.h:539
Tab separated.
Definition: ss.h:6
bool SaveXVals() const
Definition: gnuplot.h:37
TPair< TFlt, TFlt > TFltPr
Definition: ds.h:99
void SaveEps(const int &FontSz=30, const TStr &Comment=TStr())
Definition: gnuplot.h:123
TGpScaleTy ScaleTy
Definition: gnuplot.h:52
int AddPwrFit2(const int &PlotId, const TGpSeriesTy &SeriesTy=gpwLines, const double &MinX=-1.0, const TStr &Style=TStr())
Definition: gnuplot.cpp:415
static double GetPowerCoef(const TFltV &XValV, double MinX=-1.0)
Definition: xmath.cpp:299
Definition: dt.h:201
#define EAssert(Cond)
Definition: bd.h:280
TStr LblX
Definition: gnuplot.h:51
int IsSameXCol(const int &CurId, const int &PrevId) const
Definition: gnuplot.cpp:754
TKey Key
Definition: ds.h:338
Definition: dt.h:412
bool Empty() const
Definition: dt.h:488
static TStr DefDataFNm
Definition: gnuplot.h:23
static TStr Fmt(const char *FmtStr,...)
Definition: dt.cpp:1599
bool IsSorted(const bool &Asc=true) const
Checks whether the vector is sorted in ascending (if Asc=true) or descending (if Asc=false) order...
Definition: ds.h:1223
static TStr GnuPlotPath
Path to GnuPlot executable. Set if gnuplot is not found in the PATH.
Definition: gnuplot.h:19
#define EAssertR(Cond, MsgStr)
Definition: bd.h:283
TFltPr YRange
Definition: gnuplot.h:53
TVal1 Val1
Definition: ds.h:34
int AddPlot(const TIntV &YValV, const TGpSeriesTy &SeriesTy=gpwLinesPoints, const TStr &Label=TStr(), const TStr &Style=TStr())
Definition: gnuplot.cpp:182
TVal2 Val2
Definition: ds.h:35
Definition: bd.h:196
static TStr DefPlotFNm
Definition: gnuplot.h:22
static void LinearFit(const TVec< TFltPr > &XY, double &A, double &B, double &SigA, double &SigB, double &Chi2, double &R2)
Definition: xmath.cpp:150
void Gen(const TSizeTy &_Vals)
Constructs a vector (an array) of _Vals elements.
Definition: ds.h:486
static void Test()
Definition: gnuplot.cpp:732
static TStr GetScaleStr(const TGpScaleTy &ScaleTy)
Definition: gnuplot.cpp:679
int AddPwrFit1(const int &PlotId, const TGpSeriesTy &SeriesTy=gpwLines, const TStr &Style=TStr())
Definition: gnuplot.cpp:373
int AddErrBar(const TFltTrV &XYDValV, const TStr &Label=TStr())
Definition: gnuplot.cpp:261
char * CStr()
Definition: dt.h:476
TStr GetSeriesPlotStr(const int &PlotN)
Definition: gnuplot.cpp:124
void RunGnuPlot() const
Definition: gnuplot.cpp:869
TSizeTy Add()
Adds a new element at the end of the vector, after its current last element.
Definition: ds.h:559
static TStr GnuPlotFNm
GnuPlot executable file name. Set if different than the standard wgnuplot/gnuplot.
Definition: gnuplot.h:21
TStr PlotFNm
Definition: gnuplot.h:50
void DelLast()
Removes the last element of the vector.
Definition: ds.h:609
TGpScaleTy
Definition: gnuplot.h:6
static int Tics42
Definition: gnuplot.h:48
void CreatePlotFile(const TStr &Comment=TStr())
Definition: gnuplot.cpp:764
TGpSeriesTy
Definition: gnuplot.h:11
int AddFunc(const TStr &FuncStr, const TGpSeriesTy &SeriesTy=gpwLinesPoints, const TStr &Label=TStr(), const TStr &Style=TStr())
Definition: gnuplot.cpp:151
TStrV MoreCmds
Definition: gnuplot.h:56