unable create contour plot using qt, c++ and qcustomplot when x, y and z values are given.
-
wrote 15 days ago last edited by
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_NAMESPACEclass MainWindow : public QMainWindow {
Q_OBJECTpublic:
MainWindow(QWidget *parent = nullptr);
~MainWindow();protected:
void resizeEvent(QResizeEvent *event) override; // Override resizeEventprivate slots:
void plotData(); // Function to plot data
private:
Ui::MainWindow *ui;
};
#endif // MAINWINDOW_Hmainwindow.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 implementationif (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 dQCPMarginGroup *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 axisui->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> -
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.
2/2