【C++】开源:matplotlib-cpp静态图表库配置与使用

😏*★,°* :.☆( ̄▽ ̄)/$:.°★ 😏

这篇文章主要介绍matplotlib-cpp图表库配置与使用。
无专精则不能成,无涉猎则不能通。------梁启超

欢迎来到我的博客,一起学习,共同进步。

喜欢的朋友可以关注一下,下次更新不迷路🥞

文章目录

😏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. 环境配置

下面进行环境配置:

bash 复制代码
# 安装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

编译程序:

bash 复制代码
# ubuntu18
g++ -o main main.cpp -std=c++11 -I/usr/include/python2.7 -lpython2.7
# ubuntu20
g++ -o main main.cpp -std=c++11 -I/usr/include/python3.8 -lpython3.8

😆3. 使用说明

下面进行使用分析:

最简单的示例:

cpp 复制代码
#include "matplotlibcpp.h"
namespace plt = matplotlibcpp;
int main() {
    plt::plot({1,3,2,4});
    plt::show();
}

另一个复杂的示例,将图表保存为图片:

cpp 复制代码
#include "matplotlibcpp.h"
#include <cmath>

namespace plt = matplotlibcpp;

int main()
{
    // 数据处理
    int n = 5000;
    std::vector<double> x(n), y(n), z(n), w(n,2);
    for(int i=0; i<n; ++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");
}

一个三维图形示例:

cpp 复制代码
#include "matplotlibcpp.h"

namespace plt = matplotlibcpp;

int main()
{
    std::vector<std::vector<double>> x, y, z;
    for (double i = -5; i <= 5;  i += 0.25) {
        std::vector<double> 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();
}

读取txt文件中的xy两列数据并显示:

cpp 复制代码
#include <iostream>
#include <fstream>
#include "matplotlibcpp.h"

namespace plt = matplotlibcpp;

int main() {
    std::vector<double> x_data, y_data;
    double x, y;

    std::ifstream file("data.txt");
    if (file.is_open()) {
        while (file >> x >> y) {
            x_data.push_back(x);
            y_data.push_back(y);
        }
        file.close();
    } else {
        std::cerr << "Failed to open file." << std::endl;
        return 1;
    }

    // 绘制图表
    plt::plot(x_data, y_data);
    plt::show();
    
    return 0;
}

以上。

相关推荐
初级代码游戏2 分钟前
基于C++的IOT网关和平台1:github项目ctGateway
c++·物联网·github
YuforiaCode6 分钟前
第十六届蓝桥杯 2025 C/C++组 破解信息
c语言·c++·蓝桥杯
南玖yy7 分钟前
C++ 成员变量缺省值:引用、const 与自定义类型的初始化规则详解,引用类型和const类型的成员变量自定义类型成员是否可以用缺省值?
c语言·开发语言·c++·后端·架构·c++基础语法
YuforiaCode13 分钟前
第十六届蓝桥杯 2025 C/C++组 旗帜
c语言·c++·蓝桥杯
YuforiaCode15 分钟前
第十六届蓝桥杯 2025 C/C++B组 第二轮省赛 全部题解(未完结)
c语言·c++·蓝桥杯
keep intensify1 小时前
数据结构---单链表的增删查改
c语言·数据结构·c++·经验分享·学习·算法·分享
JoernLee2 小时前
Qwen3术语解密:读懂大模型黑话
人工智能·开源·llm
伊织code2 小时前
PocketBase - 单文件开源后端解决方案
后端·开源·go·api·文件·pocketbase
地平线开发者2 小时前
C++ 部署的性能优化方法
c++·算法·自动驾驶
Yingye Zhu(HPXXZYY)3 小时前
洛谷P12238 [蓝桥杯 2023 国 Java A] 单词分类
c++·算法·蓝桥杯