Loading...
Searching...
No Matches
MatplotlibPlotter.h
Go to the documentation of this file.
#ifndef MATPLOTLIBPLOTTER_H
#define MATPLOTLIBPLOTTER_H
#include "Plotter.h"
#include "matplotlibcpp.h"
namespace plt = matplotlibcpp;
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
Plotter.h
MatplotlibPlotter
Matplotlib-cpp implementation of the Plotter interface.
Definition MatplotlibPlotter.h:12
MatplotlibPlotter::addMarker
void addMarker(double xValue, const std::string &label) override
Add a vertical marker at a specific x-axis value.
Definition MatplotlibPlotter.h:41
MatplotlibPlotter::~MatplotlibPlotter
virtual ~MatplotlibPlotter()=default
MatplotlibPlotter::plot2D
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
Plot a 2D line graph.
Definition MatplotlibPlotter.h:16
MatplotlibPlotter::markers_
std::vector< std::pair< double, std::string > > markers_
Definition MatplotlibPlotter.h:95
MatplotlibPlotter::plotPosVelAcc
void plotPosVelAcc(const std::vector< double > &time, const std::vector< double > &altitude, const std::vector< double > &velocity, const std::vector< double > &acceleration) override
Plot altitude, velocity, and acceleration together on a single canvas.
Definition MatplotlibPlotter.h:45
Plotter
Abstract interface for plotting flight data.
Definition Plotter.h:9
matplotlibcpp.h
matplotlibcpp
Definition matplotlibcpp.h:43
matplotlibcpp::tight_layout
void tight_layout()
Definition matplotlibcpp.h:2741
matplotlibcpp::figure
long figure(long number=-1)
Definition matplotlibcpp.h:1875
matplotlibcpp::title
void title(const std::string &titlestr, const std::map< std::string, std::string > &keywords={})
Definition matplotlibcpp.h:2296
matplotlibcpp::axvline
void axvline(double x, double ymin=0., double ymax=1., const std::map< std::string, std::string > &keywords=std::map< std::string, std::string >())
Definition matplotlibcpp.h:2378
matplotlibcpp::subplot
void subplot(long nrows, long ncols, long plot_number)
Definition matplotlibcpp.h:2252
matplotlibcpp::plot
bool plot()
Definition matplotlibcpp.h:2860
matplotlibcpp::xlabel
void xlabel(const std::string &str, const std::map< std::string, std::string > &keywords={})
Definition matplotlibcpp.h:2431
matplotlibcpp::show
void show(const bool block=true)
Definition matplotlibcpp.h:2542
matplotlibcpp::text
void text(Numeric x, Numeric y, const std::string &s="")
Definition matplotlibcpp.h:1834
matplotlibcpp::ylabel
void ylabel(const std::string &str, const std::map< std::string, std::string > &keywords={})
Definition matplotlibcpp.h:2452
matplotlibcpp::grid
void grid(bool flag)
Definition matplotlibcpp.h:2525