93 lines
1.8 KiB
C++
93 lines
1.8 KiB
C++
#ifndef PLOTERRFUNC_H
|
|
#define PLOTERRFUNC_H
|
|
|
|
#include <KLib/math/statistics/Statistics.h>
|
|
|
|
#include <KLib/misc/gnuplot/Gnuplot.h>
|
|
#include <KLib/misc/gnuplot/GnuplotPlot.h>
|
|
#include <KLib/misc/gnuplot/GnuplotPlotElementLines.h>
|
|
|
|
|
|
class PlotErrFunc {
|
|
|
|
|
|
|
|
struct Entry {
|
|
std::string name;
|
|
K::Statistics<float> stats;
|
|
Entry(const std::string& name, const K::Statistics<float>& stats) : name(name), stats(stats) {;}
|
|
};
|
|
|
|
std::vector<Entry> entries;
|
|
|
|
K::Gnuplot gp;
|
|
K::GnuplotPlot gplot;
|
|
|
|
//std::vector<std::string> colors = {"#000000", "#ff0000", "#00bb00", "#0000ff"};
|
|
std::vector<std::string> colors = {"#000000", "#999999", "#0000ff", "#9999ff"};
|
|
|
|
std::string codeFile;
|
|
|
|
public:
|
|
|
|
/** ctor with x-axis label */
|
|
PlotErrFunc(const std::string& xLabel, const std::string& yLabel) {
|
|
gplot.setLabelX(xLabel);
|
|
gplot.setLabelY(yLabel);
|
|
}
|
|
|
|
/** add one curve */
|
|
void add(const std::string name, const K::Statistics<float> stats) {
|
|
entries.push_back(Entry(name, stats));
|
|
}
|
|
|
|
K::Gnuplot& getGP() {
|
|
return gp;
|
|
}
|
|
|
|
void writeCodeTo(const std::string& file) {
|
|
this->codeFile = file;
|
|
}
|
|
|
|
/** plot all curves */
|
|
void plot() {
|
|
|
|
gp << "set grid\n";
|
|
|
|
for (size_t i = 0; i < entries.size(); ++i) {
|
|
|
|
const Entry e = entries[i];
|
|
|
|
K::GnuplotPlotElementLines* line = new K::GnuplotPlotElementLines();
|
|
line->setTitle(e.name);
|
|
line->setColorHex(colors[i]);
|
|
line->setLineWidth(2);
|
|
gplot.add(line);
|
|
|
|
// 0 - 80%
|
|
for (int i = 0; i <= 85; i+= 5) {
|
|
const float q = i / 100.0f;
|
|
const float y = e.stats.getQuantile(q);
|
|
K::GnuplotPoint2 gp(y, q*100);
|
|
line->add(gp);
|
|
}
|
|
|
|
}
|
|
|
|
gp.draw(gplot);
|
|
|
|
if (codeFile != "") {
|
|
std::ofstream out(codeFile);
|
|
out << gp.getBuffer();
|
|
out.close();
|
|
}
|
|
|
|
gp.flush();
|
|
gp.close();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
#endif // PLOTERRFUNC_H
|