qtrocket2/include/plotting/MatplotlibPlotter.h
2025-04-20 12:29:36 -06:00

98 lines
2.8 KiB
C++

#ifndef MATPLOTLIBPLOTTER_H
#define MATPLOTLIBPLOTTER_H
#include "Plotter.h"
#include "matplotlibcpp.h"
namespace plt = matplotlibcpp;
/**
* @brief Matplotlib-cpp implementation of the Plotter interface.
*/
class MatplotlibPlotter : public Plotter {
public:
virtual ~MatplotlibPlotter() = default;
void plot2D(const std::vector<double>& xData,
const std::vector<double>& yData,
const std::string& title,
const std::string& xLabel,
const std::string& yLabel) override {
plt::figure();
plt::plot(xData, yData);
// Plot any queued markers
for (const auto& marker : markers_) {
plt::axvline(marker.first, 0.0, 1.0, {{"color", "red"}, {"linestyle", "--"}});
plt::text(marker.first, *std::max_element(yData.begin(), yData.end()),
marker.second);
}
plt::title(title);
plt::xlabel(xLabel);
plt::ylabel(yLabel);
plt::grid(true);
plt::show();
// markers_.clear();
}
void addMarker(double xValue, const std::string& label) override {
markers_.emplace_back(xValue, label);
}
void plotPosVelAcc(const std::vector<double>& time,
const std::vector<double>& altitude,
const std::vector<double>& velocity,
const std::vector<double>& acceleration) override {
plt::figure();
plt::subplot(3, 1, 1);
plt::plot(time, altitude);
plt::title("Altitude vs Time");
plt::ylabel("Altitude (m)");
plt::grid(true);
for (const auto& marker : markers_) {
plt::axvline(marker.first, 0.0, 1.0, {{"color", "red"}, {"linestyle", "--"}});
plt::text(marker.first, *std::max_element(altitude.begin(), altitude.end()),
marker.second);
}
plt::subplot(3, 1, 2);
plt::plot(time, velocity);
plt::title("Velocity vs Time");
plt::ylabel("Velocity (m/s)");
plt::grid(true);
for (const auto& marker : markers_) {
plt::axvline(marker.first, 0.0, 1.0, {{"color", "red"}, {"linestyle", "--"}});
plt::text(marker.first, *std::max_element(velocity.begin(), velocity.end()),
marker.second);
}
plt::subplot(3, 1, 3);
plt::plot(time, acceleration);
plt::title("Acceleration vs Time");
plt::xlabel("Time (s)");
plt::ylabel("Acceleration (m/s2)");
plt::grid(true);
for (const auto& marker : markers_) {
plt::axvline(marker.first, 0.0, 1.0, {{"color", "red"}, {"linestyle", "--"}});
plt::text(marker.first, *std::max_element(acceleration.begin(), acceleration.end()),
marker.second);
}
plt::tight_layout();
plt::show();
// markers_.clear();
}
private:
std::vector<std::pair<double, std::string>> markers_;
};
#endif // MATPLOTLIBPLOTTER_H