c++matplotlibcpp基本操作

由于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);

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值