Skip to content
TheChatArchiveThe Chat Archive
o3-2025-04-16· OpenAIHugging Face LMArena Human Preference 140K

Is there an alternative to matplotlib in c++?

From a Chatbot Arena battleThe same person sent this prompt to two anonymous models at once. Compare it with Claudeclaude-3-5-haiku-20241022’s answer.
  1. User

    Is there an alternative to matplotlib in c++?

  2. Assistant

    Absolutely—there are quite a few plotting / data‑visualization libraries you can use from C++ instead of (or in addition to) Python’s matplotlib. Which one is “best” depends on what you really need (static vs. interactive plots, GUI vs. headless, licensing, build system, etc.), but the following list should give you a good overview.

    🗺️ Quick map of the landscape

    CategoryLibraryOne‑liner descriptionLicense
    Thin wrappers around Python/gnuplotmatplotlib‑cpp, gnuplot‑iostreamSmall header‑only wrappers that talk to Python’s matplotlib or the gnuplot executable. Very lightweight.BSD / Boost
    C++‑native plotting enginesMathGL, PLplot, ImPlot, QCustomPlot / QtCharts, ROOTFully written in C/C++; no Python runtime needed. Range from immediate‑mode (ImPlot) to heavy scientific suites (ROOT).GPL/BSD/MIT
    Full visualization toolkitsVTK, OpenSceneGraph, ParaView (client)2‑D/3‑D scientific visualisation; more than just “plotting”.BSD/MIT
    Embedded GUI Gamedev libsSFML Graphics, SDL + ImGui/ImPlotGood for live, animated plotting in an app/game style.zlib/libpng, MIT
    Web assembliesEmscripten + Plotly.js, Cpp‑Rest + VegaC++ side prepares data, JavaScript does the drawing in a browser.MIT

    🧰 A bit more detail & code snippets

    1. matplotlib‑cpp • Header‑only wrapper (≈400 LOC).
      • Requires Python & matplotlib installed and linked via python3-dev.
    #include "matplotlibcpp.h"
    namespace plt = matplotlibcpp;
    
    int main() {
        std::vector<double> x = {0,1,2,3,4};
        std::vector<double> y = {0,1,4,9,16};
        plt::plot(x, y, "r-");
        plt::xlabel("x");
        plt::ylabel("x^2");
        plt::save("parabola.png");
    }
    

    Pros: almost a drop‑in C++ syntax copy of matplotlib.
    Cons: you’re still shipping Python.

    1. gnuplot‑iostream • Streams data to the gnuplot binary through a pipe.
      • Zero Python dependencies, but users need gnuplot installed.
    #include "gnuplot-iostream.h"
    int main() {
        Gnuplot gp;
        std::vector<std::pair<double,double>> data;
        for(int i=0;i<50;++i) data.push_back({i, std::sin(i/5.0)});
        gp << "set terminal pngcairo size 800,600\n";
        gp << "set output 'sin.png'\n";
        gp << "plot '-' w l title 'sin(x)'\n";
        gp.send1d(data);
    }
    
    1. MathGL (mgl2) • C / C++ library; 2‑D & 3‑D; OpenGL or cairo backend.
      • Comes with its own small GUI (Qt) viewer and LaTeX‑style math markup.
    #include <mgl2/mgl.h>
    int main() {
        mglGraph gr;
        mglData y(100); y.Modify("sin(pi*x)");
        gr.SetRange('x', 0, 1);
        gr.Axis(); gr.Plot(y,"b");
        gr.WritePNG("sin_mathgl.png");
    }
    
    1. ImPlot (built on Dear ImGui) • MIT license, immediate‑mode; great for realtime, in‑app plots.
      • Needs your own rendering loop. Works well with GLFW + OpenGL.
    if (ImGui::Begin("MyPlot")) {
        static float xs[100], ys[100];
        for (int i=0;i<100;++i) { xs[i] = i*0.1f; ys[i] = sinf(xs[i]); }
        if (ImPlot::BeginPlot("Sine")) {
            ImPlot::PlotLine("sin(x)", xs, ys, 100);
            ImPlot::EndPlot();
        }
    }
    ImGui::End();
    
    1. QCustomPlot / QtCharts • Rich, interactive widgets if you are already in the Qt ecosystem.
      • Drag‑zoom, axis linking, export to SVG/PDF built‑in.

    2. ROOT (from CERN) • Heavy scientific framework (histograms, fitting, 3‑D).
      • Excellent for HEP‑style data analysis; steep learning curve.
      • Example: TCanvas, TGraph, TH1F.

    3. PLplot • Mature (since the 1990s), cross‑language.
      • Works headless; drivers for X11, PDF, SVG, …

    4. VTK (Visualization Toolkit) • If you need volume rendering, vector fields, iso‑surfaces plus 2‑D charts.
      • Powerful but bigger than “just plot some lines”.

    🏃‍♂️ How to choose quickly

    • I only need static PNG/SVG and I’m okay shipping Python ⇒ matplotlib‑cpp.
    • I want no Python, very light, command‑line only ⇒ gnuplot‑iostream.
    • Interactive GUI inside my C++ Qt app ⇒ QCustomPlot / QtCharts.
    • Game‑loop / realtime dashboards ⇒ ImPlot (ImGui).
    • Heavy science / histograms / ROOT files ⇒ ROOT.
    • Cross‑platform, printed‑quality vector graphics ⇒ PLplot or MathGL.
    • 3‑D fields, meshes, volume ⇒ VTK.

    🔧 Build & package tips

    • Many of these libraries are available via vcpkg or Conan (vcpkg install implot, conan install plplot/...).
    • For header‑only libs (matplotlib‑cpp, gnuplot‑iostream) you mostly just #include and link against the dependent runtime (Python or gnuplot executable).
    • Watch for GPL vs MIT/BSD if your project is proprietary (MathGL is GPL, PLplot LGPL w/ exceptions, QCustomPlot has dual GPL/commercial).

    🚀 TL;DR If you like the matplotlib API but want to stay in C++, start with matplotlib‑cpp. If you’d rather not depend on Python, look at gnuplot‑iostream (cli) or ImPlot (GUI). For a full‑blown scientific environment there’s ROOT and VTK, and for Qt applications QCustomPlot or QtCharts integrate very smoothly.

    Pick the one whose trade‑offs (dependencies, license, interactivity level) match your project, and you’ll be graphing happily in no time!

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.