在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添加图例。
辰迅云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
推荐阅读: c++中动态编译如何支持多种语言