简体   繁体   English

QCustomPlot:如何更新图表数据?

[英]QCustomPlot: How to update data of a graph?

I use QCustomPlot to display histograms of pictures. 我使用QCustomPlot显示图片的直方图。 The function I use to set the a curve is the following: 我用来设置曲线的函数如下:

void SingleHistogram::setHist(const QVector<double> &x,
                              const QVector<double> &y)
{
    //clearGraphs();
    graph(0)->setData(x, y);
    graph(0)->rescaleAxes(true);
    replot();
}

It works great for the first picture I open: 它适用于我打开的第一张照片:

在此输入图像描述

But when I set a new histogram using the same function, the first curve is not removed (even if setData() calls clearData() method of the graph) : 但是当我使用相同的函数设置新的直方图时,第一条曲线不会被删除(即使setData()调用图的clearData()方法):

在此输入图像描述

As you can seen the second curve (the peak) is added to the graph. 如您所见,第二条曲线(峰值)被添加到图表中。

I'd like not to delete and reconstruct a new QCPGraph for efficiency since I feel like it is useless. 我不想删除和重建新的QCPGraph以提高效率,因为我觉得它没用。

Could someone tell me what I am doing wrong here? 有人能告诉我这里我做错了什么吗?

Thanks! 谢谢!

Just out of curiosity, are you clearing your vectors x,y? 出于好奇,你要清除你的向量x,y吗? if not you should clear them before loading new graph. 如果没有,你应该在加载新图表之前清除它们。

code will look somewhat like 代码看起来有点像

// Graph 1    
setHist(x,y);  // set graph

//
// ..Some code
//

//before loading new values
x.clear();
y.clear();

// Graph 2 
// Fill up new values
// x=[],y=[]

// set NEW graph
setHist(x,y);

Actually, I found that the input data I provided the graph contained 2 sets of values. 实际上,我发现我提供的输入数据图包含2组值。 So each x coordinate had 2 y values. 所以每个x坐标都有2 y值。

It's interesting to know that QCustom plot will produce this kind of graph in this case! 有趣的是,在这种情况下,QCustom图将产生这种图形!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM