QCustomPlot Discussion and Comments

Multiple Axis (Y1, Y2, Y3, Y4 ...)Return to overview

Hi,
congratulation for these really good program.!

I writing an gerneal purpose program to analye various Kind of data. My intension is to compare various data with different Units. For these I need one or more additional Y-Axis.

The program starts with an 2 axis (xy), an than it should be possible to a third, forth or more axis to these existing configuration.

Thanks

Have a look at the advanced axis demo. It also contains one axis rect with multiple axes.
What you're basically looking for is the method QCPAxisRect::addAxis. Some details are given in the documentation of QCPAxisRect.

Thank for these answer.

Could you give an advice how i can configure the addidtion axis (Axiscolor, Ticks, SubTicks ...)

I have the problem that I can have serval configurations and they have indidually configured. How I can change the axis like this example :

customPlot->xAxis->setScaleType(QCPAxis::stLinear);

Where I get the "xAxis" ?

In the following example I have no ides How it works.

 
      QCPAxisRect *wideAxisRect = new QCPAxisRect(customPlot);
      wideAxisRect->setupFullAxesBox(true);
      wideAxisRect->axis(QCPAxis::atRight, 0)->setTickLabels(true);
      wideAxisRect->addAxis(QCPAxis::atLeft)->setTickLabelColor(QColor(0,255,0));        
      wideAxisRect->addAxis(QCPAxis::atLeft)->setTickLabelColor(QColor(255,0,0)); 

Many Thanks

The QCustomPlot::xAxis/yAxis/xAxis2/yAxis2 members offer a convenient way to access the main axes of the main axis rect. The normal way of accessing any axis is via the axis rect they are part of. This is already shown in the code you posted above:

wideAxisRect->axis(QCPAxis::atRight, 0)

This accesses the first (that's why the 0, you could also leave that parameter out since it defaults to 0) axis on the right side of the axis rect wideAxisRect. If you wanted to access the second axis on the left side of the second axis rect in the customPlot, you can write:

customPlot->axisRect(1)->axis(QCPAxis::atLeft, 1)

Note that both QCustomPlot::axisRect() and QCPAxisRect::axis() might return 0 if the requested axis rect/axis doesn't exist. So if you're not absolutely sure they exist upon calling it like shown above, you might want to add intermediate checks that the calls didn't return 0 (otherwise you will get a segfault and crash your application when you continue to use a 0-pointer).

You could store the returned axis in a stack variable to then work with it more conveniently:

QCPAxis *myAxis = customPlot->axisRect(1)->axis(QCPAxis::atLeft, 1);
myAxis->setRange(2.5, 5.5);
myAxis->setTickLabels(false);
myAxis->...

Many thanks Emanuel, I will test it.

ps. Ich wünsche noch einen schönen Sonntagnachmittag.

Hello there,

sorry that I am asking again. The following code is exactly what I want to use, but I have no control over the axis. In the answer from Feb. 23th. I thought am right, now I am not unterstanding how it works.

How I have to define the additional axis, so that getting addressable response.

Thanks a lot

UA


    
void PLOT2D::Test_Additional_Axis_2(QCustomPlot *customPlot){
    demoName = "3 Test_Additional_Axis_2";
    customPlot->plotLayout()->clear(); 
//----------------------------------------------------------------------------------------------------------------------
//                                                                                                       prepare data  |
//----------------------------------------------------------------------------------------------------------------------
    QVector<double> x1(20), y1(20), y2(20), y3(20);
    qsrand(3);
    for (int i=0; i<x1.size(); ++i)  {
        x1[i] = i/(double)(x1.size()-1)*10-5.0;
        y1[i] = qCos(x1[i]);
        y2[i] = qCos(x1[i])+1.0;
        y3[i] = qCos(x1[i])+2.0;
    }
//----------------------------------------------------------------------------------------------------------------------
//                                                                                                configure axis rect  |
//----------------------------------------------------------------------------------------------------------------------
    QCPAxisRect *wideAxisRect = new QCPAxisRect(customPlot);
    wideAxisRect->setupFullAxesBox(true);
    wideAxisRect->axis(QCPAxis::atRight, 0)->setTickLabels(true);
                                                                                                             //  Axis  |
    wideAxisRect->addAxis(QCPAxis::atLeft)->setTickLabelColor  (QColor (  0,255,  0));                       //    Y2  |
    wideAxisRect->addAxis(QCPAxis::atLeft)->setTickLabelColor  (QColor (255,  0,  0));                       //    Y3  |
    wideAxisRect->addAxis(QCPAxis::atRight)->setTickLabelColor (QColor (  0,255,  0));                       //    Y4  |
    wideAxisRect->addAxis(QCPAxis::atRight)->setTickLabelColor (QColor (  0,  0,255));                       //    Y5  |
//----------------------------------------------------------------------------------------------------------------------
//                                                                                       configure axis rect low left  |
//----------------------------------------------------------------------------------------------------------------------
    customPlot->plotLayout()->addElement  (0, 0, wideAxisRect);
//----------------------------------------------------------------------------------------------------------------------
//                                            synchronize the left and right margins of the top and bottom axis rects  |
//----------------------------------------------------------------------------------------------------------------------
    QCPMarginGroup *marginGroup = new QCPMarginGroup(customPlot);
    wideAxisRect->setMarginGroup(QCP::msLeft | QCP::msRight, marginGroup);
//----------------------------------------------------------------------------------------------------------------------
//                                                                                    create and configure plottables  |
//----------------------------------------------------------------------------------------------------------------------
     QCPGraph *mainGraph1 = customPlot->addGraph(wideAxisRect->axis(QCPAxis::atBottom), wideAxisRect->axis(QCPAxis::atLeft));
    mainGraph1->setData(x1, y1);
    mainGraph1->setScatterStyle(QCPScatterStyle(QCPScatterStyle::ssCircle, QPen(Qt::red), QBrush(Qt::white), 10));
    mainGraph1->setPen(QPen(QColor( 120,  00,  00), 6));

    QCPGraph *mainGraph2 = customPlot->addGraph(wideAxisRect->axis(QCPAxis::atBottom), wideAxisRect->axis(QCPAxis::atLeft));
    mainGraph2->setData(x1, y2);
    mainGraph2->setScatterStyle(QCPScatterStyle(QCPScatterStyle::ssCircle, QPen(Qt::green), QBrush(Qt::white), 10));
    mainGraph2->setPen(QPen(QColor(   0, 120,  00), 6));

    QCPGraph *mainGraph3 = customPlot->addGraph(wideAxisRect->axis(QCPAxis::atBottom), wideAxisRect->axis(QCPAxis::atLeft));
    mainGraph3->setData(x1, y3);
    mainGraph3->setScatterStyle(QCPScatterStyle(QCPScatterStyle::ssCircle, QPen(Qt::blue), QBrush(Qt::white), 10));
    mainGraph3->setPen(QPen(QColor(   0,   0, 120), 6));

//----------------------------------------------------------------------------------------------------------------------
//                                                                             rescale axes according to graph's data  |
//----------------------------------------------------------------------------------------------------------------------
    mainGraph1->rescaleAxes();
    wideAxisRect->axis(QCPAxis::atLeft, 1)->setRangeLower(0);
}

Just get and study example projects "interaction-example" and "plot-examples".