由于matplotlibcpp的三维绘图功能十分有限,而课题需要三维可视化数据,因此最后放弃使用该库,转而使用matplot++。
引入头文件
#include "matplotlibcpp.h"
namespace plt = matplotlibcpp;
绘制散点图
#include <iostream>
#include <vector>
#include "matplotlibcpp.h"
namespace plt = matplotlibcpp;
struct Point {
double x, y;
Point(double x_, double y_) : x(x_), y(y_) {}
};
int main() {
std::vector<Point> control_points = {
{0.0, 0.0}, {1.0, 2.0}, {2.0, 3.0}, {3.0, 1.0}, {4.0, 0.0}
};
std::vector<double> x, y;
for (const auto& p : control_points) {
x.push_back(p.x);
y.push_back(p.y);
}
std::map<std::string, std::string> keywords;
keywords["color"] = "red";
keywords["marker"] = "o";
plt::scatter(x, y, 50, keywords);
plt::title("Control Points with Color");
plt::xlabel("x");
plt::ylabel("y");
plt::show();
return 0;
}
设置图例和网格
plt::named_plot("Control Points", x, y, "ro");
plt::named_plot("Bezier Curve", x_line, y_line, "b--");
plt::legend();
plt::grid(true);
设置线的颜色和样式
std::vector<double> x_line = {0.0, 1.0, 2.0, 3.0, 4.0};
std::vector<double> y_line = {0.0, 2.0, 3.0, 1.0, 0.0};
keywords["color"] = "blue";
keywords["linestyle"] = "--";
plt::plot(x_line, y_line, keywords);