QCustomPlot Discussion and Comments

Bug??? Data plotting out of orderReturn to overview

I ran across an interesting problem. My data is being plotted out of order. I have included a simple code snippet to show the problem.

    ui->setupUi(this);

    ui->plot->setInteraction(QCP::iRangeDrag, true);
    ui->plot->setInteraction(QCP::iRangeZoom, true);

    QCPGraph *graphL;

    ui->plot->clearGraphs();

#if 1
    //  BAD VERSION, BUT ONE THAT SHOULD WORK.  WHY IS IT REORDERING MY DATA???
    graphL = ui->plot->addGraph();
    graphL->setLineStyle(QCPGraph::lsLine);
    graphL->setScatterStyle(QCPScatterStyle::ssDisc);
    graphL->addData(1.936885, 1.504717);   // pnt0
    graphL->addData(1.941000, 1.675000);   // pnt1
    graphL->addData(1.940299, 1.764220);   // pnt2
#else
    //  GOOD VERSION, BUT SHOULDN'T HAVE TO SPLIT INTO LINE SEGMENTS
    graphL = ui->plot->addGraph();
    graphL->setLineStyle(QCPGraph::lsLine);
    graphL->setScatterStyle(QCPScatterStyle::ssDisc);
    graphL->addData(1.936885, 1.504717);   // pnt0
    graphL->addData(1.941000, 1.675000);   // pnt1

    graphL = ui->plot->addGraph();
    graphL->setLineStyle(QCPGraph::lsLine);
    graphL->setScatterStyle(QCPScatterStyle::ssDisc);
    graphL->addData(1.941000, 1.675000);   // pnt1
    graphL->addData(1.940299, 1.764220);   // pnt2
#endif

    ui->plot->xAxis->setLabel("X");
    ui->plot->yAxis->setLabel("Y");

    ui->plot->rescaleAxes();

    ui->plot->replot();

If #if 1 is used, then you will see the points plotted as 0, 2, 1. If you draw them as line segments, then you get the correct order of 0, 1, 2. I tried to post an image, but was unable to do so. This was done using Qt 5.1.1, mingw 32bit, Qt Creator 2.8.1 on a Windows 7 Pro 64bit box.

Did I do something wrong? I would think that custom plot should not ever reorder my data.

Thanks,
Mike Goza

Hi Mike,

What you observe is intended behaviour and part of an important optimization that QCPGraph does. QCPGraph can only be used for single-valued keys. I quote the documentation of QCPGraph:

Graphs are used to display single-valued data. Single-valued means that there should only be one data point per unique key coordinate. In other words, the graph can't have loops. If you do want to plot non-single-valued curves, rather use the QCPCurve plottable.

So if you want loops, please use QCPCurve instead.

I figured it was something simple. I'll try that.

Thanks!

I have a Problem with data plotting too where a single y-value is out of order... I add several Graphs to plotter with

// Add graph
plotter->addGraph();
plotter->graph(iGraph)->setData(*xAxis, [inline]*spn->getYAxis()[/inline]);
plotter->graph(iGraph)->setPen(QPen(spn->getColor()));
plotter->graph(iGraph)->setLineStyle(spn->getLineStyle());
plotter->graph(iGraph)->setScatterStyle(spn->getScatterStyle());
plotter->graph(iGraph)->setName(spn->getDisplayName());
plotter->graph(iGraph)->rescaleValueAxis();

in a for-loop. When the data is plotted, there are sometimes "Peaks" in the graph displayed. I plot values over time and the x- and y-values are as expected in *spn->getYAxis() and *xAxis, but when I debug the plotter component and look into the QCPGraph that should plot These values i am confused. The values aren't as expected. When i look at the Position around the Peak, i see a QMapNode<double, QCPData> entry with key=5.3941...e-315 and value=5.32862...e-315. That is not what i expected :) So this is the first time i debug the plotter, so i think i look at the wrong place.

To the Peaks:
Because i can't upload an Image, i will try to explain the graph... The graph is looking like this:

                              # y=60
                             /|
                            / \
----#-#-#--#---------#-----#   #--#-#-##----#-----------#-#-##-##-#--#-#-----#... y=50

# = data Point
- = line
/ = line
| = line

The values are always 50, but there is an y-value Peak with value 60.

So i have two questions:
1. Am i looking at the correct place when Debugging QCustomPlot component to see the values?
2. What is the reason for the y-value Peaks in plotter?

I am using QCustomPlot 1.2.0 beta2 with Qt 4.8. Also, i observed this behaviour with older Versions like 1.1.0... Hope this helps.

