explaingit

lava/matplotlib-cpp

4,755C++Audience · researcherComplexity · 2/5Setup · moderate

TLDR

A single-header C++ library that lets you draw charts and graphs from inside a C++ program by calling Python's matplotlib under the hood, with syntax nearly identical to Python or MATLAB charting code.

Mindmap

mindmap
  root((matplotlib-cpp))
    What it does
      Charts from C++ code
      Calls Python matplotlib
    Chart types
      Line and scatter plots
      3D surface plots
      Arrow plots
      xkcd hand-drawn style
    Setup
      Single header file
      Python dependency
      CMake build files
    Limitation
      One Python session
      per process only
Click or tap to explore — scroll the page freely

Code map

Detail Auto

An interactive map of this repo's files and how they connect — its source is parsed live in your browser. Click Visualize to build it.

filefunction / class

Things people build with this

USE CASE 1

Plot real-time results from a running C++ algorithm without saving data to a file first

USE CASE 2

Add line charts and scatter plots to a C++ numerical simulation with two lines of code

USE CASE 3

Visualize C++ algorithm output during development without switching to Python or MATLAB

USE CASE 4

Create 3D surface plots from C++ scientific computing data

Tech stack

C++PythonmatplotlibCMake

Getting it running

Difficulty · moderate Time to first run · 30min

Requires Python 3 and matplotlib installed on the same machine, you must link your C++ compiler against the Python runtime, which adds CMake configuration steps.

In plain English

matplotlib-cpp is a C++ library that lets you draw charts and graphs from inside a C++ program. It works by calling Python's matplotlib library behind the scenes, so it requires Python to be installed on the same machine alongside a few Python packages. The library itself is just one file you drop into your project, with no complicated build system required. To use it, you include the single header file in your C++ code and write chart commands that look very similar to how you would draw charts in Python or MATLAB. A simple line chart can be created in two lines of code. The library supports many chart types: line plots, scatter plots, 3D surface plots, arrow plots, and even an xkcd hand-drawn style. Setting it up takes a bit more work than a typical C++ library because of the Python dependency. On Ubuntu or a similar Linux system you install a few Python packages, then link your C++ compiler against the Python runtime. CMake build files are included to help with this. The library works with both Python 2 and Python 3, though Python 3 is the default in the build configuration. One limitation to be aware of: because it starts a Python interpreter internally, you cannot use any other library in the same program that also starts its own Python interpreter. Only one Python session can run per process. The project was created during a graduate thesis when the author needed to view charts while a C++ algorithm was still running, rather than saving data to a file first and plotting it separately afterward. That origin explains the focus on simplicity: the goal was the smallest possible gap between having numbers in C++ and seeing a chart on screen.

Copy-paste prompts

Prompt 1
I'm writing a C++ sorting algorithm and want to plot how comparison count changes over input size as the algorithm runs. Show me how to use matplotlib-cpp to draw a live-updating line chart.
Prompt 2
Add a 3D surface plot to my C++ simulation using matplotlib-cpp, show what headers to include, what data structure to pass, and what the plot call looks like.
Prompt 3
Write a CMakeLists.txt that compiles a C++ project using matplotlib-cpp and links it against Python 3 on Ubuntu.
Prompt 4
How do I plot multiple data series on the same chart with matplotlib-cpp, using different colors and adding a legend?
Prompt 5
What is the one-Python-interpreter limitation of matplotlib-cpp and what breaks if I also use another library that embeds Python in the same program?
Open on GitHub → Explain another repo

← lava on gitmyhub — every repo by this author, as a profile.

Verify against the repo before relying on details.