Gnuplot.cc
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
#include "Gnuplot.h"
00046
#include <plearn/math/TMat_maths.h>
00047
00048
00049
00050
#ifdef WIN32
00051
#include <io.h>
00052
00053
00054
#define chmod _chmod
00055
#define fileno _fileno
00056
#define popen _popen
00057
#define pclose _pclose
00058
#endif
00059
00060
namespace PLearn {
00061
using namespace std;
00062
00063 Gnuplot::Gnuplot(
int nb_max_plot)
00064 : tmpfilenames(nb_max_plot,"/tmp/","gp"),
00065 gp_cstream(popen("gnuplot","w"))
00066 {
00067
tognuplot.
attach(fileno(
gp_cstream));
00068
tognuplot.
outmode=PStream::raw_ascii;
00069
tognuplot <<
"set data style lines" <<
endl;
00070 }
00071
00072 Gnuplot::Gnuplot(
const Vec& v1,
const string& opt1)
00073 : tmpfilenames(5,"/tmp/","gp"),
00074 gp_cstream(popen("gnuplot","w"))
00075 {
00076
tognuplot.
attach(fileno(
gp_cstream));
00077
tognuplot.
outmode=PStream::raw_ascii;
00078
tognuplot <<
"set data style lines" <<
endl;
00079
plot(v1,opt1);
00080 }
00081
00082 Gnuplot::Gnuplot(
const Vec& v1,
const string& opt1,
const Vec& v2,
const string& opt2)
00083 : tmpfilenames(5,"/tmp/","gp"),
00084 gp_cstream(popen("gnuplot","w"))
00085 {
00086
tognuplot.
attach(fileno(
gp_cstream));
00087
tognuplot.
outmode=PStream::raw_ascii;
00088
tognuplot <<
"set data style lines" <<
endl;
00089
plot(v1,opt1,v2,opt2);
00090 }
00091
00092 Gnuplot::Gnuplot(
const Vec& v1,
const string& opt1,
const Vec& v2,
const string& opt2,
const Vec& v3,
const string& opt3)
00093 : tmpfilenames(5,"/tmp/","gp"),
00094 gp_cstream(popen("gnuplot","w"))
00095 {
00096
tognuplot.
attach(fileno(
gp_cstream));
00097
tognuplot.
outmode=PStream::raw_ascii;
00098
tognuplot <<
"set data style lines" <<
endl;
00099
plot(v1,opt1,v2,opt2,v3,opt3);
00100 }
00101
00102
00103 Gnuplot::~Gnuplot()
00104 {
00105
tognuplot <<
"\nquit" <<
endl;
00106 pclose(
gp_cstream);
00107 }
00108
00109 PStream& Gnuplot::operator<<(
const string& str)
00110 {
00111
tognuplot << str;
00112
return tognuplot;
00113 }
00114
00115 void Gnuplot::flush()
00116 {
tognuplot.
flush(); }
00117
00118 void Gnuplot::setxrange(
real xmin,
real xmax)
00119 {
tognuplot <<
"set xrange [" << xmin <<
":" << xmax <<
"]" <<
endl; }
00120
00121 void Gnuplot::setyrange(
real ymin,
real ymax)
00122 {
tognuplot <<
"set yrange [" << ymin <<
":" << ymax <<
"]" <<
endl; }
00123
00124 void Gnuplot::setrange(
real xmin,
real xmax,
real ymin,
real ymax)
00125 {
00126
setxrange(xmin,xmax);
00127
setyrange(ymin,ymax);
00128 }
00129
00130 void Gnuplot::seteps(
const string &filename)
00131 {
00132
tognuplot <<
"set term post color" <<
endl;
00133
tognuplot <<
"set output \"" << filename <<
"\"" <<
endl;
00134 }
00135
00136 void Gnuplot::plot(
const Vec& v1,
const string& opt1)
00137 {
00138
saveGnuplot(
tmpfilenames[0].
c_str(), v1);
00139 chmod(
tmpfilenames[0].
c_str(),0777);
00140
tognuplot <<
"plot '" <<
tmpfilenames[0] <<
"' " << opt1 <<
endl;
00141 }
00142
00143 void Gnuplot::plot(
const Vec& v1,
const string& opt1,
const Vec& v2,
const string& opt2)
00144 {
00145
saveGnuplot(
tmpfilenames[0].
c_str(), v1);
00146 chmod(
tmpfilenames[0].
c_str(),0777);
00147
saveGnuplot(
tmpfilenames[1].
c_str(), v2);
00148 chmod(
tmpfilenames[1].
c_str(),0777);
00149
tognuplot <<
"plot '" <<
tmpfilenames[0] <<
"' " << opt1 <<
", '" << tmpfilenames[1] <<
"' " << opt2 <<
endl;
00150 }
00151
00152 void Gnuplot::plot(
const Vec& v1,
const string& opt1,
const Vec& v2,
const string& opt2,
const Vec& v3,
const string& opt3)
00153 {
00154
saveGnuplot(
tmpfilenames[0].
c_str(), v1);
00155 chmod(
tmpfilenames[0].
c_str(),0777);
00156
saveGnuplot(
tmpfilenames[1].
c_str(), v2);
00157 chmod(
tmpfilenames[1].
c_str(),0777);
00158
saveGnuplot(
tmpfilenames[2].
c_str(), v3);
00159 chmod(
tmpfilenames[2].
c_str(),0777);
00160
tognuplot <<
"plot '" <<
tmpfilenames[0] <<
"' " << opt1
00161 <<
", '" << tmpfilenames[1] <<
"' " << opt2
00162 <<
", '" << tmpfilenames[2] <<
"' " << opt3 <<
endl;
00163 }
00164
00165 void Gnuplot::plot(
const Vec& v1,
const string& opt1,
const Vec& v2,
const string& opt2,
const Vec& v3,
const string& opt3,
const Vec& v4,
const string& opt4)
00166 {
00167
saveGnuplot(
tmpfilenames[0].
c_str(), v1);
00168 chmod(
tmpfilenames[0].
c_str(),0777);
00169
saveGnuplot(
tmpfilenames[1].
c_str(), v2);
00170 chmod(
tmpfilenames[1].
c_str(),0777);
00171
saveGnuplot(
tmpfilenames[2].
c_str(), v3);
00172 chmod(
tmpfilenames[2].
c_str(),0777);
00173
saveGnuplot(
tmpfilenames[3].
c_str(), v4);
00174 chmod(
tmpfilenames[3].
c_str(),0777);
00175
tognuplot <<
"plot '" <<
tmpfilenames[0] <<
"' " << opt1
00176 <<
", '" << tmpfilenames[1] <<
"' " << opt2
00177 <<
", '" << tmpfilenames[2] <<
"' " << opt3
00178 <<
", '" << tmpfilenames[3] <<
"' " << opt4 <<
endl;
00179 }
00180
00181 void Gnuplot::plot(
const Mat& m1,
const string& opt1)
00182 {
00183
saveGnuplot(
tmpfilenames[0].
c_str(), m1);
00184
tognuplot <<
"plot '"<<
tmpfilenames[0] <<
"' " << opt1 <<
endl;
00185 }
00186
00187
00188 void Gnuplot::plot(
const Mat& m1,
const string& opt1,
const Mat& m2,
const string& opt2,
const Mat& m3,
const string& opt3)
00189 {
00190
saveGnuplot(
tmpfilenames[0].
c_str(), m1);
00191
saveGnuplot(
tmpfilenames[1].
c_str(), m2);
00192
saveGnuplot(
tmpfilenames[2].
c_str(), m3);
00193
string command =
"plot '" +
tmpfilenames[0] +
"' " + opt1 +
", " +
00194
"'" + tmpfilenames[1] +
"' " + opt2 +
", " +
00195
"'" + tmpfilenames[2] +
"' " + opt3;
00196
00197
tognuplot << command <<
endl;
00198 }
00199
00200 void Gnuplot::plot(
const Mat& m1,
const string& opt1,
const Mat& m2,
const string& opt2)
00201 {
00202
saveGnuplot(
tmpfilenames[0].
c_str(), m1);
00203
saveGnuplot(
tmpfilenames[1].
c_str(), m2);
00204
tognuplot <<
"plot '" <<
tmpfilenames[0] <<
"' " << opt1 <<
", ";
00205
tognuplot <<
"'" << tmpfilenames[1] <<
"' " << opt2 <<
endl;
00206 }
00207
00208 void Gnuplot::plot(
const Mat& m1,
const string& opt1,
const Mat& m2,
const string& opt2,
const Mat& m3,
const string& opt3,
const Mat& m4,
const string& opt4)
00209 {
00210
saveGnuplot(
tmpfilenames[0].
c_str(), m1);
00211
saveGnuplot(
tmpfilenames[1].
c_str(), m2);
00212
saveGnuplot(
tmpfilenames[2].
c_str(), m3);
00213
saveGnuplot(
tmpfilenames[3].
c_str(), m4);
00214
tognuplot <<
"plot '" <<
tmpfilenames[0] <<
"' " << opt1 <<
", ";
00215
tognuplot <<
"'" << tmpfilenames[1] <<
"' " << opt2 <<
", ";
00216
tognuplot <<
"'" << tmpfilenames[2] <<
"' " << opt3 <<
", ";
00217
tognuplot <<
"'" << tmpfilenames[3] <<
"' " << opt4 <<
endl;
00218 }
00219
00220 void Gnuplot::plot(
const Mat& m1,
const string& opt1,
const Mat& m2,
const string& opt2,
const Mat& m3,
00221
const string& opt3,
const Mat& m4,
const string& opt4,
const Mat& m5,
const string& opt5)
00222 {
00223
saveGnuplot(
tmpfilenames[0].
c_str(), m1);
00224
saveGnuplot(
tmpfilenames[1].
c_str(), m2);
00225
saveGnuplot(
tmpfilenames[2].
c_str(), m3);
00226
saveGnuplot(
tmpfilenames[3].
c_str(), m4);
00227
saveGnuplot(
tmpfilenames[4].
c_str(), m5);
00228
tognuplot <<
"plot '" <<
tmpfilenames[0] <<
"' " << opt1 <<
", ";
00229
tognuplot <<
"'" << tmpfilenames[1] <<
"' " << opt2 <<
", ";
00230
tognuplot <<
"'" << tmpfilenames[2] <<
"' " << opt3 <<
", ";
00231
tognuplot <<
"'" << tmpfilenames[3] <<
"' " << opt4 <<
", ";
00232
tognuplot <<
"'" << tmpfilenames[4] <<
"' " << opt5 <<
endl;
00233 }
00234
00235 void Gnuplot::plot(
const Mat& m1,
const string& opt1,
const Mat& m2,
const string& opt2,
const Mat& m3,
00236
const string& opt3,
const Mat& m4,
const string& opt4,
const Mat& m5,
const string& opt5,
00237
const Mat& m6,
const string& opt6)
00238 {
00239
saveGnuplot(
tmpfilenames[0].
c_str(), m1);
00240
saveGnuplot(
tmpfilenames[1].
c_str(), m2);
00241
saveGnuplot(
tmpfilenames[2].
c_str(), m3);
00242
saveGnuplot(
tmpfilenames[3].
c_str(), m4);
00243
saveGnuplot(
tmpfilenames[4].
c_str(), m5);
00244
saveGnuplot(
tmpfilenames[5].
c_str(), m6);
00245
tognuplot <<
"plot '" <<
tmpfilenames[0] <<
"' " << opt1 <<
", ";
00246
tognuplot <<
"'" << tmpfilenames[1] <<
"' " << opt2 <<
", ";
00247
tognuplot <<
"'" << tmpfilenames[2] <<
"' " << opt3 <<
", ";
00248
tognuplot <<
"'" << tmpfilenames[3] <<
"' " << opt4 <<
", ";
00249
tognuplot <<
"'" << tmpfilenames[4] <<
"' " << opt5 <<
", ";
00250
tognuplot <<
"'" << tmpfilenames[5] <<
"' " << opt6 <<
endl;
00251 }
00252
00253 void Gnuplot::plotClasses(
const Mat& m)
00254 {
00255
Mat s = m.
copy();
00256
sortRows(s, 2);
00257
string fname =
tmpfilenames[0];
00258
int l = s.
length();
00259
string command =
"plot ";
00260 ofstream out(
fname.c_str());
00261
if(!out)
00262
PLERROR(
"Could not open file %s for writing ",
fname.c_str());
00263
00264
real oldc = FLT_MAX;
00265
int index = 0;
00266
for(
int i=0; i<l; i++)
00267 {
00268
real x = s(i,0);
00269
real y = s(i,1);
00270
real c = s(i,2);
00271
if(c!=oldc)
00272 {
00273
if(i>0)
00274 {
00275 out <<
"\n\n";
00276 command +=
", ";
00277 }
00278 command +=
"'" +
fname +
"' index " +
tostring(index) +
" title '" +
tostring(c) +
"' with points";
00279 ++index;
00280 }
00281 out <<
x <<
" " << y <<
" " << c <<
"\n";
00282 oldc = c;
00283 }
00284 out.close();
00285
00286
00287
tognuplot << command <<
endl;
00288 }
00289
00290 void Gnuplot::multiplot(
vector<Mat *> &ms,
vector<string> &opts)
00291 {
00292
tognuplot <<
"plot ";
00293
for (
unsigned int i = 0; i < ms.size(); ++i) {
00294
saveGnuplot(
tmpfilenames[i].
c_str(), *(ms[i]));
00295
if (i)
tognuplot <<
", ";
00296
tognuplot <<
"'" <<
tmpfilenames[i] <<
"' " << opts[i];
00297 }
00298
tognuplot <<
endl;
00299 }
00300
00301 void Gnuplot::plot3d(
const Mat &m1,
const string &opt1)
00302 {
00303
00304
tognuplot <<
"set dgrid3d" <<
endl;
00305
00306
saveGnuplot(
tmpfilenames[0].
c_str(), m1);
00307
tognuplot <<
"splot '" <<
tmpfilenames[0] <<
"' " << opt1 <<
endl;
00308 }
00309
00310 void Gnuplot::histoplot(
Vec feature,
real minval,
real maxval,
int
00311 nbins,
bool do_normalize,
char* title)
00312 {
00313 ofstream out(
tmpfilenames[0].
c_str());
00314
Vec histo =
histogram(feature, minval, maxval, nbins);
00315
if(do_normalize)
00316
normalize(histo,
real(1.0));
00317
real binwidth = (maxval-minval)/nbins;
00318
00319
for(
int i=0; i<nbins; i++)
00320 out << minval + (0.5+i)*binwidth + 0.5 <<
" " << histo[i] <<
" " << binwidth <<
endl;
00321
00322
00323
tognuplot <<
"plot '" <<
tmpfilenames[0] <<
"' title '" << title <<
"' with boxes" <<
endl;
00324 }
00325
00326 void Gnuplot::histoplot(
Vec feature,
Vec classnums,
real minval,
00327
real maxval,
int nbins,
bool do_normalize)
00328 {
00329 ofstream out(
tmpfilenames[0].
c_str());
00330
int nclasses = (
int)
max(classnums) + 1;
00331
real binwidth = (maxval-minval)/nbins;
00332
real deltaval = maxval-minval+ 1e-6;
00333
00334
Mat histo(nclasses, nbins);
00335
for(
int i=0; i<feature.
length(); i++)
00336 {
00337
real val = feature[i];
00338
int binpos =
int((
val-minval)/deltaval*nbins);
00339
if(binpos>=0 && binpos<nbins)
00340 histo(
int(classnums[i]),binpos)++;
00341 }
00342
00343
if(do_normalize)
00344
normalize(histo,
real(1.0));
00345
00346
for(
int c=0; c<nclasses; c++)
00347 {
00348
for(
int i=0; i<nbins; i++)
00349 out << minval+(0.5+i)*binwidth <<
" " << histo(c,i) <<
" " << binwidth
00350 <<
endl;
00351 out <<
endl;
00352 }
00353
00354
tognuplot <<
"plot '" <<
tmpfilenames[0] <<
"' every PLearn:::0::0 title 'Class 0' with boxes";
00355
for(
int c=1; c<nclasses; c++)
00356
tognuplot <<
", '" << tmpfilenames[0] <<
"' every PLearn:::" << c <<
"::" << c
00357 <<
" title 'Class " << c <<
"' with boxes";
00358
tognuplot <<
endl;
00359 }
00360
00361 void Gnuplot::featureplot(
Mat inputs,
Vec classnums,
char* withwhat)
00362 {
00363 ofstream out(
tmpfilenames[0].
c_str());
00364
00365
int nclasses = (
int)
max(classnums) + 1;
00366
00367
for(
int c=0; c<nclasses; c++)
00368 {
00369
for(
int i=0; i<inputs.
length(); i++)
00370
if((
int)classnums[i] == c)
00371
for(
int j=0; j<inputs.
width(); j++)
00372 out << j <<
' ' << inputs(i,j) <<
endl;
00373 out <<
endl;
00374 }
00375
00376
tognuplot <<
"plot '" <<
tmpfilenames[0] <<
"' every PLearn:::0::0 title 'Class 0' with " << withwhat;
00377
for(
int c=1; c<nclasses; c++)
00378
tognuplot <<
", '" << tmpfilenames[0] <<
"' every PLearn:::" << c <<
"::" << c
00379 <<
" title 'Class " << c <<
"' with " << withwhat;
00380
tognuplot <<
endl;
00381 }
00382
00383 void Gnuplot::plotcdf(
Vec feature,
const string& title)
00384 {
00385
Vec v = feature.
copy();
00386
sortElements(v);
00387 ofstream out(
tmpfilenames[0].
c_str());
00388
00389
for(
int i=0; i<v.
length(); i++)
00390 out << v[i] <<
' ' <<
real(i+1)/v.
length() <<
'\n';
00391 out.close();
00392
tognuplot <<
"plot '" <<
tmpfilenames[0] <<
"' using 1:2 title '" << title <<
"' with impulses" <<
endl;
00393 }
00394
00395 void Gnuplot::plotcdf(
const Array<Vec>& vecarray,
const Array<string>& titlearray)
00396 {
00397
tognuplot <<
"plot ";
00398
for(
int c=0; c<vecarray.
size(); c++)
00399 {
00400
Vec v = vecarray[c].
copy();
00401
sortElements(v);
00402 ofstream out(
tmpfilenames[c].
c_str());
00403
for(
int i=0; i<v.
length(); i++)
00404 out << v[i] <<
' ' <<
real(i+1)/v.
length() <<
'\n';
00405 out.close();
00406
tognuplot <<
"'" <<
tmpfilenames[c] <<
"' using 1:2 title '" << titlearray[c] <<
"'";
00407
if(c==vecarray.
size()-1)
00408
tognuplot <<
endl;
00409
else
00410
tognuplot <<
", ";
00411 }
00412 }
00413
00414 void Gnuplot::plotdensity(
Vec feature,
const string& title,
int halfwindowsize,
string range)
00415 {
00416
Vec v = feature.
copy();
00417
sortElements(v);
00418 ofstream out(
tmpfilenames[0].
c_str());
00419
for (
int i=1; i<v.
length()-1; i++)
00420 {
00421
real x = v[i];
00422
int lowi = std::max(i-halfwindowsize,1);
00423
int highi = std::min(i+halfwindowsize,v.length()-2);
00424
real lowx = 0.5*(v[lowi-1]+v[lowi]);
00425
real highx = 0.5*(v[highi+1]+v[highi]);
00426
if (highx == lowx)
PLERROR(
"In Gnuplot::plotdensity(...), density at this point is inf, use larger windowsize");
00427
real density = (highi-lowi+1)/(highx-lowx);
00428 out <<
x <<
' ' << density <<
"\n";
00429 }
00430 out.close();
00431
tognuplot <<
"plot " << range <<
" '" <<
tmpfilenames[0] <<
"' title '" << title <<
"' with lines" <<
endl;
00432 }
00433
00434 void Gnuplot::plotdensity(
const Array<Vec>& vecarray,
const Array<string>& titlearray,
int halfwindowsize)
00435 {
00436
tognuplot <<
"plot ";
00437
for(
int c=0; c<vecarray.
size(); c++)
00438 {
00439
Vec v = vecarray[c].
copy();
00440
sortElements(v);
00441 ofstream out(
tmpfilenames[c].
c_str());
00442
for (
int i=1; i<v.
length()-1; i++)
00443 {
00444
real x = v[i];
00445
int lowi = std::max(i-halfwindowsize,1);
00446
int highi = std::min(i+halfwindowsize,v.length()-2);
00447
real lowx = 0.5*(v[lowi-1]+v[lowi]);
00448
real highx = 0.5*(v[highi+1]+v[highi]);
00449
real density = (highi-lowi+1)/(highx-lowx);
00450 out <<
x <<
' ' << density <<
"\n";
00451 }
00452 out.close();
00453
tognuplot <<
"'" <<
tmpfilenames[c] <<
"' using 1:2 title '" << titlearray[c] <<
"'";
00454
if(c==vecarray.
size()-1)
00455
tognuplot <<
endl;
00456
else
00457
tognuplot <<
", ";
00458 }
00459 }
00460
00461
00462 void Gnuplot::export_ps(
string psfname,
string psoptions)
00463 {
00464
tognuplot <<
"set terminal postscript " << psoptions <<
"\n"
00465 <<
"set output '" << psfname <<
"'\n"
00466 <<
"replot\n"
00467 <<
"set terminal x11" <<
endl;
00468 }
00469
00470
#ifdef WIN32
00471
#undef _chmod
00472
#undef _fileno
00473
#undef _popen
00474
#undef _pclose
00475
#endif
00476
00477 }
Generated on Tue Aug 17 15:54:30 2004 for PLearn by
1.3.7