That's probably a manifestation of how QMap stores multiple values for the same key. Iterating through them will yield the last added entry (for that key) first. I wish this was different, but that's just how Qt made their container work. And adding extra code to invert the iteration over multiple values with the same key would reduce performance too much for all other users, I fear. So I suggest you implement this special case behaviour in your code. What you need to do is impose an order, either by sorting your data accordingly before, using QCPCurve (if you don't need QCPGraph's high performance), or adding an extra base point before your peaks.

Hi DerManu,

thanks for the fast Response, i am not sure whether i understood your explenation really :)

"...QMap stores multiple values for the same key..."

For example in my graphs:

Graph 1                            Graph 2           Graph ...
(key)         (value)
x             y                    x         y
...           ...                  ...       ...
9             40                   9         40
10            50                   10        50
11            50                   11        50
12            50                   12        60
13            50                   13        60
14            60                   14        70
...           ...                  ...       ...

Where x means QVector<double> *xAxis and y is (QVector<double>)*spn->getYAxis(). I understand key as the x-axis, which is in this example the time and value as the y-axis, which is the value of the timestamp. Why are there multiple values for the same key?

I tried to replace QCPGraph with QCPCurve, but the result is the same (still with Peaks), what am i doing wrong?

//    QCPGraph *graph = ui->plotter->addGraph(axisRect->axis(QCPAxis::atBottom), axisRect->axis(QCPAxis::atLeft));
    QCPCurve *graph = new QCPCurve(axisRect->axis(QCPAxis::atBottom), axisRect->axis(QCPAxis::atLeft));
    ui->plotter->addPlottable(graph);

Ah, I must have understood your question wrong. For the following code using your data

  ui->customPlot->addGraph();
  QVector<double> x1 = QVector<double>() << 9 << 10 << 11 << 12 << 13 << 14;
  QVector<double> y1 = QVector<double>() << 40 << 50 << 50 << 50 << 50 << 60;
  ui->customPlot->graph(0)->setData(x1, y1);
  
  ui->customPlot->addGraph();
  QVector<double> x2 = QVector<double>() << 9 << 10 << 11 << 12 << 13 << 14;
  QVector<double> y2 = QVector<double>() << 40 << 50 << 50 << 60 << 60 << 70;
  ui->customPlot->graph(1)->setData(x2, y2);
  
  ui->customPlot->yAxis->setAutoTickStep(false);
  ui->customPlot->xAxis->setAutoTickStep(false);
  ui->customPlot->yAxis->setTickStep(10);
  ui->customPlot->xAxis->setTickStep(1);

  ui->customPlot->rescaleAxes();
  ui->customPlot->savePng("./taneeda-test.png", 300, 200);

I get this output which looks correct:
http://www.qcustomplot.com/images/forum/taneeda-test.png
Do you get something different? Is it what you'd expect? (If you want to show me an image, you can mail it to the support address given on the contact page. For security reasons, image upload currently isn't allowed in this forum. Might change some day...)

The two graph are just examples to illustrate the Graphs. With this example the Graphs are as expected, no Problem.

Please try to use the example below, in this case you see the Peak at y=60 value range:

