C++ third-party development library matplotlib-cpp

Matplotlib-cpp is an open source library for drawing graphs in C++. It provides similar functions to Python's Matplotlib library, making data visualization in a C++ environment more convenient. Based on Matplotlib-cpp, we can use various plotting functions and style options to create various types of charts, including line charts, scatter plots, histograms, etc. It is closely integrated with the syntax and data structure of C++, which facilitates chart drawing and data analysis in C++ projects.

1. Installation

Download matplotlib-cpp

git clone https://github.com/lava/matplotlib-cpp.git

2. Basic functions

matplotlib-cpp mainly provides the following functions:

  • Basic drawing: matplotlib-cpp supports many basic 2D graphics, including line graphs, scatter plots, bar graphs, histograms, pie charts, etc.

  • Subpictures: Multiple subpictures can be created on a canvas, and each subpicture can be drawn independently.

  • Colors, markers, and line styles: You can customize the colors, markers (shapes of data points), and line styles (solid, dashed, etc.) of each graph.

  • Legend and title: You can add a legend and title to each graph to explain what the graph means.

  • Axis settings: You can customize the range, scale, and labels of the axis.

  • Grid lines: Grid lines can be added to make it easier to observe the data.

  • Text and labels: Text and labels can be added to graphs to emphasize specific data points or areas.

  • Save and display: Graphics can be saved as files in various formats or displayed directly in the window.

3. Example

#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);
    }

    // 设置输出图像的大小为1200x780像素
    plt::figure_size(1200, 780);
    // 绘制给定x和y数据的折线图,颜色自动选择
    plt::plot(x, y);
    // 绘制给定x和y数据的红色虚线
    plt::plot(x, w, "r--");
    // 绘制一条线,其名称将出现在图例中为"log(x)"
    plt::named_plot("log(x)", x, z);
    // 设置x轴的范围为[0,1000000]
    plt::xlim(0, 1000*1000);
    // 添加图表标题
    plt::title("Sample figure");
    // 启用图例
    plt::legend();
    // 保存图像(文件格式由扩展名确定)
    plt::save("./basic.png");

    return 0;
}

 

#include <cmath>
#include "matplotlibcpp.h"

using namespace std;
namespace plt = matplotlibcpp;

int main()
{
    // 准备数据
    int n = 5000; // 数据点个数
    vector<double> x(n),y(n);
    for(int i=0; i<n; ++i) {
        double t = 2*M_PI*i/n;
        x.at(i) = 16*sin(t)*sin(t)*sin(t);
        y.at(i) = 13*cos(t) - 5*cos(2*t) - 2*cos(3*t) - cos(4*t);
    }

    // plot() 函数接受任意数量的 (x, y, format) 三元组
    // x 必须是可迭代的(即提供 begin(x) 和 end(x) 函数),
    // y 可以是可调用的(提供 operator() const 函数)或者可迭代的
    plt::plot(x, y, "r-", x, [](double d) { return 12.5+abs(sin(d)); }, "k-");

    // 显示图表
    plt::show();
}

#include "matplotlibcpp.h"
#include <vector>
#include <cmath>

namespace plt = matplotlibcpp;

int main() {
    std::vector<double> t(1000);
    std::vector<double> x(t.size());

    for(size_t i = 0; i < t.size(); i++) {
        t[i] = i / 100.0;
        x[i] = sin(2.0 * M_PI * 1.0 * t[i]);
    }

    plt::xkcd(); // 应用xkcd风格的绘图
    plt::plot(t, x);
    plt::title("AN ORDINARY SIN WAVE"); // 设置图表标题
    plt::save("xkcd.png"); // 保存图像为"xkcd.png"
}

 

#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();
}

references

The best choice for C++ charting: efficient and powerful

C++ third-party development library matplotlib-cpp 

Guess you like

Origin blog.csdn.net/xhtchina/article/details/131915565