温馨提示×

c++ matplotlib怎样添加图例

c++
小樊
90
2024-07-21 14:11:01
栏目: 编程语言

在C++中使用Matplotlib添加图例可以通过以下代码实现:

#include <iostream>
#include "matplotlibcpp.h"

namespace plt = matplotlibcpp;

int main() {
    std::vector<double> x = {1, 2, 3, 4, 5};
    std::vector<double> y = {10, 15, 13, 18, 16};

    plt::plot(x, y, "b-", "label", "Line 1");
    plt::legend();
    plt::show();

    return 0;
}

在上面的代码中,我们首先创建了一个包含x和y值的向量,然后使用plt::plot()函数绘制了这些值的折线图,并通过添加"label", "Line 1"参数来指定图例的标签为"Line 1"。最后调用plt::legend()函数来添加图例,并调用plt::show()函数显示绘制的图形。

通过这种方式,可以在C++中使用Matplotlib添加图例。

0