1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
| #include <iostream>
#include <memory>
#include <QApplication>
#include <QDialog>
#include "qcustomplot.h"
#include <armadillo>
using namespace std;
void convertToVectorDouble(const arma::vec Avec, std::vector<double> &Aout);
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QDialog window;
QCustomPlot customPlot;
QVBoxLayout *mainLayout = new QVBoxLayout();
QPushButton *buttonQuit = new QPushButton("Quit");
mainLayout->addWidget(buttonQuit);
mainLayout->addWidget(&customPlot);
window.setLayout(mainLayout);
// setup customPlot as central widget of window:
arma::arma_rng::set_seed_random();
// generate data
arma::mat Tvalrand = arma::randn(1000,1);
arma::vec Tsamples(Tvalrand.n_rows);
Tsamples = Tvalrand.col(0);
arma::uvec histVal(50);
arma::vec histValD(50);
std::vector<double> histValVec(50);
std::vector<double> abcissesVec(50);
double xmin = Tsamples.min();
double xmax = Tsamples.max();
double step = (xmax - xmin)/50;
histVal = arma::hist(Tsamples, 50);
histValD = arma::conv_to<arma::vec>::from(histVal);
histValD /= step*1000;
convertToVectorDouble(histValD, histValVec);
int l = 0;
double step_2 = (xmax - xmin)/(50-1);
std::generate(abcissesVec.begin(), abcissesVec.end(), [&] () -> double {return xmin+(l++)*step_2;} );
abcissesVec.back() = xmax;
// create plot (from quadratic plot example):
QVector<double> x(50), y(50);
x = QVector<double>::fromStdVector(abcissesVec);
y = QVector<double>::fromStdVector(histValVec);
// layouts
customPlot.plotLayout()->clear();
QCPLayoutGrid *subLayout = new QCPLayoutGrid;
QCPAxisRect *leftAxis = new QCPAxisRect(&customPlot);
QCPAxisRect *rightAxis = new QCPAxisRect(&customPlot);
customPlot.plotLayout()->addElement(0,0,subLayout);
subLayout->addElement(0, 0, leftAxis);
subLayout->addElement(0, 1, rightAxis);
QCPGraph *leftGraph = customPlot.addGraph(leftAxis->axis(QCPAxis::atBottom),leftAxis->axis(QCPAxis::atLeft));
leftGraph->setData(x, y);
leftGraph->keyAxis()->setLabel("x");
leftGraph->valueAxis()->setLabel("y");
leftGraph->rescaleAxes();
QCPGraph *rightGraph = customPlot.addGraph(rightAxis->axis(QCPAxis::atBottom),rightAxis->axis(QCPAxis::atLeft));
rightGraph->setData(x, y);
rightGraph->keyAxis()->setLabel("x");
rightGraph->valueAxis()->setLabel("y");
rightGraph->rescaleAxes();
/*arma::mat valRandImage = arma::randn<arma::mat>(10*10,1);
arma::vec meanA(10*10);
std::vector<double> im2show(10*10);
meanA = valRandImage.col(0);
/*convertToVectorDouble(meanA, im2show);*/
/*QCPColorMap *colorMap = new QCPColorMap(customPlot.xAxis, customPlot.yAxis);
colorMap->data()->setSize(10, 10);
colorMap->data()->setRange(QCPRange(0, 10), QCPRange(0, 10));
for (int i =0; i < 10; ++i)
{
for (int j = 0; j < 10; ++j)
{
cout << 255*meanA(j+i*10) << endl;
colorMap->data()->setCell(i,j,255*meanA(j+i*10));//(unsigned char) 255*meanA(j+i*10));
}
}
colorMap->setGradient(QCPColorGradient::gpGrayscale);
colorMap->setInterpolate(false);
colorMap->setTightBoundary(false);
colorMap->rescaleDataRange(true);
customPlot.rescaleAxes();
customPlot.replot();*/
window.setGeometry(100, 100, 500, 400);
window.show();
return a.exec();
}
void convertToVectorDouble(const arma::vec Avec, std::vector<double> &Aout)
{
unsigned int nElem = Avec.n_elem;
for (unsigned int i = 0; i < nElem; ++i)
{
Aout.at(i) = Avec(i);
}
} |
Partager