c++ matplotlib怎样添加图例

   2024-10-06 7240
核心提示:在C++中使用Matplotlib添加图例可以通过以下代码实现:#include iostream#include matplotlibcpp.hnamespace plt = matplotlibcp

在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添加图例。

 
举报打赏
 
更多>同类物流大全
推荐图文
推荐物流大全
点击排行

网站首页  |  关于我们  |  联系方式 | 网站留言    |  赣ICP备2021007278号