customPlot->addGraph();
  QVector<double> x1 = QVector<double>()
          << 3567
          << 4164
          << 4719
          << 5273
          << 5913
          << 6425
          << 7023
          << 7620
          << 8175
          << 8772
          << 11417
          << 12015
          << 13593
          << 14148
          << 14745
          << 15300
          << 15854
          << 16452
          << 17006
          << 17604
          << 19908
          << 23577
          << 24131
          << 24729
          << 25283
          << 25881
          << 26435
          << 27033
          << 27587
          << 28185
          << 28782
          << 29379
          << 33603
          << 34200
          << 34755
          << 35310
          << 35907
          << 36462
          << 37059
          << 37656
          << 38296
          << 43587
          << 44184
          << 44739
          << 45336
          << 45891
          << 46488
          << 47042
          << 47640
          << 48194
          << 48792
          << 49389
          << 50114
          << 53570
          << 54168
          << 54722
          << 55320
          << 55874
          << 56471
          << 57069
          << 57666
          << 58263
          << 58861
          << 60098
          << 61421
          << 62018
          << 62615
          << 63170
          << 63767
          << 64322
          << 64919
          << 65474
          << 66028
          << 66626
          << 67180
          << 67778
          << 68332
          << 68930
          << 69484
          << 70082
          << 70636
          << 71191
          << 71788
          << 72343
          << 72940
          << 73537
          << 74092
          << 74689
          << 75244
          << 75799
          << 76396
          << 76951
          << 77548
          << 78103
          << 78700
          << 79297
          << 80108
          << 83606
          << 84204
          << 84758
          << 85313
          << 85910
          << 86465
          << 87062
          << 87617
          << 88214
          << 88769
          << 89366
          << 90006
          << 90561
          << 93590
          << 94145
          << 94699
          << 95297
          << 95851
          << 96449
          << 99904
          << 100459
          << 103574
          << 104171
          << 104726
          << 105280
          << 105878
          << 106432
          << 107029
          << 107627
          << 108181
          << 108779
          << 109333
          << 109931
          << 110485
          << 111424
          << 112021
          << 113600
          << 114197
          << 114752
          << 115306
          << 115904
          << 116458
          << 117056
          << 117610
          << 119872
          << 120469
          << 123584
          << 124138
          << 124735
          << 125190
          << 125745
          << 126342
          << 126897
          << 127494
          << 128091
          << 128689
          << 129243
          << 129841
          << 130438
          << 131035
          << 133510
          << 134107
          << 134662
          << 135259
          << 135814
          << 136411
          << 136966
          << 137563
          << 138203
          << 143493
          << 144048
          << 144645
          << 145200
          << 145755
          << 146352
          << 146949
          << 147504
          << 148101
          << 150021
          << 153477
          << 154032
          << 154586
          << 155184
          << 155738
          << 156336
          << 156976
          << 157530
          << 158127
          << 158725
          << 161071
          << 161669
          << 162266
          << 162863
          << 163461
          << 164015
          << 164613
          << 165167
          << 165722
          << 166319
          << 166874
          << 167471
          << 168026
          << 168623
          << 169220
          << 169775
          << 170372
          << 170927
          << 171524
          << 172079
          << 172634
          << 173231
          << 173828
          << 174383
          << 174937
          << 175492
          << 176089
          << 176644
          << 177241
          << 177839
          << 178393
          << 178991
          << 179545
          << 180143
          << 180740
          << 183470
          << 184068
          << 184622
          << 185220
          << 185774
          << 186329
          << 186926
          << 187524
          << 188078
          << 188676
          << 189273
          << 189870
          << 193454
          << 194009
          << 194606
          << 195161
          << 195758
          << 196313
          << 199769
          << 203480
          << 204035
          << 204590
          << 205187
          << 205742
          << 206339
          << 206894
          << 207491
          << 208088
          << 208686
          << 211288
          << 211885
          << 213464
          << 214061
          << 214616
          << 215213
          << 215768
          << 216365
          << 216920
          << 217517
          << 218072
          << 219778
          << 223490
          << 224045
          << 224642
          << 225197
          << 225752
          << 226349
          << 226903
          << 227501
          << 228098
          << 228653
          << 229250
          << 229847
          << 230402
          << 230999
          << 233517
          << 234114
          << 234668
          << 235223
          << 235820
          << 236375
          << 236972
          << 237570
          << 238210
          << 243500
          << 244097
          << 244652
          << 245207
          << 245804
          << 246359
          << 246956
          << 247511
          << 248108
          << 250028
          << 253441
          << 254038
          << 254593
          << 255148
          << 255745
          << 256342
          << 256897
          << 257537
          << 258134
          << 258732
          << 261078
          << 261675
          << 262273
          << 262870
          << 263467
          << 264022
          << 264619
          << 265174
          << 265729
          << 266326
          << 266881
          << 267478
          << 268033
          << 268630
          << 269184
          << 269739
          << 270336
          << 270891
          << 271488
          << 272043
          << 272640
          << 273195
          << 273792
          << 274347
          << 274902
          << 275499
          << 276054
          << 276651
          << 277206
          << 277803
          << 278400
          << 278955
          << 279552
          << 280107
          << 280661
          << 281259
          << 281856
          << 282411
          << 283008
          << 283605
          << 284160
          << 284715
          << 285312
          << 285866
          << 286464
          << 287018
          << 287616
          << 288170
          << 288768
          << 289365
          << 289920
          << 290517
          << 291114
          << 291669
          << 292266
          << 292864
          << 293461
          << 294016
          << 294613
          << 295167
          << 295722
          << 296319
          << 296874
          << 297471
          << 298026
          << 298623
          << 299178
          << 299818
          << 300415
          << 303487
          << 304084
          << 304639
          << 305236
          << 305791
          << 306388
          << 306943
          << 307540
          << 308180
          << 308735
          << 311295
          << 311892
          << 313471
          << 314068
          << 314623
          << 315177
          << 315775
          << 316329
          << 316927
          << 317481
          << 319785
          << 323454
          << 324052
          << 324606
          << 325161
          << 325758
          << 326313
          << 326910
          << 327508
          << 328062
          << 328659
          << 329257
          << 329854
          << 333523
          << 334078
          << 334675
          << 335230
          << 335785
          << 336382
          << 336979
          << 337576
          << 338216
          << 343507
          << 344062
          << 344616
          << 345214
          << 345768
          << 346365
          << 346920
          << 347517
          << 348072
          << 348669
          << 349267
          << 349864
          << 350419
          << 353491
          << 354045
          << 354600
          << 355197
          << 355752
          << 356349
          << 356946
          << 357544
          << 358141
          << 358781
          << 361085
          << 361682
          << 362280
          << 362834
          << 363474
          << 364029
          << 364626
          << 365181
          << 365735
          << 366333
          << 366887
          << 367485
          << 368039
          << 368637
          << 369191
          << 369789
          << 370343
          << 370941
          << 371495
          << 372050
          << 326910
          << 372647
          << 373202
          << 373799
          << 374354
          << 374908
          << 375506
          << 376060
          << 376658
          << 377255
          << 377810
          << 378407
          << 379004
          << 379559
          << 383484
          << 384039
          << 384636
          << 385191
          << 385745
          << 386343
          << 386897
          << 387495
          << 388092
          << 388646
          << 389244
          << 389841
          << 390438
          << 393468
          << 394022
          << 394620
          << 395174
          << 395772
          << 396326
          << 399782
          << 403451
          << 404049
          << 404603
          << 405158
          << 405755
          << 406310
          << 406907;
  QVector<double> y1 = QVector<double>()
          << 0
          << 0
          << 0
          << 0
          << 0
          << 0
          << 0
          << 0
          << 70
          << 70
          << 70
          << 70
          << 70
          << 70
          << 70
          << 70
          << 70
          << 70
          << 70
          << 70
          << 70
          << 70
          << 70
          << 70
          << 70
          << 70
          << 70
          << 70
          << 70
          << 70
          << 70
          << 70
          << 70
          << 70
          << 70
          << 70
          << 70
          << 70
          << 70
          << 70
          << 70
          << 70
          << 70
          << 70
          << 70
          << 70
          << 70
          << 70
          << 70
          << 70
          << 70
          << 70
          << 70
          << 70
          << 70
          << 70
          << 70
          << 70
          << 70
          << 70
          << 0
          << 0
          << 0
          << 0
          << 0
          << 0
          << 0
          << 0
          << 0
          << 0
          << 0
          << 0
          << 0
          << 0
          << 0
          << 0
          << 0
          << 0
          << 0
          << 0
          << 0
          << 0
          << 0
          << 0
          << 0
          << 0
          << 0
          << 0
          << 0
          << 0
          << 0
          << 0
          << 0
          << 0
          << 0
          << 0
          << 0
          << 0
          << 0
          << 0
          << 0
          << 0
          << 0
          << 0
          << 0
          << 0
          << 0
          << 0
          << 0
          << 0
          << 0
          << 0
          << 0
          << 0
          << 0
          << 0
          << 0
          << 0
          << 0
          << 0
          << 0
          << 0
          << 0
          << 0
          << 0
          << 0
          << 0
          << 0
          << 10
          << 10
          << 10
          << 10
          << 10
          << 10
          << 10
          << 10
          << 10
          << 10
          << 10
          << 10
          << 10
          << 10
          << 10
          << 10
          << 10
          << 10
          << 10
          << 10
          << 10
          << 10
          << 10
          << 10
          << 10
          << 10
          << 10
          << 10
          << 10
          << 10
          << 10
          << 10
          << 10
          << 10
          << 10
          << 10
          << 10
          << 10
          << 10
          << 10
          << 10
          << 10
          << 10
          << 10
          << 10
          << 10
          << 10
          << 10
          << 10
          << 10
          << 10
          << 10
          << 10
          << 10
          << 10
          << 20
          << 20
          << 20
          << 20
          << 20
          << 20
          << 20
          << 20
          << 20
          << 20
          << 20
          << 20
          << 20
          << 20
          << 20
          << 20
          << 20
          << 20
          << 20
          << 20
          << 20
          << 20
          << 20
          << 20
          << 20
          << 20
          << 20
          << 20
          << 20
          << 20
          << 20
          << 20
          << 20
          << 20
          << 20
          << 20
          << 20
          << 20
          << 20
          << 20
          << 20
          << 20
          << 20
          << 20
          << 20
          << 20
          << 20
          << 20
          << 20
          << 20
          << 20
          << 20
          << 20
          << 20
          << 20
          << 20
          << 20
          << 20
          << 20
          << 20
          << 20
          << 20
          << 20
          << 20
          << 20
          << 30
          << 30
          << 30
          << 30
          << 30
          << 30
          << 30
          << 30
          << 30
          << 30
          << 30
          << 30
          << 30
          << 30
          << 30
          << 30
          << 30
          << 30
          << 30
          << 30
          << 30
          << 30
          << 30
          << 30
          << 30
          << 30
          << 30
          << 30
          << 30
          << 30
          << 30
          << 30
          << 30
          << 30
          << 30
          << 30
          << 30
          << 30
          << 30
          << 30
          << 30
          << 30
          << 30
          << 30
          << 30
          << 30
          << 30
          << 30
          << 30
          << 30
          << 30
          << 30
          << 30
          << 30
          << 40
          << 40
          << 40
          << 40
          << 40
          << 40
          << 40
          << 40
          << 40
          << 40
          << 40
          << 40
          << 40
          << 40
          << 40
          << 40
          << 40
          << 40
          << 40
          << 40
          << 40
          << 40
          << 40
          << 40
          << 40
          << 40
          << 40
          << 40
          << 40
          << 40
          << 40
          << 40
          << 40
          << 40
          << 40
          << 40
          << 40
          << 40
          << 40
          << 40
          << 40
          << 40
          << 40
          << 40
          << 40
          << 40
          << 40
          << 40
          << 40
          << 40
          << 40
          << 40
          << 40
          << 40
          << 40
          << 40
          << 40
          << 40
          << 40
          << 40
          << 40
          << 40
          << 40
          << 40
          << 40
          << 40
          << 40
          << 40
          << 40
          << 40
          << 40
          << 40
          << 40
          << 40
          << 40
          << 40
          << 40
          << 40
          << 40
          << 40
          << 50
          << 50
          << 50
          << 50
          << 50
          << 50
          << 50
          << 50
          << 50
          << 50
          << 50
          << 50
          << 50
          << 50
          << 50
          << 50
          << 50
          << 50
          << 50
          << 50
          << 50
          << 50
          << 50
          << 50
          << 50
          << 50
          << 50
          << 50
          << 50
          << 50
          << 50
          << 50
          << 50
          << 50
          << 50
          << 50
          << 50
          << 50
          << 50
          << 50
          << 50
          << 50
          << 50
          << 50
          << 50
          << 50
          << 50
          << 50
          << 50
          << 50
          << 50
          << 50
          << 50
          << 50
          << 60
          << 60
          << 60
          << 60
          << 60
          << 60
          << 60
          << 60
          << 60
          << 60
          << 60
          << 60
          << 60
          << 60
          << 60
          << 60
          << 60
          << 60
          << 60
          << 60
          << 60
          << 60
          << 60
          << 60
          << 60
          << 60
          << 60
          << 60
          << 60
          << 60
          << 60
          << 60
          << 60
          << 60
          << 60
          << 60
          << 60
          << 60
          << 60
          << 60
          << 60
          << 60
          << 60
          << 60
          << 60
          << 60
          << 60
          << 60
          << 60
          << 60
          << 60
          << 60
          << 60
          << 60
          << 60
          << 60
          << 60
          << 60
          << 60
          << 60
          << 60
          << 60
          << 60
          << 60;
  ui->customPlot->graph(0)->setData(x1, y1);

  customPlot->yAxis->setAutoTickStep(false);
  customPlot->xAxis->setAutoTickStep(false);
  customPlot->yAxis->setTickStep(10);
  customPlot->xAxis->setTickStep(1);

  customPlot->rescaleAxes();

With this example you see the Peak at the range of y=60 values

sorry, typing mistake, the Peak is in the y=50 value range...

I can see the spike, but it's in your data:

The value 326910 exists twice as key, once with value 50 and once with value 60. And the second time comes later in that range:

            ...
            << 371495
            << 372050
            << 326910 // here
            << 372647
            << 373202
            ...

And thus overwrites the previous value 50 with 60, resulting in the display you see.

Awesome, i really thank you for the fast help and sorry for my mistake :)