Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Qt Creator and other tools
  4. unable create contour plot using qt, c++ and qcustomplot when x, y and z values are given.
Forum Update on Monday, May 27th 2025

unable create contour plot using qt, c++ and qcustomplot when x, y and z values are given.

Scheduled Pinned Locked Moved Unsolved Qt Creator and other tools
2 Posts 2 Posters 95 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    Soniya
    wrote on 12 Apr 2025, 14:08 last edited by
    #1

    unable create contour plot using qt, c++ and qcustomplot but i am able to get the contour using python matplotlib using same data. tried some interpolation as well but not getting expected contour plot.

    mainwindow.h

    #include "qcustomplot.h"
    #include <QFile>
    #include <QTextStream>
    #include <QVBoxLayout>
    #include <QVector>

    QT_BEGIN_NAMESPACE
    namespace Ui {
    class MainWindow;
    }
    QT_END_NAMESPACE

    class MainWindow : public QMainWindow {
    Q_OBJECT

    public:
    MainWindow(QWidget *parent = nullptr);
    ~MainWindow();

    protected:
    void resizeEvent(QResizeEvent *event) override; // Override resizeEvent

    private slots:

    void plotData(); // Function to plot data

    private:
    Ui::MainWindow *ui;
    };
    #endif // MAINWINDOW_H

    mainwindow.cpp

    #include "mainwindow.h"
    #include "ui_mainwindow.h"
    #include <QDebug>
    #include <QFile>
    #include <QPrintDialog>
    #include <QTextStream>
    #include <QVector>
    #include <cmath>

    MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent), ui(new Ui::MainWindow) {
    ui->setupUi(this);

    // Set up horizontal layout for the main window
    ui->centralwidget = new QWidget(this);
    ui->horizontalLayout = new QHBoxLayout(ui->centralwidget);
    this->setCentralWidget(ui->centralwidget);
    // Create and configure the custom plot
    ui->customPlot = new QCustomPlot();
    ui->customPlot->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
    ui->horizontalLayout->addWidget(ui->customPlot); // Add custom plot to the layout
    ui->horizontalLayout->addWidget(ui->customPlot); // Add color scale next to custom plot
    setWindowTitle("Contour Plot");

    plotData(); // Now plot the data after loading
    }

    MainWindow::~MainWindow() { delete ui; }

    void MainWindow::resizeEvent(QResizeEvent *event) {
    QMainWindow::resizeEvent(event); // Call base class implementation

    if (ui->customPlot) {
    // Ensure that the customPlot fills the main window
    ui->customPlot->setGeometry(0, 0, this->width(), this->height());
    ui->customPlot->replot(); // Optional: replot to ensure data is displayed correctly
    }
    }

    void MainWindow::plotData() {
    // Load data from file
    QVector<double> xData, yData, zData;
    QFile file("D:\WorkData\2025\Projects\DRA\ContourPlotData\tecplot_ptt.dat");
    if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
    return;
    }

    QTextStream in(&file);
    while (!in.atEnd()) {
    double x, y, z;
    in >> x >> y >> z;
    if (!in.status()) {
    xData.append(x);
    yData.append(y);
    zData.append(z);
    }
    }
    file.close();

    // Create color map
    QCPColorMap *colorMap =
    new QCPColorMap(ui->customPlot->xAxis, ui->customPlot->yAxis);
    int nx = 400, ny = 400;

    // Fill grid with interpolated values
    double xMin = *std::min_element(xData.begin(), xData.end());
    double xMax = *std::max_element(xData.begin(), xData.end());
    double yMin = *std::min_element(yData.begin(), yData.end());
    double yMax = *std::max_element(yData.begin(), yData.end());
    colorMap->data()->setSize(nx, ny);
    colorMap->data()->setRange(QCPRange(xMin, xMax), QCPRange(yMin, yMax));

    // Gaussian smoothing interpolation
    double sigma = 0.15;
    double sigma2 = sigma * sigma;

    for (int ix = 0; ix < nx; ++ix) {
    for (int iy = 0; iy < ny; ++iy) {
    double xi = xMin + (xMax - xMin) * ix / (nx - 1);
    double yi = yMin + (yMax - yMin) * iy / (ny - 1);

      double weightedZ = 0.0;
      double weightSum = 0.0;
    
      for (int i = 0; i < xData.size(); ++i) {
        double dx = xi - xData[i];
        double dy = yi - yData[i];
        double dist2 = dx * dx + dy * dy;
        double weight = std::exp(-dist2 / (2 * sigma2));
        weightedZ += weight * zData[i];
        weightSum += weight;
      }
    
      double zi = weightSum > 0 ? weightedZ / weightSum : 0;
      colorMap->data()->setCell(ix, iy, zi);
    }
    

    }

    // Customize and display
    colorMap->setInterpolate(true);
    colorMap->setGradient(QCPColorGradient::gpThermal);
    colorMap->rescaleDataRange();

    ui->customPlot->rescaleAxes();
    // Setup color scale and appearance
    QCPColorScale *colorScale = new QCPColorScale(ui->customPlot);
    ui->customPlot->plotLayout()->addElement(0, 1, colorScale);
    colorScale->setType(QCPAxis::atRight);
    colorMap->setColorScale(colorScale);
    colorScale->axis()->setLabel("PTT"); // Z value label

    // Legend Color setup
    QCPColorGradient customGradient;
    customGradient.setColorStopAt(0, Qt::white); // Start color (white)
    customGradient.setColorStopAt(0.25, Qt::blue); // End color (red)
    customGradient.setColorStopAt(0.5, Qt::cyan);
    customGradient.setColorStopAt(0.75, Qt::yellow);
    customGradient.setColorStopAt(1.0, Qt::red); // End color (red)
    customGradient.setColorStopAt(1.25, Qt::darkGreen);
    customGradient.setColorStopAt(1.5, Qt::red);
    customGradient.setColorStopAt(1.75, Qt::darkRed); // End color (red)
    colorMap->setGradient(customGradient); // Apply the custom gradient to the color map
    colorMap->rescaleDataRange(); // Rescale the color map to fit d

    QCPMarginGroup *marginGroup = new QCPMarginGroup(ui->customPlot);
    ui->customPlot->axisRect()->setMarginGroup(QCP::msBottom | QCP::msTop,
    marginGroup);
    colorScale->setMarginGroup(QCP::msBottom | QCP::msTop, marginGroup);

    // Axis titles setup
    ui->customPlot->xAxis->setLabel("Theta"); // Set the label for X axis
    ui->customPlot->yAxis->setLabel("Radius"); // Set the label for Y axis

    ui->customPlot->rescaleAxes();
    }

    main.cpp
    #include "mainwindow.h"

    #include <QApplication>

    int main(int argc, char *argv[])
    {
    QApplication a(argc, argv);
    MainWindow w;
    w.show();
    return a.exec();
    }

    <?xml version="1.0" encoding="UTF-8"?>
    <ui version="4.0">
    <class>MainWindow</class>
    <widget class="QMainWindow" name="MainWindow">
    <property name="geometry">
    <rect>
    <x>0</x>
    <y>0</y>
    <width>944</width>
    <height>630</height>
    </rect>
    </property>
    <property name="windowTitle">
    <string>MainWindow</string>
    </property>
    <widget class="QWidget" name="centralwidget">
    <widget class="QWidget" name="horizontalLayoutWidget">
    <property name="geometry">
    <rect>
    <x>50</x>
    <y>30</y>
    <width>861</width>
    <height>511</height>
    </rect>
    </property>
    <layout class="QHBoxLayout" name="horizontalLayout">
    <item>
    <widget class="QCustomPlot" name="customPlot" native="true"/>
    </item>
    </layout>
    </widget>
    </widget>
    <widget class="QMenuBar" name="menubar">
    <property name="geometry">
    <rect>
    <x>0</x>
    <y>0</y>
    <width>944</width>
    <height>26</height>
    </rect>
    </property>
    </widget>
    <widget class="QStatusBar" name="statusbar"/>
    </widget>
    <customwidgets>
    <customwidget>
    <class>QCustomPlot</class>
    <extends>QWidget</extends>
    <header location="global">qcustomplot.h</header>
    <container>1</container>
    </customwidget>
    </customwidgets>
    <resources/>
    <connections/>
    </ui>

    1 Reply Last reply
    0
    • S Offline
      S Offline
      SGaist
      Lifetime Qt Champion
      wrote on 12 Apr 2025, 19:14 last edited by
      #2

      Hi,

      Please use coding tags (the </> button in the editor) to make your code readable and please reduce it to a minimal example that shows the issue.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      1

      2/2

      12 Apr 2025, 19:14

      • Login

      • Login or register to search.
      2 out of 2
      • First post
        2/2
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved