Contour plot using customplot library with x, y, z data not coming full contour.
-
I am using qt creator and trying to plot the contour plot with x, y, z data points. i am able to plot points but not contour. colormap is not coming. is there any interpolation required for it. is there any direct interpolation function for contour plot?
Below is the code
mainwindow.h#ifndef MAINWINDOW_H
#define MAINWINDOW_H#include <QMainWindow>
#include <QVector>
#include "qcustomplot.h"
#include <QVBoxLayout>
#include <QFile>
#include <QTextStream>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;
QCPColorScale *colorScale; // Pointer to color scale
QVector<QVector<double>> data; // Vector to hold the data
int nx; // Number of columns in data
int ny; // Number of rows in data
};#endif // MAINWINDOW_H
mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QFile>
#include <QTextStream>
#include <QVector>
#include <QDebug>
#include <QVBoxLayout>
#include <QHBoxLayout>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 // Create color scale and add it to a layout colorScale = new QCPColorScale(ui->customPlot); colorScale->setType(QCPAxis::atRight); ui->horizontalLayout ->addWidget(ui->customPlot); // Add color scale next to custom plot //loadData(); // Load data on initialization plotData(); // Now plot the data after loading
}
MainWindow::~MainWindow()
{
delete ui;
}//void MainWindow::loadData()
//{
// data.resize(nx); // Resize data container to hold nx by ny values// QFile file("D:\WorkData\2025\Projects\DRA\ContourPlotData\tecplot_ptt.dat");
// if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
// return;// double x, y, z;
// int index = 0;
// QTextStream in(&file);// while (!in.atEnd() && index < nx * ny) {
// QString line = in.readLine();
// QStringList numbers = line.split(' ', QString::SkipEmptyParts);// if (numbers.size() == 3) {
// x = numbers[0].toDouble();
// y = numbers[1].toDouble();
// z = numbers[2].toDouble();// int xIndex = static_cast<int>((x + 4) / (8.0 / (nx - 1)));
// int yIndex = static_cast<int>((y - 7) / (5.0 / (ny - 1)));// if (xIndex >= 0 && xIndex < nx && yIndex >= 0 && yIndex < ny) {
// data[xIndex][yIndex] = z; // Store the z value in the data vector
// }
// index++;
// }
// }
// file.close();
//}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()
{
QCPColorMap *colorMap = new QCPColorMap(ui->customPlot->xAxis, ui->customPlot->yAxis);
// Adjust dimensions as needed
int nx = 425;
int ny = 425;colorMap->data()->setSize(nx, ny); // Set the color map size colorMap->data()->setRange(QCPRange(-4, 4), QCPRange(7, 12)); // Modify based on your data range //colorMap->data()->setRange(QCPRange(-1.0, 1.0), QCPRange(7.5, 11.5)); QFile file("D:\\WorkData\\2025\\Projects\\DRA\\ContourPlotData\\tecplot_ptt.dat"); // Assuming your data is saved in `data.txt` if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) return; double x, y, z; int index = 0; // Cell index QTextStream in(&file); while (!in.atEnd() && index < nx * ny) { QString line = in.readLine(); QStringList numbers = line.split(" ", QString::SkipEmptyParts); if (numbers.size() == 3) { // Convert the strings to double x = numbers[0].toDouble(); y = numbers[1].toDouble(); z = numbers[2].toDouble(); // Find the index in the color map int xIndex = static_cast<int>((x - -4) / (8.0 / (nx - 1))); // Adjust based on your ranges int yIndex = static_cast<int>((y - 7) / (5.0 / (ny - 1))); // Adjust based on your ranges if (xIndex >= 0 && xIndex < nx && yIndex >= 0 && yIndex < ny) { colorMap->data()->setCell(xIndex, yIndex, z); } index++; } } file.close(); // 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 // Title setup //colorMap->setGradient(QCPColorGradient::gpJet); // Color gradient 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);
ui->customPlot->setWindowTitle("ContourPlot PTT"); // Set the main title// Axis titles setup ui->customPlot->xAxis->setLabel("X Axis Title"); // Set the label for X axis ui->customPlot->yAxis->setLabel("Y Axis Title"); // Set the label for Y axis //ui->customPlot->replot(); // ui->customPlot->afterReplot(); ui->horizontalLayout->addWidget(ui->customPlot); ui->customPlot->addLayer("contourLayer"); // Create a layer for contours colorMap->setLayer("contourLayer"); ui->customPlot->rescaleAxes();
// QCPContourPlot *contourPlot = new QCPContourPlot(ui->customPlot);
// contourPlot->setColorMap(colorMap);
// contourPlot->setData(colorMap->data()); // Set data from the color map
// contourPlot->setContourFillColor(true); // This will fill the areas between contour lines
// contourPlot->setContourLines(15); // Set number of contour lines you want to display}
mainwindow.ui
<?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>848</width>
<height>615</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>19</x>
<y>49</y>
<width>801</width>
<height>511</height>
</rect>
</property>
<layout class="QHBoxLayout" name="horizontalLayout">
<item>
<widget class="QCustomPlot" name="customPlot" native="true">
<property name="maximumSize">
<size>
<width>731</width>
<height>511</height>
</size>
</property>
</widget>
</item>
</layout>
</widget>
</widget>
<widget class="QMenuBar" name="menubar">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>848</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>main.cpp
#include "mainwindow.h"#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}using qcustomplot.h and qcustomplot.cpp as well in same project.
can anyone please me to get contour plotdata is below
-1.3523 11.6294 1.6393
-1.2402 11.6441 1.5674
-1.1279 11.6576 1.6377
-1.0154 11.6698 1.7405
-0.9029 11.6807 1.7257
-0.7902 11.6904 1.7124
-0.6775 11.6987 1.6947
-0.5647 11.7058 1.6807
-0.4518 11.7115 1.6608
-0.3389 11.7160 1.6316
-0.2260 11.7193 1.5598
-0.1130 11.7212 1.6933
0.0000 11.7218 1.7488
0.1130 11.7212 1.7311
0.2260 11.7193 1.7134
0.3389 11.7160 1.6936
0.4518 11.7115 1.6768
0.5647 11.7058 1.6651 -
I am using qt creator and trying to plot the contour plot with x, y, z data points. i am able to plot points but not contour. colormap is not coming. is there any interpolation required for it. is there any direct interpolation function for contour plot?
Below is the code
mainwindow.h#ifndef MAINWINDOW_H
#define MAINWINDOW_H#include <QMainWindow>
#include <QVector>
#include "qcustomplot.h"
#include <QVBoxLayout>
#include <QFile>
#include <QTextStream>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;
QCPColorScale *colorScale; // Pointer to color scale
QVector<QVector<double>> data; // Vector to hold the data
int nx; // Number of columns in data
int ny; // Number of rows in data
};#endif // MAINWINDOW_H
mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QFile>
#include <QTextStream>
#include <QVector>
#include <QDebug>
#include <QVBoxLayout>
#include <QHBoxLayout>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 // Create color scale and add it to a layout colorScale = new QCPColorScale(ui->customPlot); colorScale->setType(QCPAxis::atRight); ui->horizontalLayout ->addWidget(ui->customPlot); // Add color scale next to custom plot //loadData(); // Load data on initialization plotData(); // Now plot the data after loading
}
MainWindow::~MainWindow()
{
delete ui;
}//void MainWindow::loadData()
//{
// data.resize(nx); // Resize data container to hold nx by ny values// QFile file("D:\WorkData\2025\Projects\DRA\ContourPlotData\tecplot_ptt.dat");
// if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
// return;// double x, y, z;
// int index = 0;
// QTextStream in(&file);// while (!in.atEnd() && index < nx * ny) {
// QString line = in.readLine();
// QStringList numbers = line.split(' ', QString::SkipEmptyParts);// if (numbers.size() == 3) {
// x = numbers[0].toDouble();
// y = numbers[1].toDouble();
// z = numbers[2].toDouble();// int xIndex = static_cast<int>((x + 4) / (8.0 / (nx - 1)));
// int yIndex = static_cast<int>((y - 7) / (5.0 / (ny - 1)));// if (xIndex >= 0 && xIndex < nx && yIndex >= 0 && yIndex < ny) {
// data[xIndex][yIndex] = z; // Store the z value in the data vector
// }
// index++;
// }
// }
// file.close();
//}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()
{
QCPColorMap *colorMap = new QCPColorMap(ui->customPlot->xAxis, ui->customPlot->yAxis);
// Adjust dimensions as needed
int nx = 425;
int ny = 425;colorMap->data()->setSize(nx, ny); // Set the color map size colorMap->data()->setRange(QCPRange(-4, 4), QCPRange(7, 12)); // Modify based on your data range //colorMap->data()->setRange(QCPRange(-1.0, 1.0), QCPRange(7.5, 11.5)); QFile file("D:\\WorkData\\2025\\Projects\\DRA\\ContourPlotData\\tecplot_ptt.dat"); // Assuming your data is saved in `data.txt` if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) return; double x, y, z; int index = 0; // Cell index QTextStream in(&file); while (!in.atEnd() && index < nx * ny) { QString line = in.readLine(); QStringList numbers = line.split(" ", QString::SkipEmptyParts); if (numbers.size() == 3) { // Convert the strings to double x = numbers[0].toDouble(); y = numbers[1].toDouble(); z = numbers[2].toDouble(); // Find the index in the color map int xIndex = static_cast<int>((x - -4) / (8.0 / (nx - 1))); // Adjust based on your ranges int yIndex = static_cast<int>((y - 7) / (5.0 / (ny - 1))); // Adjust based on your ranges if (xIndex >= 0 && xIndex < nx && yIndex >= 0 && yIndex < ny) { colorMap->data()->setCell(xIndex, yIndex, z); } index++; } } file.close(); // 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 // Title setup //colorMap->setGradient(QCPColorGradient::gpJet); // Color gradient 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);
ui->customPlot->setWindowTitle("ContourPlot PTT"); // Set the main title// Axis titles setup ui->customPlot->xAxis->setLabel("X Axis Title"); // Set the label for X axis ui->customPlot->yAxis->setLabel("Y Axis Title"); // Set the label for Y axis //ui->customPlot->replot(); // ui->customPlot->afterReplot(); ui->horizontalLayout->addWidget(ui->customPlot); ui->customPlot->addLayer("contourLayer"); // Create a layer for contours colorMap->setLayer("contourLayer"); ui->customPlot->rescaleAxes();
// QCPContourPlot *contourPlot = new QCPContourPlot(ui->customPlot);
// contourPlot->setColorMap(colorMap);
// contourPlot->setData(colorMap->data()); // Set data from the color map
// contourPlot->setContourFillColor(true); // This will fill the areas between contour lines
// contourPlot->setContourLines(15); // Set number of contour lines you want to display}
mainwindow.ui
<?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>848</width>
<height>615</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>19</x>
<y>49</y>
<width>801</width>
<height>511</height>
</rect>
</property>
<layout class="QHBoxLayout" name="horizontalLayout">
<item>
<widget class="QCustomPlot" name="customPlot" native="true">
<property name="maximumSize">
<size>
<width>731</width>
<height>511</height>
</size>
</property>
</widget>
</item>
</layout>
</widget>
</widget>
<widget class="QMenuBar" name="menubar">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>848</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>main.cpp
#include "mainwindow.h"#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}using qcustomplot.h and qcustomplot.cpp as well in same project.
can anyone please me to get contour plotdata is below
-1.3523 11.6294 1.6393
-1.2402 11.6441 1.5674
-1.1279 11.6576 1.6377
-1.0154 11.6698 1.7405
-0.9029 11.6807 1.7257
-0.7902 11.6904 1.7124
-0.6775 11.6987 1.6947
-0.5647 11.7058 1.6807
-0.4518 11.7115 1.6608
-0.3389 11.7160 1.6316
-0.2260 11.7193 1.5598
-0.1130 11.7212 1.6933
0.0000 11.7218 1.7488
0.1130 11.7212 1.7311
0.2260 11.7193 1.7134
0.3389 11.7160 1.6936
0.4518 11.7115 1.6768
0.5647 11.7058 1.6651