zl程序教程

您现在的位置是:首页 >  其他

当前栏目

QCustomplot (四) 控制轴相关属性

控制属性 相关
2023-09-27 14:27:31 时间

数字格式和精度

我们可以通过void QCPAxis::setNumberFormat ( const QString & formatCode)设置轴数字属性。formatCode是一个字符串,

  • 第一个参数和QString::number()关于Format的规定一致:
FormatMeaning
eformat as [-]9.9e[+
Eformat as [-]9.9E[+
fformat as [-]9.9
guse e or f format, whichever is the most concise
Guse E or f format, whichever is the most concise
  • 第二个参数和第三个参数都是QCustomPlot的参数,它们是可选的。
    其中第二个参数是为了让第一个参数的科学计数法更加beatiful的,用b表示。乘法默认情况下是使用居中的dot表示,如果你想要用 × \times ×表示,那么你可以用字符c(cross)表示。

轴颜色

通过QPen来设置

标签

  • 字体 setLabelFonts

刻度

  • setTickLabelColor 刻度颜色
  • setTickLabelRotation 倾斜度
  • setTickLength 主刻度长度
  • setSubTicks 次刻度
  • setSubTickLength 次刻度长度
    在这里插入图片描述

范围

customPlot->xAxis->setRange().

Widget::Widget(QWidget *parent)
    : QWidget(parent)
{
    QChart q;
    this->resize(800,500);
    QCustomPlot *customPlot=new QCustomPlot();
    customPlot->setParent(this);
    //customPlot->resize(650,500);
    QHBoxLayout * pLayout=new QHBoxLayout;
    pLayout->addWidget(customPlot);
    this->setLayout(pLayout);


    // generate some data:
    std::vector<double> t;
    std::generate_n(std::back_inserter(t),2000,[n=-1000.0]()mutable {return (n++)*0.004;});
    std::vector<double> y1,y2;
    std::transform(t.begin(),t.end(),std::back_inserter(y1),[](double ele){return sin(ele);});
    std::transform(t.begin(),t.end(),std::back_inserter(y2),[](double ele){return cos(ele);});
    QPen pen;
    pen.setStyle(Qt::DotLine);
    customPlot->addGraph();
    customPlot->graph(0)->setName("y=sin(x)");
    customPlot->graph(0)->setPen(QPen(Qt::red));
    customPlot->graph(0)->setData(QVector<double>(t.begin(),t.end()),QVector<double>(y1.begin(),y1.end()));

    customPlot->addGraph();
    customPlot->graph(1)->setPen(QPen(Qt::blue));
    customPlot->graph(1)->setName("y=cos(x)");
    customPlot->graph(1)->setData(QVector<double>(t.begin(),t.end()),QVector<double>(y2.begin(),y2.end()));


    customPlot->graph(0)->setBrush(QBrush(QColor(255,50,30,20)));
    customPlot->graph(0)->setChannelFillGraph(customPlot->graph(1));
    customPlot->legend->removeItem(customPlot->legend->itemCount()-1); // don't show two confidence band graphs in legend


    // give the axes some labels:
    customPlot->xAxis->setLabel("x");
    customPlot->xAxis->setSubTicks(true);
    customPlot->xAxis->setTickLength(50);
    customPlot->xAxis->setSubTickLength(30);
    customPlot->xAxis->setTickLabelColor(Qt::darkGray);
    customPlot->xAxis->setTickLabelRotation(30);
    customPlot->xAxis->setNumberFormat("eb");
    customPlot->yAxis->setNumberPrecision(1);
    customPlot->yAxis->setLabel("y");

    // set axes ranges, so we see all data:
    customPlot->xAxis->setRange(-3.14/2, 3.14);
    customPlot->yAxis->setRange(0, 1);

    customPlot->legend->setVisible(true);

}

在这里插入图片描述