QtRocket
 
Loading...
Searching...
No Matches
MatplotlibPlotter.h
Go to the documentation of this file.
1#ifndef MATPLOTLIBPLOTTER_H
2#define MATPLOTLIBPLOTTER_H
3
4#include "Plotter.h"
5#include "matplotlibcpp.h"
6
7namespace plt = matplotlibcpp;
8
12class MatplotlibPlotter : public Plotter {
13public:
14 virtual ~MatplotlibPlotter() = default;
15
16 void plot2D(const std::vector<double>& xData,
17 const std::vector<double>& yData,
18 const std::string& title,
19 const std::string& xLabel,
20 const std::string& yLabel) override {
21
23 plt::plot(xData, yData);
24
25 // Plot any queued markers
26 for (const auto& marker : markers_) {
27 plt::axvline(marker.first, 0.0, 1.0, {{"color", "red"}, {"linestyle", "--"}});
28 plt::text(marker.first, *std::max_element(yData.begin(), yData.end()),
29 marker.second);
30 }
31
32 plt::title(title);
33 plt::xlabel(xLabel);
34 plt::ylabel(yLabel);
35 plt::grid(true);
36 plt::show();
37
38// markers_.clear();
39 }
40
41 void addMarker(double xValue, const std::string& label) override {
42 markers_.emplace_back(xValue, label);
43 }
44
45 void plotPosVelAcc(const std::vector<double>& time,
46 const std::vector<double>& altitude,
47 const std::vector<double>& velocity,
48 const std::vector<double>& acceleration) override {
49
51 plt::subplot(3, 1, 1);
52 plt::plot(time, altitude);
53 plt::title("Altitude vs Time");
54 plt::ylabel("Altitude (m)");
55 plt::grid(true);
56
57 for (const auto& marker : markers_) {
58 plt::axvline(marker.first, 0.0, 1.0, {{"color", "red"}, {"linestyle", "--"}});
59 plt::text(marker.first, *std::max_element(altitude.begin(), altitude.end()),
60 marker.second);
61 }
62
63 plt::subplot(3, 1, 2);
64 plt::plot(time, velocity);
65 plt::title("Velocity vs Time");
66 plt::ylabel("Velocity (m/s)");
67 plt::grid(true);
68
69 for (const auto& marker : markers_) {
70 plt::axvline(marker.first, 0.0, 1.0, {{"color", "red"}, {"linestyle", "--"}});
71 plt::text(marker.first, *std::max_element(velocity.begin(), velocity.end()),
72 marker.second);
73 }
74
75 plt::subplot(3, 1, 3);
76 plt::plot(time, acceleration);
77 plt::title("Acceleration vs Time");
78 plt::xlabel("Time (s)");
79 plt::ylabel("Acceleration (m/s2)");
80 plt::grid(true);
81
82 for (const auto& marker : markers_) {
83 plt::axvline(marker.first, 0.0, 1.0, {{"color", "red"}, {"linestyle", "--"}});
84 plt::text(marker.first, *std::max_element(acceleration.begin(), acceleration.end()),
85 marker.second);
86 }
87
89 plt::show();
90
91// markers_.clear();
92 }
93
94private:
95 std::vector<std::pair<double, std::string>> markers_;
96};
97
98#endif // MATPLOTLIBPLOTTER_H
Matplotlib-cpp implementation of the Plotter interface.
Definition MatplotlibPlotter.h:12
void addMarker(double xValue, const std::string &label) override
Add a vertical marker at a specific x-axis value.
Definition MatplotlibPlotter.h:41
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
Plot a 2D line graph.
Definition MatplotlibPlotter.h:16
std::vector< std::pair< double, std::string > > markers_
Definition MatplotlibPlotter.h:95
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
Abstract interface for plotting flight data.
Definition Plotter.h:9
Definition matplotlibcpp.h:43
void tight_layout()
Definition matplotlibcpp.h:2741
long figure(long number=-1)
Definition matplotlibcpp.h:1875
void title(const std::string &titlestr, const std::map< std::string, std::string > &keywords={})
Definition matplotlibcpp.h:2296
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
void subplot(long nrows, long ncols, long plot_number)
Definition matplotlibcpp.h:2252
bool plot()
Definition matplotlibcpp.h:2860
void xlabel(const std::string &str, const std::map< std::string, std::string > &keywords={})
Definition matplotlibcpp.h:2431
void show(const bool block=true)
Definition matplotlibcpp.h:2542
void text(Numeric x, Numeric y, const std::string &s="")
Definition matplotlibcpp.h:1834
void ylabel(const std::string &str, const std::map< std::string, std::string > &keywords={})
Definition matplotlibcpp.h:2452
void grid(bool flag)
Definition matplotlibcpp.h:2525