Matplotlib-cpp在linux中C++代码中调用画图

Matplotlib-cpp是一个使用C++实现的调用Matplotlib接口进行绘图的库,非常好用。

代码库地址:https://github.com/Cryoris/matplotlib-cpp

首先是安装库,需要安装系统对应版本的python,查看python版本后:

sudo apt-get install python3.8-dev

 再CMakelists中添加

find_package(PythonLibs REQUIRED)
include_directories(
        ${PYTHON_INCLUDE_DIRS}
)
target_link_libraries(自己可执行文件名
        ${PYTHON_LIBRARIES}
        )

并且将matplotlibcpp.h 文件添加到头文件目录即可

#include "hybrid_a_star/hybrid_a_star.h"

#include "hybrid_a_star/display_tools.h"

#include "hybrid_a_star/timer.h"

#include "hybrid_a_star/trajectory_optimizer.h"

#include <iostream>

#include "matplotlib-cpp/matplotlibcpp.h"

2 三维在线旋转变换网站

https://www.andre-gaschler.com/rotationconverter/

Quaternions - Visualisation

Rotation Tool

3.判断点是否在多边形内的方法

Determining if a point lies on the interior of a polygon

C++调用画图功能有多种方法,以下为你介绍常见的几种: ### 使用 Matplotlib C++ 接口 可以借助 `matplotlib-cpp` 库来实现 C++ 调用 `Matplotlib` 的绘图功能。以下是一个示例代码,用于动态显示图片: ```cpp #define _USE_MATH_DEFINES #include <cmath> #include "../matplotlibcpp.h" namespace plt = matplotlibcpp; int main() { int n = 1000; std::vector<double> x, y, z; for(int i=0; i<n; i++) { x.push_back(i*i); y.push_back(sin(2*M_PI*i/360.0)); z.push_back(log(i)); if (i % 10 == 0) { // Clear previous plot plt::clf(); // Plot line from given x and y data. Color is selected automatically. plt::plot(x, y); // Plot a line whose name will show up as "log(x)" in the legend. plt::named_plot("log(x)", x, z); // Set x-axis to interval [0,1000000] plt::xlim(0, n*n); // Add graph title plt::title("Sample figure"); // Enable legend. plt::legend(); // Display plot continuously plt::pause(0.01); } } } ``` 此代码通过 `matplotlib-cpp` 库绘制了两条曲线,并且动态显示图形。需要注意的是,图像默认会保存下来,若要显示图像,需要调用 `plt.show()` [^1]。 ### 调用 Python 画图 C++ 可以通过调用 Python 脚本来实现画图功能。首先要确保系统中安装了 Python 以及相应的绘图库(如 `matplotlib`),然后使用 C++Python 扩展模块来调用 Python 代码。示例如下: ```python # 测试函数 def add(a,b): print "in python fuinction" print 'a='+str(a) print 'b='+str(b) print 'result='+str(a+b) return ``` 在 C++调用这个 Python 函数,能够实现相应的功能。 ### 使用图形库 还可以使用一些专门的 C++ 图形库,像 `OpenGL`、`Qt` 等。以 `Qt` 为例,以下是一个简单的绘制矩形的示例: ```cpp #include <QApplication> #include <QWidget> #include <QPainter> class MyWidget : public QWidget { public: void paintEvent(QPaintEvent *event) override { QPainter painter(this); painter.drawRect(10, 10, 100, 100); } }; int main(int argc, char *argv[]) { QApplication app(argc, argv); MyWidget widget; widget.show(); return app.exec(); } ``` 这个示例使用 `Qt` 库创建了一个窗口,并在窗口中绘制了一个矩形。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值