关键词不能为空

当前您在: 主页 > 英语 >

范围QCustomPlot Demo 范例

作者:高考题库网
来源:https://www.bjmy2z.cn/gaokao
2021-01-21 05:18
tags:

底气-范围

2021年1月21日发(作者:血腥)
Plot Screenshots: Simple Demo


// add two new graphs and set their look:

customPlot->addGraph();

customPlot->graph(0)->setPen(QPen(Qt::blue)); // line color blue for first graph

customP lot->graph(0)->setBrush(QBrush(QColor(0, 0, 255, 20))); // first graph will be filled with translucent blue

customPlot->addGraph();

customPlot->graph(1)->setPen(QPen(Qt::red)); // line color red for second graph

// generate some points of data (y0 for first, y1 for second graph):

QVector x(250), y0(250), y1(250);

for(inti=0; i<250; ++i)

{



x[i] = i;



y0[i] = qExp(-i/150.0)*qCos(i/10.0); // exponentially decaying cosine



y1[i] = qExp(-i/150.0);












// exponential envelope

}

// configure right and top axis to show ticks but no labels:

// (see QCPAxisRect::setupFullAxesBox for a quicker method to do this)

customPlot->xAxis2->setVisible(true);

customPlot->xAxis2->setTickLabels(false);

customPlot->yAxis2->setVisible(true);

customPlot->yAxis2->setTickLabels(false);

// make left and bottom axes always transfer their ranges to right and top axes:

connect(customPlot->xAxis, SIGNAL(rangeChanged(QCPRange)), customPlot->xAxis2, SLOT(setRange(QCPRange)));

connect(customPlot->yAxis, SIGNAL(rangeChanged(QCPRange)), customPlot->yAxis2, SLOT(setRange(QCPRange)));

// pass data points to graphs:

customPlot->graph(0)->setData(x, y0);

customPlot->graph(1)->setData(x, y1);

// let the ranges scale themselves so graph 0 fits perfectly in the visible area:

customPlot->graph(0)->rescaleAxes();

// same thing for graph 1, but only enlarge ranges (in case graph 1 is smaller than graph 0):

customPlot->graph(1)->rescaleAxes(true);

// Note: we could have also just called customPlot->rescaleAxes(); instead

// Allow user to drag axis ranges with mouse, zoom with mouse wheel and select graphs by clicking:

customPlot->setInteractions(QCP::iRangeDrag| QCP::iRangeZoom| QCP::iSelectPlottables);








Plot Screenshots: Sinc Scatter Demo


customPlot->legend->setVisible(true);

