返回资讯中心
外部精选
软件工程
#开发者实时流

开发者把开源项目交给Claude,所有issue全部修复

来源标题:I handed my open-source project to Claude, and it fixed every single issue

一位开发者将开源C++绘图库matplotlib-cpp交给Claude处理,结果所有issue均被修复。该库定位为最简单的C++绘图库,API模仿Matlab和matplotlib风格。

Hacker News 最新HotHotLava2026/11/9 · 今天28 分钟阅读

以下正文由 AI 翻译自 Hacker News 最新(原文版权归原站所有),可能存在偏差,重要内容请以原文为准。

欢迎来到 matplotlib-cpp,这可能是最简单的 C++ 绘图库。它的设计目标是模仿 Matlab 和 matplotlib 所使用的绘图 API。

#include "matplotlibcpp.h"
namespace plt = matplotlibcpp;

int main() {
    plt::plot({1,3,2,4});
    plt::show();
}
g++ minimal.cpp -std=c++11 $(python3-config --includes) $(python3 -c 'import numpy; print("-I"+numpy.get_include())') $(python3-config --ldflags --embed)

运行结果:(图)

#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);
    // 使用可选关键字参数添加文本标注。
    double txt_offset = 0.1;
    plt::text((450.0*450.0), 1+txt_offset, "Peak at (450^2, 1)",
              {{"fontsize","8"},{"backgroundcolor","lightgrey"},{"color","#3C7DBE"},{"verticalalignment","bottom"},{"horizontalalignment","center"}});
    // 根据给定的 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");
}
g++ basic.cpp -std=c++11 $(python3-config --includes) $(python3 -c 'import numpy; print("-I"+numpy.get_include())') $(python3-config --ldflags --embed)

运行结果:(图)

#include "../matplotlibcpp.h"
#include <string>
#include <vector>

namespace plt = matplotlibcpp;

int main()
{
    std::vector<std::string> x = {"Jan 2021", "Feb 2021", "Mar 2021"};
    std::vector<double> y = {58.4, 61.2, 63.9};
    plt::named_plot("Vehicle speed (max)", x, y, "o-");
    plt::legend();
    plt::save("./string_xaxis_named_plot.png");
}
g++ examples/string_xaxis_named_plot.cpp -std=c++11 -I. $(python3-config --includes) $(python3 -c 'import numpy; print("-I"+numpy.get_include())') $(python3-config --ldflags --embed)

预期行为与约束条件:

另外,matplotlib-cpp 还支持一些基于 C++11 的语法糖:

#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();
}
g++ modern.cpp -std=c++11 $(python3-config --includes) $(python3 -c 'import numpy; print("-I"+numpy.get_include())') $(python3-config --ldflags --embed)

运行结果:(图)

或者来一个滑稽的 xkcd 风格示例:

#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();
    plt::plot(t, x);
    plt::title("AN ORDINARY SIN WAVE");
    plt::save("xkcd.png");
}
g++ xkcd.cpp -std=c++11 $(python3-config --includes) $(python3 -c 'import numpy; print("-I"+numpy.get_include())') $(python3-config --ldflags --embed)

运行结果:(图)

处理向量场时,你可能会对箭头图(quiver plot)感兴趣:

#include "../matplotlibcpp.h"

namespace plt = matplotlibcpp;

int main()
{
    // u 和 v 分别是我们要绘制的箭头的 x 和 y 分量
    std::vector<int> x, y, u, v;
    for (int i = -5; i <= 5; i++) {
        for (int j = -5; j <= 5; j++) {
            x.push_back(i);
            u.push_back(-i);
            y.push_back(j);
            v.push_back(-j);
        }
    }

    plt::quiver(x, y, u, v);
    plt::show();
}
g++ quiver.cpp -std=c++11 $(python3-config --includes) $(python3 -c 'import numpy; print("-I"+numpy.get_include())') $(python3-config --ldflags --embed)

运行结果:(图)

处理三维函数时,你可能会对三维绘图感兴趣:

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

运行结果:(图)

matplotlib-cpp 的工作原理是对流行的 Python 绘图库 matplotlib 进行封装。(matplotlib.org)

这意味着你必须有一个可用的 Python 安装,包括开发头文件。

在 Ubuntu 上:

sudo apt-get install python3-matplotlib python3-numpy python3-dev

如果由于某种原因,你无法在系统上获得可用的 numpy 安装,可以在包含头文件之前定义宏 WITHOUT_NUMPY,以消除这一依赖。

该库的 C++ 部分仅由单个头文件 matplotlibcpp.h 组成,可以放在任何位置。

由于内部会启动一个 Python 解释器,因此使用 matplotlib-cpp 时必须链接 libpython。大多数版本应该都能正常工作,不过 Python 时不时会随意破坏兼容性,所以在使用最新版本时需要谨慎。

C++ 代码同时兼容 Python 2 和 Python 3。CMakeLists.txt 文件默认使用 Python 3;如需改为针对 Python 2 构建,请向 cmake 传递 -DUSING_PYTHON3=OFF。

(原文较长,此处译出前 6000 字,完整内容请点「打开原文」。)

正文由 探How 从来源站点 RSS 同步,内容未经改写;遇到排版缺失或需要图片、视频时请以原文为准。

打开原文
去社区讨论 →