SNAP Library 2.3, User Reference  2014-06-16 11:58:46
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  AddCmd(TStr::Fmt("set terminal png size %d,%d", SizeX, SizeY));
583  AddCmd(TStr::Fmt("set output '%s'", FNm.CStr()));
584  //#else // EPS
585  //AddCmd("set terminal postscript eps 10 enhanced color");
586  //AddCmd(TStr::Fmt("set output '%s%s.eps'", FNm.GetFPath().CStr(), FNm.GetFMid().CStr()));
587  //#endif
588  } else {
589  AddCmd(Terminal);
590  AddCmd(TStr::Fmt("set output '%s'", FNm.CStr()));
591  }
592  Pause(false);
593  CreatePlotFile(Comment.Empty()? Title : Comment);
594  RunGnuPlot();
595  MoreCmds.DelLast();
596  MoreCmds.DelLast();
597 }
598 
599 void TGnuPlot::SaveEps(const TStr& FNm, const int& FontSz, const TStr& Comment) {
600  AddCmd(TStr::Fmt("set terminal postscript enhanced eps %d color", FontSz));
601  AddCmd(TStr::Fmt("set output '%s'", FNm.CStr()));
602  Pause(false);
603  CreatePlotFile(Comment.Empty()? Title : Comment);
604  RunGnuPlot();
605  MoreCmds.DelLast();
606  MoreCmds.DelLast();
607 }
608 
609 void TGnuPlot::MakeExpBins(const TFltPrV& XYValV, TFltPrV& ExpXYValV, const double& BinFactor, const double& MinYVal) {
610  TFltKdV KdV(XYValV.Len(), 0), OutV;
611  for (int i = 0; i < XYValV.Len(); i++) {
612  KdV.Add(TFltKd(XYValV[i].Val1, XYValV[i].Val2)); }
613  KdV.Sort();
614  TGnuPlot::MakeExpBins(KdV, OutV, BinFactor, MinYVal);
615  ExpXYValV.Gen(OutV.Len(), 0);
616  for (int i = 0; i < OutV.Len(); i++) {
617  ExpXYValV.Add(TFltPr(OutV[i].Key, OutV[i].Dat)); }
618 }
619 
620 void TGnuPlot::MakeExpBins(const TFltKdV& XYValV, TFltKdV& ExpXYValV, const double& BinFactor, const double& MinYVal) {
621  if (XYValV.Empty()) { ExpXYValV.Clr(false); return; }
622  IAssert(! XYValV.Empty());
623  IAssert(XYValV.IsSorted());
624  const TFlt MxX = XYValV.Last().Key;
625  // find buckets
626  TFltV BucketEndV; BucketEndV.Add(1);
627  double PrevBPos = 1, BPos = 1;
628  while (BPos <= MxX) {
629  PrevBPos = (uint) floor(BPos);
630  BPos *= BinFactor;
631  if (floor(BPos) == PrevBPos) {
632  BPos = PrevBPos + 1; }
633  BucketEndV.Add(floor(BPos));
634  }
635  //printf("buckets:\n"); for (int i = 0; i < BucketEndV.Len(); i++) { printf("\t%g\n", BucketEndV[i]);}
636  ExpXYValV.Gen(BucketEndV.Len(), 0);
637  int CurB = 0;
638  double AvgPos=0, Cnt=0, AvgVal=0;
639  for (int v = 0; v < XYValV.Len(); v++) {
640  if (XYValV[v].Key() == 0.0) { continue; }
641  AvgPos += XYValV[v].Key ;//* XYValV[v].Dat; // x
642  AvgVal += XYValV[v].Dat; // y
643  Cnt++;
644  if (v+1 == XYValV.Len() || XYValV[v+1].Key > BucketEndV[CurB]) {
645  if (Cnt != 0) {
646  //AvgPos /= AvgVal;
647  //AvgVal /= (BucketEndV[CurB]-BucketEndV[CurB-1]);
648  AvgPos /= (double) Cnt;
649  AvgVal /= (double) Cnt;
650  if (AvgVal < MinYVal) { AvgVal = MinYVal; }
651  ExpXYValV.Add(TFltKd(AvgPos, AvgVal));
652  //printf("b: %6.2f\t%6.2f\n", AvgPos, AvgVal);
653  AvgPos = 0; AvgVal = 0; Cnt = 0;
654  }
655  CurB++;
656  }
657  }
658 }
659 
660 void TGnuPlot::LoadTs(const TStr& FNm, TStrV& ColNmV, TVec<TFltKdV>& ColV) {
661  PSs Ss = TSs::LoadTxt(ssfTabSep, FNm);
662  int row = 0;
663  ColNmV.Clr();
664  while (Ss->At(0, row)[0] == '#') { row++; }
665  for (int c = 1; c < Ss->GetXLen(row); c+=2) {
666  ColNmV.Add(Ss->At(c, row));
667  }
668  row++;
669  ColV.Gen(ColNmV.Len(), ColNmV.Len());
670  for (; row < Ss->GetYLen(); row++) {
671  for (int c = 0; c < Ss->GetXLen(row); c+=2) {
672  if (Ss->At(c,row).Empty()) break;
673  ColV[c/2].Add(TFltKd(Ss->At(c,row).GetFlt(), Ss->At(c+1,row).GetFlt()));
674  }
675  }
676 }
677 
679  switch(ScaleTy){
680  case gpsNoAuto: return TStr("set noautoscale");
681  case gpsAuto: return TStr("set autoscale");
682  case gpsLog: return TStr("set logscale");
683  case gpsLog2X: return TStr("set logscale x 2");
684  case gpsLog2Y: return TStr("set logscale y 2");
685  case gpsLog2XY: return TStr("set logscale xy 2");
686  case gpsLog10X: return TStr("set logscale x 10");
687  case gpsLog10Y: return TStr("set logscale y 10");
688  case gpsLog10XY: return TStr("set logscale xy 10");
689  default: Fail;
690  }
691  return TStr();
692 }
693 
695  switch(SeriesTy) {
696  case gpwLines: return TStr("lines");
697  case gpwPoints: return TStr("points");
698  case gpwLinesPoints: return TStr("linespoints");
699  case gpwImpulses: return TStr("impulses");
700  case gpwDots: return TStr("dots");
701  case gpwSteps: return TStr("steps");
702  case gpwFSteps: return TStr("fsteps");
703  case gpwHiSteps: return TStr("histeps");
704  case gpwBoxes: return TStr("boxes");
705  case gpwErrBars: return TStr("errorbars");
706  case gpwFilledCurves: return TStr("filledcurves");
707  default: Fail;
708  }
709  return TStr();
710 }
711 
712 void TGnuPlot::SaveTs(const TIntKdV& KdV, const TStr& FNm, const TStr& HeadLn) {
713  FILE *F = fopen(FNm.CStr(), "wt");
714  EAssert(F);
715  if (! HeadLn.Empty()) fprintf(F, "# %s\n", HeadLn.CStr());
716  for (int i = 0; i < KdV.Len(); i++) {
717  fprintf(F, "%d\t%d\n", KdV[i].Key(), KdV[i].Dat()); }
718  fclose(F);
719 }
720 
721 
722 void TGnuPlot::SaveTs(const TIntFltKdV& KdV, const TStr& FNm, const TStr& HeadLn) {
723  FILE *F = fopen(FNm.CStr(), "wt");
724  EAssert(F);
725  if (! HeadLn.Empty()) fprintf(F, "# %s\n", HeadLn.CStr());
726  for (int i = 0; i < KdV.Len(); i++)
727  fprintf(F, "%d\t%g\n", KdV[i].Key(), KdV[i].Dat());
728  fclose(F);
729 }
730 
732  TFltV DeltaY;
733  TFltPrV ValV1, ValV2, ValV3;
734  for (int i = 1; i < 30; i++) {
735  ValV1.Add(TFltPr(i, pow(double(i), 1.2)));
736  DeltaY.Add(5*TInt::Rnd.GetUniDev());
737  ValV2.Add(TFltPr(i, 5*i-1));
738  }
739  for (int i = -10; i < 20; i++) {
740  ValV3.Add(TFltPr(i, 2*i + 2 + TInt::Rnd.GetUniDev()));
741  }
742  TGnuPlot GnuPlot("testDat", "TestPlot", true);
743  GnuPlot.SetXYLabel("X", "Y");
744  const int id2 = GnuPlot.AddPlot(ValV2, gpwPoints, "y=5*x-1");
745  const int id3 = GnuPlot.AddPlot(ValV3, gpwPoints, "y=2*x+2");
746  GnuPlot.AddErrBar(ValV1, DeltaY, "y=x^2", "Error bar");
747  GnuPlot.AddLinFit(id2, gpwLines);
748  GnuPlot.AddLinFit(id3, gpwLines);
749  GnuPlot.Plot();
750  GnuPlot.SavePng("testPlot.png");
751 }
752 
753 int TGnuPlot::IsSameXCol(const int& CurId, const int& PrevId) const {
754  //if (SerId < 1) { return -1; }
755  if (SeriesV[CurId].XYValV.Len() != SeriesV[PrevId].XYValV.Len()) { return -1; }
756  for (int x = 0; x < SeriesV[CurId].XYValV.Len(); x++) {
757  if (SeriesV[CurId].XYValV[x] != SeriesV[PrevId].XYValV[x]) { return -1; }
758  }
759  IAssert(SeriesV[PrevId].XCol > 0);
760  return SeriesV[PrevId].XCol;
761 }
762 
763 void TGnuPlot::CreatePlotFile(const TStr& Comment) {
764  time_t ltime; time(&ltime);
765  char* TimeStr = ctime(&ltime); TimeStr[strlen(TimeStr) - 1] = 0;
766  // rearrange columns so that longest are on the left
767  //SeriesV.Sort(false);
768  TIntV SerIdV(SeriesV.Len(), 0);
769  for (int i = 0; i < SeriesV.Len(); i++) { SerIdV.Add(i); }
770  SerIdV.SortCmp(TGpSeriesCmp(SeriesV));
771  // set columns
772  int ColCnt = 1;
773  bool SaveData = false;
774  for (int s = 0; s < SeriesV.Len(); s++) {
775  TGpSeries& Plt = SeriesV[SerIdV[s]];
776  if (Plt.XYValV.Empty()) { continue; }
777  Plt.DataFNm = DataFNm;
778  // plots use same X column
779  const int PrevCol = s > 0 ? IsSameXCol(SerIdV[s], SerIdV[s-1]) : -1;
780  if (PrevCol != -1) { Plt.XCol = PrevCol; }
781  else { Plt.XCol = ColCnt; ColCnt++; }
782  Plt.YCol = ColCnt; ColCnt++;
783  if (! Plt.ZValV.Empty()) { Plt.ZCol = ColCnt; ColCnt++; }
784  if (! Plt.XYValV.Empty()) { SaveData=true; }
785  }
786  // save data file (skip duplicate X columns)
787  if (SaveData) {
788  FILE *F = fopen(DataFNm.CStr(), "wt");
789  EAssertR(F != NULL, TStr("Can not open data file ")+DataFNm);
790  fprintf(F, "#\n");
791  fprintf(F, "# %s (%s)\n", Comment.CStr(), TimeStr);
792  fprintf(F, "#\n");
793  // column names
794  for (int i = 0; i < SerIdV.Len(); i++) {
795  const TGpSeries& Ser = SeriesV[SerIdV[i]];
796  if (Ser.XYValV.Empty()) { continue; }
797  if (i == 0) { fprintf(F, "# "); } else { fprintf(F, "\t"); }
798  if (Ser.SaveXVals()) {
799  if (! LblX.Empty()) { fprintf(F, "%s\t", LblX.CStr()); }
800  else { fprintf(F, "XVals\t"); }
801  }
802  if (Ser.Label.Empty()) { fprintf(F, "%s", LblY.CStr()); }
803  else { fprintf(F, "%s", SeriesV[SerIdV[i]].Label.CStr()); }
804  if (Ser.ZCol > 0) fprintf(F, "\tDeltaY");
805  }
806  fprintf(F, "\n");
807  // data
808  for (int row = 0; row < SeriesV[SerIdV[0]].XYValV.Len(); row++) {
809  for (int i = 0; i < SeriesV.Len(); i++) {
810  const TGpSeries& Ser = SeriesV[SerIdV[i]];
811  if (row < Ser.XYValV.Len()) {
812  if (i > 0) { fprintf(F, "\t"); }
813  if (Ser.SaveXVals()) { fprintf(F, "%g\t%g", Ser.XYValV[row].Key(), Ser.XYValV[row].Dat()); }
814  else { fprintf(F, "%g", Ser.XYValV[row].Dat()); }
815  if (! Ser.ZValV.Empty()) { fprintf(F, "\t%g", Ser.ZValV[row]()); }
816  }
817  }
818  fprintf(F, "\n");
819  }
820  fclose(F);
821  }
822  // save plot file
823  FILE *F = fopen(PlotFNm.CStr(), "wt");
824  EAssertR(F != 0, TStr("Can not open plot file ")+PlotFNm);
825  TStr CurDir = TDir::GetCurDir();
826  CurDir.ChangeStrAll("\\", "\\\\");
827  fprintf(F, "#\n");
828  fprintf(F, "# %s (%s)\n", Comment.CStr(), TimeStr);
829  fprintf(F, "#\n\n");
830  if (! Title.Empty()) fprintf(F, "set title \"%s\"\n", Title.CStr());
831  fprintf(F, "set key bottom right\n");
832  fprintf(F, "%s\n", GetScaleStr(ScaleTy).CStr());
834  fprintf(F, "set format x \"10^{%%L}\"\n");
835  fprintf(F, "set mxtics 10\n"); }
837  fprintf(F, "set format y \"10^{%%L}\"\n");
838  fprintf(F, "set mytics 10\n"); }
839  if (ScaleTy==gpsLog2X || ScaleTy==gpsLog2XY) { fprintf(F, "set format x \"2^{%%L}\"\n"); }
840  if (ScaleTy==gpsLog2Y || ScaleTy==gpsLog2XY) { fprintf(F, "set format y \"2^{%%L}\"\n"); }
841  if (SetGrid) fprintf(F, "set grid\n");
842  if (XRange.Val1 != XRange.Val2) fprintf(F, "set xrange [%g:%g]\n", XRange.Val1(), XRange.Val2());
843  if (YRange.Val1 != YRange.Val2) fprintf(F, "set yrange [%g:%g]\n", YRange.Val1(), YRange.Val2());
844  if (! LblX.Empty()) fprintf(F, "set xlabel \"%s\"\n", LblX.CStr());
845  if (! LblY.Empty()) fprintf(F, "set ylabel \"%s\"\n", LblY.CStr());
846  if (Tics42 < -1) {
847  Tics42 = GetTics42();
848  }
849  if (Tics42) {
850  fprintf(F, "set tics scale 2\n"); // New in version 4.2
851  } else {
852  fprintf(F, "set ticscale 2 1\n"); // Old (deprecated)
853  }
854  // custom commands
855  for (int i = 0; i < MoreCmds.Len(); i++) {
856  fprintf(F, "%s\n", MoreCmds[i].CStr()); }
857  // plot
858  if (! SeriesV.Empty()) {
859  fprintf(F, "plot \t");
860  for (int i = 0; i < SeriesV.Len(); i++) {
861  fprintf(F, "%s", GetSeriesPlotStr(i).CStr()); }
862  fprintf(F, "\n");
863  }
864  if (SetPause) fprintf(F, "pause -1 \"Hit return to exit. %s\"\n", PlotFNm.CStr());
865  fclose(F);
866 }
867 
868 void TGnuPlot::RunGnuPlot() const {
869  // try running gnuplot
870  if (system(TStr::Fmt("%s %s", GnuPlotFNm.CStr(), PlotFNm.CStr()).CStr())==0) { return; }
871  if (! GnuPlotPath.Empty()) {
872  #if defined(GLib_WIN)
873  if (system(TStr::Fmt("%s\\%s %s", GnuPlotPath.CStr(), GnuPlotFNm.CStr(), PlotFNm.CStr()).CStr())==0) { return; }
874  #else
875  if (system(TStr::Fmt("%s/%s %s", GnuPlotPath.CStr(), GnuPlotFNm.CStr(), PlotFNm.CStr()).CStr())==0) { return; }
876  #endif
877  }
878  //Old
879  //#if defined(GLib_WIN)
880  //if (system(TStr::Fmt(".\\%s %s", GpFNm.CStr(), PlotFNm.CStr()).CStr())==0) { return; }
881  //#else
882  //if (system(TStr::Fmt("./%s %s", GpFNm.CStr(), PlotFNm.CStr()).CStr())==0) { return; }
883  //#endif
884  //if (system(TStr::Fmt("%s%s %s", GpPath.CStr(), GpFNm.CStr(), PlotFNm.CStr()).CStr())==0) { return; }
885  //FailR(TStr::Fmt("Cannot find GnuPlot (%s) for plot %s. Set the PATH.", GpFNm.CStr(), PlotFNm.CStr()).CStr());
886  //ErrNotify(TStr::Fmt("Cannot find GnuPlot (%s) for plot %s. Set the PATH.", GpFNm.CStr(), PlotFNm.CStr()).CStr());
887  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());
888 }
889 
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:1104
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:609
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:1050
static const double Mx
Definition: dt.h:1294
int AddPwrFit(const int &PlotId, const TGpSeriesTy &SeriesTy=gpwLines, const TStr &Style=TStr())
Definition: gnuplot.cpp:366
Definition: dt.h:1289
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:660
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:712
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:694
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:753
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:731
static TStr GetScaleStr(const TGpScaleTy &ScaleTy)
Definition: gnuplot.cpp:678
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:868
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:763
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