customPlot->legend->setFont(QFont(

// set locale to english, so we get english decimal separator:

customPlot->setLocale(QLocale(Q Locale::English, QLocale::UnitedKingdom));

// add confidence band graphs:

customPlot->addGraph();

QPenpen;

le(Qt::DotLine);

th(1);

or(QColor(180,180,180));

customPlot->graph(0)->setName(

customPlot->graph(0)->setPen(pen);

cus tomPlot->graph(0)->setBrush(QBrush(QColor(255,50,3 0,20)));

customPlot->addGraph();

c ustomPlot->legend->removeItem(customPlot->legend-> itemCount()-1); // don't show two confidence band graphs in legend

customPlot->graph(1)->setPen(pen);

cus tomPlot->graph(0)->setChannelFillGraph(customPlot- >graph(1));

// add theory curve graph:

customPlot->addGraph();

le(Qt::DashLine);

th(2);

or(Qt::red);

customPlot->graph(2)->setPen(pen);

customPlot->graph(2)->setName(

// add data point graph:

customPlot->addGraph();

customPlot->gr aph(3)->setPen(QPen(Qt::blue));

customPlot ->graph(3)->setLineStyle(QCPGraph::lsNone);
customPlot->graph(3)->setScatterStyle(QCPScatter Style(QCPScatterStyle::ssCross, 4));

custo mPlot->graph(3)->setErrorType(QCPGraph::etValue);< br>
customPlot->graph(3)->setErrorPen(QPen(QCo lor(180,180,180)));

customPlot->graph(3)->setName(


// generate ideal sinc curve data and some randomly perturbed data for scatter plot:

QVector x0(250), y0(250);

QVector yConfUpper(250), yConfLower(250);

for(inti=0; i<250; ++i)

{



x0[i] = (i/249.0-0.5)*30+0.01; // by adding a small offset we make sure not do divide by zero in next code line



y0[i] = qSin(x0[i])/x0[i]; // sinc function



yConfUpper[i] = y0[i]+0.15;



yConfLower[i] = y0[i]-0.15;



x0[i] *= 1000;

}

QVector x1(50), y1(50), y1err(50);

for(inti=0; i<50; ++i)

{



// generate a gaussian distributed random number:



doubletmp1 =
rand
()/(double)RAND_MAX;



doubletmp2 =
rand
()/(double)RAND_MAX;



doubler = qSqrt(-2*qLn(tmp1))*qCos(2*M_PI*tmp2); // box- muller transform for gaussian distribution



// set y1 to value of y0 plus a random gaussian pertubation:



x1[i] = (i/50.0-0.5)*30+0.25;



y1[i] = qSin(x1[i])/x1[i]+r*0.15;



x1[i] *= 1000;



y1err[i] = 0.15;

}

// pass data to graphs and let QCustomPlot determine the axes ranges so the whole thing is visible:

customPlot->graph(0)->setData(x0, yConfUpper);

customPlot->graph(1)->setData(x0, yConfLower);

customPlot->graph(2)->setData(x0, y0);

customPlot->graph(3)->setDataValueError(x1, y1, y1err);

customPlot->graph(2)->rescaleAxes();

customPlot->graph(3)->rescaleAxes(true);

// setup look of bottom tick labels:

c ustomPlot->xAxis->setTickLabelRotation(30);

customPlot->xAxis->setAutoTickCount(9);

customPlot->xAxis->setNumberFormat(

customPlot->xAxis->setNumberPrecision(1);

customPlot->xAxis->moveRange(-10);

// make top right axes clones of bottom left axes. Looks prettier:

customPlot->axisRect()->se tupFullAxesBox();






Plot Screenshots: Scatter Style Demo



customPlot->legend->setVisible(true);

customPlot->legend->setFont(QFont(

customPlot->legend->setRowSpacing(-3);

QVector shapes;

shapes << QCPScatterStyle::ssCross;

shapes << QCPScatterStyle::ssPlus;

shapes << QCPScatterStyle::ssCircle;

shapes << QCPScatterStyle::ssDisc;

shapes << QCPScatterStyle::ssSquare;

shapes << QCPScatterStyle::ssDiamond;

shapes << QCPScatterStyle::ssStar;

shapes << QCPScatterStyle::ssTriangle;

shapes << QCPScatterStyle::ssTriangleInverted;

shapes << QCPScatterStyle::ssCrossSquare;

shapes << QCPScatterStyle::ssPlusSquare;

shapes << QCPScatterStyle::ssCrossCircle;

shapes << QCPScatterStyle::ssPlusCircle;

shapes << QCPScatterStyle::ssPeace;

shapes << QCPScatterStyle::ssCustom;


QPenpen;

// add graphs with different scatter styles:

for(inti=0; i<(); ++i)

{



customPlot->addGraph();



or(QColor(qSin(i*0.3)*100+100, qSin(i*0.6+0.7)*100+100, qSin(i*0.4+0.6)*100+100));



// generate data:



QVector x(10), y(10);



for(intk=0; k<10; ++k)



{





x[k] = k/10.0* 4*3.14+ 0.01;





y[k] = 7*qSin(x[k])/x[k] + (()-i)*5;



}



customPlot->graph()->setData(x, y);



customPlot->graph()->rescaleAxes(true);



customPlot->graph()->setPen(pen);



customPlot->graph()->setName(QCPScatterSt yle::ator(QCPScatterStyle::
xOfEnumerator(



customPlot->graph()->setLineStyle(QC PGraph::lsLine);



// set scatter style:



if((i) != QCPScatterStyle::ssCustom)



{





customPlot->graph()->setSc atterStyle(QCPScatterStyle((i), 10));



}



else



{





QPainterPathcustomScatterPath;





for(inti=0; i<3; ++i)







o(qCos(2*M_PI*i/3.0)*9, qSin(2*M_PI*i/3.0)*9, qCos(2*M_PI*(i+0.9)/3.0)*9,
qSin(2*M_PI*(i+0.9)/3.0)*9, 0, 0);





customPlot->graph()->setScatter Style(QCPScatterStyle(customScatterPath,
QPen(Qt::black,
0),
QColor(40,
70,
255,
50), 10));



}

}

// set blank axis lines:

customPlot->rescaleAxes();

customPlot->xAxis->setTicks(false);

customPlot->yAxis->setTicks(false);

customPlot->xAxis->setTickLabels(false);

customPlot->yAxis->setTickLabels(false);

// make top right axes clones of bottom left axes:

customPlot->axisRect()->setupFullAxe sBox();















Plot Screenshots: Styled Plot Demo


// prepare data:

QVector x1(20), y1(20);

QVector x2(100), y2(100);

QVector x3(20), y3(20);

QVector x4(20), y4(20);

for(inti=0; i<(); ++i)

{



x1[i] = i/(double)()*10;



y1[i] = qCos(x1 [i]*0.8+qSin(x1[i]*0.16+1.0))*qSin(x1[i]*0.54)+1.4 ;

}

for(inti=0; i<(); ++i)

{



x2[i] = i/(double)()*10;



y2[i] = qCos(x2 [i]*0.85+qSin(x2[i]*0.165+1.1))*qSin(x2[i]*0.50)+1 .7;

}

for(inti=0; i<(); ++i)

{



x3[i] = i/(double)()*10;



y3[i] = 0.05+3* (0.5+qCos(x3[i]*x3[i]*0.2+2)*0.5)/(double)(x3[i]+0 .7)+qrand()/(double)RAND_MAX*0.01;

}

for(inti=0; i<(); ++i)

{



x4[i] = x3[i];



y4[i] = (0.5-y3[i])+((x4[i]-2)*(x4[i]-2)*0.02);

}


// create and configure plottables:

QCPGraph*graph1 = customPlot->addGraph();

graph1->setData(x1, y1);

graph1->setSc atterStyle(QCPScatterStyle(QCPScatterStyle::ssCirc le, QPen(Qt::black, 1.5), QBrush(Qt::white), 9));

graph1->setPen(QPen(QColor(120, 120, 120), 2));


QCPGraph*graph2 = customPlot->addGraph();

graph2->setData(x2, y2);

graph2->setPen(Qt::NoPen);

graph2->setBrush(QColor(200, 200, 200, 20));

graph2->setChannelFillGraph(graph1);


QCPBars*bars1 = newQCPBars(customPlot->xAxis, customPlot->yAxis);

customPlot->addPlottable(bars1);

bars1->setWidth(9/(double)());

bars1->setData(x3, y3);

bars1->setPen(Qt::NoPen);

bars1->setBrush(QColor(10, 140, 70, 160));


QCPBars*bars2 = newQCPBars(customPlot->xAxis, customPlot->yAxis);

customPlot->addPlottable(bars2);

bars2->setWidth(9/(double)());

bars2->setData(x4, y4);

bars2->setPen(Qt::NoPen);

bars2->setBrush(QColor(10, 100, 50, 70));

bars2->moveAbove(bars1);


// move bars above graphs and grid below bars:

customPlot->addLayer(

customPlot->addLayer(

graph1->setLayer(

customPlot->xAxis->grid()->setLayer(

customPlot->yAxis->grid()->setLayer(


// set some pens, brushes and backgrounds:

customPlot->xAxis->setBasePen(QPen(Qt::white, 1));

customPlot->yAxis->setBasePen(QPen(Qt::white, 1));

customPlot->xAxis->setTickPen(QPen(Qt::white, 1));

customPlot->yAxis->setTickPen(QPen(Qt::white, 1));

customPlot->xAxis->setSubTickPen(QPen (Qt::white, 1));

customPlot->yAxis->setSub TickPen(QPen(Qt::white, 1));

customPlot->x Axis->setTickLabelColor(Qt::white);

custom Plot->yAxis->setTickLabelColor(Qt::white);

customPlot->xAxis->grid()->setPen(QPen(QColor(140 , 140, 140), 1, Qt::DotLine));

customPlot- >yAxis->grid()->setPen(QPen(QColor(140, 140, 140), 1, Qt::DotLine));

customPlot->xAxis->grid( )->setSubGridPen(QPen(QColor(80, 80, 80), 1, Qt::DotLine));

customPlot->yAxis->grid()-> setSubGridPen(QPen(QColor(80, 80, 80), 1, Qt::DotLine));

customPlot->xAxis->grid()-> setSubGridVisible(true);

customPlot->yAxis ->grid()->setSubGridVisible(true);

customP lot->xAxis->grid()->setZeroLinePen(Qt::NoPen);

customPlot->yAxis->grid()->setZeroLinePen(Qt:: NoPen);

customPlot->xAxis->setUpperEnding( QCPLineEnding::esSpikeArrow);

customPlot-> yAxis->setUpperEnding(QCPLineEnding::esSpikeArrow) ;

QLinearGradientplotGradient;

rt(0, 0);

alStop(0, 350);

orAt(0, QColor(80, 80, 80));

orAt(1, QColor(50, 50, 50));

customPlot->setBackground(plotGradient);

QLinearGradientaxisRectGradient;

rt(0, 0);

alStop(0, 350);

orAt(0, QColor(80, 80, 80));

orAt(1, QColor(30, 30, 30));

customPlot->axisRect()->setBackg round(axisRectGradient);


customPlot->rescaleAxes();

customPlot->yAxis->setRange(0, 2);











Plot Screenshots: Color Map Demo


// configure axis rect:

customPlot->se tInteractions(QCP::iRangeDrag|QCP::iRangeZoom); // this will also allow rescaling the color scale by
dragging/zooming

customPlot->axisRect( )->setupFullAxesBox(true);

customPlot->xAxis->setLabel(

customPlot->yAxis->setLabel(


// set up the QCPColorMap:

QCPColorMap*colorMap = newQCPColorMap(customPlot->xAxis, customPlot->yAxis);

customPlot->addPlottable(colorMap);

intnx = 200;

intny = 200;

colorMap->data()->setSize(nx, ny); // we want the color map to have nx * ny data points

colorMap->data()->setRange(QCPRange(-4,
4),
QCPRange(-4, 4)); // and
span the
coordinate
range -4..4
in
both
key
(x)
and
value (y) dimensions

// now we assign some data, by accessing the QCPColorMapData instance of the color map:

doublex, y, z;

for(intxIndex=0; xIndex
{



for(intyIndex=0; yIndex


{





colorMap->data()->cellToCoord(xIndex, yIndex, &x, &y);





doubler = 3*qSqrt(x*x+y*y)+1e-2;





z = 2*x*(qCos(r+2)/r-qSin(r+2)/r); // the B field strength of dipole radiation (modulo physical constants)





colorMap->data()->setCell(xIndex, yIndex, z);



}

}


// add a color scale:

QCPColorScale*colorScale = newQCPColorScale(customPlot);

customPlot->plotLayout()->addElement(0, 1, colorScale); // add it to the right of the main axis rect

colorScale->setType(QCPAxis::atRight); // scale shall be vertical bar with tick/axis labels right (actually atRight is
already the default)

colorMap->setColorScale(colorScale); // associate the color map with the color scale

colorScale->axis()->setLabel(


// set the color gradient of the color map to one of the presets:

colorMap->setGradient(QCPColo rGradient::gpPolar);

// we could have also created a QCPColorGradient instance and added own colors to

// the gradient, see the documentation of QCPColorGradient for what's possible.


// rescale the data dimension (color) such that all data points lie in the span visualized by the color gradient:

colorMap->rescaleDataRange();


// make sure the axis rect and color scale synchronize their bottom and top margins (so they line up):

QCPMarginGroup*marginGroup = newQCPMarginGroup(customPlot);

customPlot- >axisRect()->setMarginGroup(QCP::msBottom|QCP::msT op, marginGroup);

colorScale->setMarginGro up(QCP::msBottom|QCP::msTop, marginGroup);


// rescale the key (x) and value (y) axes so the whole color map is visible:

customPlot->rescaleAxes();


Plot Screenshots: Scatter Pixmap Demo


cus tomPlot->axisRect()->setBackground(QPixmap(

customPlot->addGraph();

customPlot->gr aph()->setLineStyle(QCPGraph::lsLine);

QPenpen;

or(QColor(255, 200, 20, 200));

le(Qt::DashLine);

thF(2.5);

customPlot->graph()->setPen(pen);

cust omPlot->graph()->setBrush(QBrush(QColor(255,200,20 ,70)));

customPlot->graph()->setScatterSty le(QCPScatterStyle(QPixmap(

// set graph name, will show up in legend next to icon:

customPlot->graph()->setName(

// set data:

QVector year, value;

year
<< 2005<< 2006<< 2007<< 2008

<< 2009

<< 2010;

value << 2.17<< 3.42<< 4.94<< 10.38<< 15.86<< 29.33;

customPlot->graph()->setData(year, value);


// set title of plot:

customPlot->plotLayout()->insertRow(0);

customPlot->plotLayout()->addElement(0, 0, newQCPPlotTitle(customPlot,

// set a fixed tick-step to one tick per year value:

customPlot->xAxis->setAutoTickStep(false);

customPlot->xAxis->setTickStep(1);

customPlot->xAxis->setSubTickCount(3);

// other axis configurations:

customPlot->xAxis->setLabel(

customPlot->yAxis->setLabel(

customPlot->xAxis2->setVisible(true);

customPlot->yAxis2->setVisible(true);

customPlot->xAxis2->setTickLabels(false);

customPlot->yAxis2->setTickLabels(false);

customPlot->xAxis2->setTicks(false);

customPlot->yAxis2->setTicks(false);

customPlot->xAxis2->setSubTickCount(0);

customPlot->yAxis2->setSubTickCount(0);

customPlot->xAxis->setRange(2004.5, 2010.5);

customPlot->yAxis->setRange(0, 30);

// setup legend:

customPlot-> legend->setFont(QFont(font().family(), 7));
customPlot->legend->setIconSize(50, 20);

customPlot->legend->setVisible(true);












Plot Screenshots: Realtime Data Demo

Setup function:
customPlot->addGraph(); // blue line

customPlot->graph(0)->setPen(QPe n(Qt::blue));

customPlot->graph(0)->setBru sh(QBrush(QColor(240, 255, 200)));

customP lot->graph(0)->setAntialiasedFill(false);

customPlot->addGraph(); // red line

cu stomPlot->graph(1)->setPen(QPen(Qt::red));

customPlot->graph(0)->setChannelFillGraph(customP lot->graph(1));


customPlot->addGraph(); // blue dot

cu stomPlot->graph(2)->setPen(QPen(Qt::blue));
customPlot->graph(2)->setLineStyle(QCPGraph::lsN one);

customPlot->graph(2)->setScatterStyl e(QCPScatterStyle::ssDisc);

customPlot->addGraph(); // red dot

cus tomPlot->graph(3)->setPen(QPen(Qt::red));

customPlot->graph(3)->setLineStyle(QCPGraph::lsNon e);

customPlot->graph(3)->setScatterStyle( QCPScatterStyle::ssDisc);


customPlot ->xAxis->setTickLabelType(QCPAxis::ltDateTime);

customPlot->xAxis->setDateTimeFormat(

customPlot->xAxis->setAutoTickStep(false);

customPlot->xAxis->setTickStep(2);

cus tomPlot->axisRect()->setupFullAxesBox();


// make left and bottom axes transfer their ranges to right and top axes:

connect(customPlot->xAxis, SIGNAL(rangeChanged(QCPRange)), customPlot->xAxis2, SLOT(setRange(QCPRange)));

connect(customPlot->yAxis, SIGNAL(rangeChanged(QCPRange)), customPlot->yAxis2, SLOT(setRange(QCPRange)));


// setup a timer that repeatedly calls MainWindow::realtimeDataSlot:

connect(&dataTimer, SIGNAL(timeout()), this, SLOT(realtimeDataSlot()));

(0); // Interval 0 means to refresh as fast as possible




realtimeDataSlot
, called by timer:
// calculate two new data points:

doublekey = QDateTime::currentDateTime().toMSecsSinceEpoch()/1 000.0;

staticdoublelastPointKey = 0;

if(key-lastPointKey > 0.01) // at most add point every 10 ms

{



doublevalue0 = qSin(key); //qSin(key*1.6+qCos(key*1.7)*2)*10 + qSin(key*1.2+0.56)*20 + 26;



doublevalue1 = qCos(key); //qSin(key*1.3+qCos(key*1.2)*1.2)*7 + qSin(key*0.9+0.26)*24 + 26;



// add data to lines:



ui->customPlot->graph(0)->addData(key, value0);



ui->customPlot->graph(1)->addData(key, value1);



// set data of dots:



ui->customPlot->graph(2)->clearData();



ui->customPlot->graph(2)->addData(key, value0);



ui->customPlot->graph(3)->clearData();



ui->customPlot->graph(3)->addData(key, value1);



// remove data of lines that's outside visible range:



ui ->customPlot->graph(0)->removeDataBefore(key-8);


ui->customPlot->graph(1)->removeD ataBefore(key-8);



// rescale value (vertical) axis to fit the current data:



ui->customPlot->graph(0)->rescaleVal ueAxis();



ui->customPlot->graph( 1)->rescaleValueAxis(true);



lastPointKey = key;

}

// make key axis range scroll with the data (at a constant range size of 8):

ui->customPlot->xAxis->setRange(key+0.25, 8, Qt::AlignRight);

ui->customPlot->replot();


// calculate frames per second:

staticdoublelastFpsKey;

staticintframeCount;

++frameCount;

if(key-lastFpsKey > 2) // average fps over 2 seconds

{



ui->statusBar->showMessage(









QString(









.arg(frameCount/(key-lastFpsKey), 0, 'f', 0)









.arg(ui->customPlot->graph(0)->data()->count()+ui ->customPlot->graph(1)->data()->count())









, 0);



lastFpsKey = key;



frameCount = 0;

}

























Plot Screenshots: Multiple Axes Demo


customPlot->setLocale(QLocale(QLocale::En glish, QLocale::UnitedKingdom)); // period as decimal separator and comma as
thousand separator

customPlot->legend->setVisible(true);

QFontlegendFont = font();
// start out with MainWindow's font..

ntSize(9); // and make a bit smaller for legend

customPlot->legend->setFont(legendFont);
< br>customPlot->legend->setBrush(QBrush(QColor(255, 255,255,230)));

//
by
default,
the
legend
is
in
the
inset
layout
of
the
main
axis
rect.
So
this
is
how
we
access
it
to
change
legend
placement:

customPlot->axisRect()->ins etLayout()->setInsetAlignment(0, Qt::AlignBottom|Qt::AlignRight);


// setup for graph 0: key axis left, value axis bottom

// will contain left maxwell-like function

customPlot->addGraph(customPlot->yAxis, customPlot->xAxis);

customPlot->graph(0)->setPen(QPen(QColor(255, 100, 0)));

customPlot->graph(0)->setBrush( QBrush(QPixmap(

customPlot->graph(0)->setL ineStyle(QCPGraph::lsLine);

customPlot->gr aph(0)->setScatterStyle(QCPScatterStyle(QCPScatter Style::ssDisc, 5));

customPlot->graph(0)->setName(


// setup for graph 1: key axis bottom, value axis left (those are the default axes)

// will contain bottom maxwell-like function

customPlot->addGraph();

customPlot->gr aph(1)->setPen(QPen(Qt::red));

customPlot- >graph(1)->setBrush(QBrush(QPixmap(

custom Plot->graph(1)->setLineStyle(QCPGraph::lsStepCente r);

customPlot->graph(1)->setScatterStyle( QCPScatterStyle(QCPScatterStyle::ssCircle, Qt::red, Qt::white, 7));

customPlot->graph (1)->setErrorType(QCPGraph::etValue);

customPlot->graph(1)->setName(


// setup for graph 2: key axis top, value axis right

// will contain high frequency sine with low frequency beating:

customPlot->addGraph(customPlot->xAxis2, customPlot->yAxis2);

customPlot->graph(2)- >setPen(QPen(Qt::blue));

customPlot->graph(2)->setName(


// setup for graph 3: same axes as graph 2

// will contain low frequency beating envelope of graph 2

customPlot->addGraph(customPlot->xAxis2, customPlot->yAxis2);

QPenblueDotPen;

or(QColor(30, 40, 255, 150));

le(Qt::DotLine);

thF(4);

customPlot->graph(3)->setPen(blueDotPen);

customPlot->graph(3)->setName(


// setup for graph 4: key axis right, value axis top

// will contain parabolically distributed data points with some random perturbance

customPlot->addGraph(customPlot->yAxis2, customPlot->xAxis2);

customPlot->graph(4)->setPen(QColor(50, 50, 50, 255));

customPlot->graph(4)->setLineSt yle(QCPGraph::lsNone);

customPlot->graph(4 )->setScatterStyle(QCPScatterStyle(QCPScatterStyle ::ssCircle, 4));

customPlot->graph(4)->setName(


// generate data, just playing with numbers, not much to learn here:

QVector x0(25), y0(25);

QVector x1(15), y1(15), y1err(15);

QVector x2(250), y2(250);

QVector x3(250), y3(250);

QVector x4(250), y4(250);

for(inti=0; i<25; ++i) // data for graph 0

{



x0[i] = 3*i/25.0;



y0[i] = qExp(-x0[i]*x0[i]*0.8)*(x0[i]*x0[i]+x0[i]);

}

for(inti=0; i<15; ++i) // data for graph 1

{



x1[i] = 3*i/15.0;;



y1[i] = qExp(-x1[i]*x1[i])*(x1[i]*x1[i])*2.6;



y1err[i] = y1[i]*0.25;

}

for(inti=0; i<250; ++i) // data for graphs 2, 3 and 4

{



x2[i] = i/250.0*3*M_PI;



x3[i] = x2[i];



x4[i] = i/250.0*100-50;



y2[i] = qSin(x2[i]*12)*qCos(x2[i])*10;



y3[i] = qCos(x3[i])*10;



y4[i] = 0.01*x4[i]*x4[i] + 1.5*(
rand
()/(double)RAND_MAX-0.5) + 1.5*M_PI;

}


// pass data points to graphs:

customPlot->graph(0)->setData(x0, y0);

customPlot->graph(1)->setDataValueError(x1, y1, y1err);

customPlot->graph(2)->setData(x2, y2);

customPlot->graph(3)->setData(x3, y3);

customPlot->graph(4)->setData(x4, y4);

// activate top and right axes, which are invisible by default:

customPlot->xAxis2->setVisible(true);

customPlot->yAxis2->setVisible(true);

// set ranges appropriate to show data:

customPlot->xAxis->setRange(0, 2.7);

customPlot->yAxis->setRange(0, 2.6);

customPlot->xAxis2->setRange(0, 3.0*M_PI);

customPlot->yAxis2->setRange(-70, 35);

// set pi ticks on top axis:

QVector piTicks;

QVector piLabels;

piTicks << 0

<< 0.5*M_PI << M_PI << 1.5*M_PI << 2*M_PI << 2.5*M_PI << 3*M_PI;

piLabels <<
π

π

< QString::fromUtf8(
π

QString::fromUtf8(
π

π

π


customPlot->xAxis2->setAutoTicks(false);
< br>customPlot->xAxis2->setAutoTickLabels(false);
customPlot->xAxis2->setTickVector(piTicks);

customPlot->xAxis2->setTickVectorLabels(p iLabels);

// add title layout element:

customPlot->plotLayout()->insertRow(0);

customPlot->plotLayout()->addElement(0, 0, newQCPPlotTitle(customPlot,

// set labels:

customPlot->xAxis->setLabel(

customPlot->yAxis->setLabel(

customPlot->xAxis2->setLabel(

customPlot->yAxis2->setLabel(

// make ticks on bottom axis go outward:

customPlot->xAxis->setTickLength(0, 5);

customPlot->xAxis->setSubTickLength(0, 3);

// make ticks on right axis go inward and outward:

customPlot->yAxis2->setTickLength(3, 3);

customPlot->yAxis2->setSubTickLength(1, 1);












Plot Screenshots: Logarithmic Axis Demo

customPlot->setNoAntialiasingOnDrag(true); // more performance/responsiveness during dragging

customPlot->addGraph();

QPenpen;

or(QColor(255,170,100));

th(2);

le(Qt::DotLine);

customPlot->graph(0)->setPen(pen);

customPlot->graph(0)->setName(


customPlot->addGraph();

customPlot->gr aph(1)->setPen(QPen(Qt::red));

customPlot- >graph(1)->setBrush(QBrush(QColor(255, 0, 0, 20)));

customPlot->graph(1)->setErrorType( QCPGraph::etBoth);

customPlot->graph(1)->setName(


customPlot->addGraph();

customPlot->gr aph(2)->setPen(QPen(Qt::blue));

customPlot ->graph(2)->setBrush(QBrush(QColor(0, 0, 255, 20)));

customPlot->graph(2)->setName(


customPlot->addGraph();

or(QColor(0,0,0));

th(1);

le(Qt::DashLine);

customPlot->graph(3)->setPen(pen);

cus tomPlot->graph(3)->setBrush(QBrush(QColor(0,0,0,15 )));

customPlot->graph(3)->setLineStyle(QC PGraph::lsStepCenter);

customPlot->graph(3)->setName(


QVector x0(200), y0(200);

QVector x1(200), y1(200);

QVector x2(200), y2(200);

QVector x3(21), y3(21);

for(inti=0; i<200; ++i)

{



x0[i] = i/10.0;



y0[i] = x0[i];



x1[i] = i/10.0;



y1[i] = -qSin(x1[i])*qExp(x1[i]);



x2[i] = i/10.0;



y2[i] = qSin(x2[i])*qExp(x2[i]);

}

for(inti=0; i<21; ++i)

{



x3[i] = i;



y3[i] = 1;



for(intk=1; k<=i; ++k) y3[i] *= k; // factorial

}

customPlot->graph(0)->setData(x0, y0);

customPlot->graph(1)->setData(x1, y1);

customPlot->graph(2)->setData(x2, y2);

customPlot->graph(3)->setData(x3, y3);


customPlot->yAxis->grid()->setSubGridVis ible(true);

customPlot->xAxis->grid()->set SubGridVisible(true);

customPlot->yAxis->s etScaleType(QCPAxis::stLogarithmic);

customPlot->yAxis->setScaleLogBase(100);

customPlot->yAxis->setNumberFormat(

customPlot->yAxis->setNumberPrecision(0); // makes sure

customPlot->yAxis->setSubTickCount(10);

customPlot->xAxis->setRange(0, 19.9);

customPlot->yAxis->setRange(1e-2, 1e10);

// make range draggable and zoomable:

customPlot->setInteractions(QCP::iRangeDrag| QCP::iRangeZoom);


// make top right axes clones of bottom left axes:

customPlo t->axisRect()->setupFullAxesBox();

// connect signals so top and right axes move in sync with bottom and left axes:

connect(customPlot->xAxis, SIGNAL(rangeChanged(QCPRange)), customPlot->xAxis2, SLOT(setRange(QCPRange)));

connect(customPlot->yAxis, SIGNAL(rangeChanged(QCPRange)), customPlot->yAxis2, SLOT(setRange(QCPRange)));


customPlot->legend->setVisible(true);

customPlot->legend->setBrush(QBrush(QColor(255,255 ,255,150)));

customPlot->axisRect()->inset Layout()->setInsetAlignment(0, Qt::AlignLeft|Qt::AlignTop); // make legend align in top
left corner or axis rect





Plot Screenshots: Line Style Demo


customPlot->legend->setVisible(true);

customPlot->legend->setFont(QFont(

QPenpen;

QStringListlineNames;

lineNames <<

// add graphs with different line styles:

for(inti=QCPGraph::lsNone; i<=QCPGraph::lsImpulse; ++i)

{



customPlot->addGraph();



or(QColor(qSin(i*1+1.2)*80+80, qSin(i*0.3+0)*80+80, qSin(i*0.3+1.5)*80+80));



customPlot->graph()->setPen(pen);



customPlot->graph()->setName((i-QCPGraph: :lsNone));



customPlot->graph()-> setLineStyle((QCPGraph::LineStyle)i);



customPlot->graph()->setScatterStyle(QCPS catterStyle(QCPScatterStyle::ssCircle, 5));



// generate data:



QVector x(15), y(15);



for(intj=0; j<15; ++j)



{





x[j] = j/15.0* 5*3.14+ 0.01;





y[j] = 7*qSin(x[j])/x[j] - (i-QCPGraph::lsNone)*5+ (QCPGraph::lsImpulse)*5+ 2;



}



customPlot->graph()->setData(x, y);



customPlot->graph()->rescaleAxes(true);

}

// zoom out a bit:

customPlot->yAxis->scaleRange(1.1, customPlot->yAxis->range().center());

customPlot->xAxis->scaleRange(1.1, customPlot->xAxis->range().center());

// set blank axis lines:

customPlot->xAxis->setTicks(false);

customPlot->yAxis->setTicks(true);

customPlot->xAxis->setTickLabels(false);

customPlot->yAxis->setTickLabels(true);

// make top right axes clones of bottom left axes:

customPlot->axisRect()->setupFullAxe sBox();



















Plot Screenshots: Date Axis Demo


// set locale to english, so we get english month names:

customPlot->setLocale (QLocale(QLocale::English, QLocale::UnitedKingdom));

// seconds of current time, we'll use it as starting point in time for data:

doublenow = QDateTime::currentDateTime().toTime_t();

srand
(8); // set the random seed, so we always get the same random data

// create multiple graphs:

for(intgi=0; gi<5; ++gi)

{



customPlot->addGraph();



QPenpen;



or(QColor(0, 0, 255, 200));



customPlot->graph()- >setLineStyle(QCPGraph::lsLine);



customPlot->graph()->setPen(pen);



customPlot->graph()->setBrush(QBrush(QCol or(255/4.0*gi,160,50,150)));



// generate random walk data:



QVector
time
(250), value(250);



for(inti=0; i<250; ++i)



{





time
[i] = now + 24*3600*i;





if(i == 0)







value[i] = (i/50.0+1)*(
rand
( )/(double)RAND_MAX-0.5);





else







value[i] = qFabs(value[i-1])*(1+0.02/4.0*(4-gi)) + (i/50.0+1) *(
rand
()/(double)RAND_MAX-0.5);



}



customPlot->graph()->setData(
time
, value);

}

底气-范围


底气-范围


底气-范围


底气-范围


底气-范围


底气-范围


底气-范围


底气-范围



本文更新与2021-01-21 05:18,由作者提供,不代表本网站立场,转载请注明出处:https://www.bjmy2z.cn/gaokao/542604.html

QCustomPlot Demo 范例的相关文章