贵州省城乡建设厅网站首页,西宁百度推广公司电话,网上花店网页制作代码,搜索引擎优化 简历#x1f60f;★,:.☆(#xffe3;▽#xffe3;)/$:.★ #x1f60f; 这篇文章主要介绍matplotlib-cpp图表库配置与使用。 无专精则不能成#xff0c;无涉猎则不能通。——梁启超 欢迎来到我的博客#xff0c;一起学习#xff0c;共同进步。 喜欢的朋友可以关注一下★,°:.☆(▽)/$:.°★ 这篇文章主要介绍matplotlib-cpp图表库配置与使用。 无专精则不能成无涉猎则不能通。——梁启超 欢迎来到我的博客一起学习共同进步。 喜欢的朋友可以关注一下下次更新不迷路 文章目录 :smirk:1. 项目介绍:blush:2. 环境配置:satisfied:3. 使用说明 1. 项目介绍
项目Github地址https://github.com/lava/matplotlib-cpp
matplotlib-cpp 是一个用于 C 的简易接口它允许你在 C 程序中使用 Python 的 matplotlib 库来绘制图表。这个库提供了一个类似于 matplotlib 的 API使得在 C 中生成各种类型的图表变得更加简单和方便。
以下是 matplotlib-cpp 的一些主要特点和功能 1.轻量级matplotlib-cpp 是一个轻量级的库只包含少量的头文件并且没有其他的依赖项。这使得它很容易集成到你的项目中。 2.简单易用matplotlib-cpp 提供了与 matplotlib 类似的函数和方法使得在 C 中绘制图表变得直观和易于理解。你可以使用类似于 Python 的语法来创建图表、设置图表属性和保存图表。 3.支持多种图表类型matplotlib-cpp 支持绘制多种类型的图表包括线图、散点图、柱状图、饼图、等高线图等。你可以选择适合你数据展示需求的图表类型。 4.支持自定义设置你可以自定义图表的各种属性如标题、标签、坐标轴范围、图例、颜色等。这样你可以根据具体需求来设计和美化图表。 5.与 Python 的无缝集成使用 matplotlib-cpp你可以在 C 代码中调用 Python 的 matplotlib 库来生成图表。这使得你可以利用 Python 在图表方面丰富的生态系统和强大的功能来扩展你的 C 应用程序。 2. 环境配置
下面进行环境配置
# 安装python包
sudo apt install python3 python3-pip
pip3 install matplotlib numpy
# 源码编译
git clone https://github.com/lava/matplotlib-cpp.git
cd matplotlib-cpp
mkdir build cd build
cmake ..
make
sudo make install编译程序
# ubuntu18
g -o main main.cpp -stdc11 -I/usr/include/python2.7 -lpython2.7
# ubuntu20
g -o main main.cpp -stdc11 -I/usr/include/python3.8 -lpython3.83. 使用说明
下面进行使用分析
最简单的示例
#include matplotlibcpp.h
namespace plt matplotlibcpp;
int main() {plt::plot({1,3,2,4});plt::show();
}另一个复杂的示例将图表保存为图片
#include matplotlibcpp.h
#include cmathnamespace plt matplotlibcpp;int main()
{// 数据处理int n 5000;std::vectordouble x(n), y(n), z(n), w(n,2);for(int i0; in; i) {x.at(i) i*i;y.at(i) sin(2*M_PI*i/360.0);z.at(i) log(i);}// 设置分辨率plt::figure_size(1200, 780);// Plot line from given x and y data. Color is selected automatically.plt::plot(x, y);// Plot a red dashed line from given x and y data.plt::plot(x, w,r--);// Plot a line whose name will show up as log(x) in the legend.plt::named_plot(log(x), x, z);// 设置x轴plt::xlim(0, 1000*1000);// 图表标题plt::title(Sample figure);// 添加图例plt::legend();// 保存为照片plt::save(./basic.png);
}一个三维图形示例
#include matplotlibcpp.hnamespace plt matplotlibcpp;int main()
{std::vectorstd::vectordouble x, y, z;for (double i -5; i 5; i 0.25) {std::vectordouble x_row, y_row, z_row;for (double j -5; j 5; j 0.25) {x_row.push_back(i);y_row.push_back(j);z_row.push_back(::std::sin(::std::hypot(i, j)));}x.push_back(x_row);y.push_back(y_row);z.push_back(z_row);}plt::plot_surface(x, y, z);plt::show();
}以上。