Installation
matplotlib-cpp works by wrapping the popular python plotting library matplotlib. (matplotlib.org) This means you have to have a working python installation, including development headers. On Ubuntu:
sudo apt-get install python-matplotlib python-numpy python2.7-dev
If, for some reason, you're unable to get a working installation of numpy on your system, you can define the macro WITHOUT_NUMPY before including the header file to erase this dependency.
The C++-part of the library consists of the single header file matplotlibcpp.h which can be placed anywhere.
Since a python interpreter is opened internally, it is necessary to link against libpython in order to user matplotlib-cpp. Most versions should work, although python likes to randomly break compatibility from time to time so some caution is advised when using the bleeding edge.
CMake
The C++ code is compatible to both python2 and python3. However, the CMakeLists.txt file is currently set up to use python3 by default, so if python2 is required this has to be changed manually. (a PR that adds a cmake option for this would be highly welcomed)
NOTE: By design (of python), only a single python interpreter can be created per process. When using this library, no other library that is spawning a python interpreter internally can be used.
To compile the code without using cmake, the compiler invocation should look like this:
g++ example.cpp -I/usr/include/python2.7 -lpython2.7
This can also be used for linking against a custom build of python
g++ example.cpp -I/usr/local/include/fancy-python4 -L/usr/local/lib -lfancy-python4
Vcpkg
You can download and install matplotlib-cpp using the vcpkg dependency manager:
git clone
https://github.com/Microsoft/vcpkg.git
cd vcpkg
./bootstrap-vcpkg.sh
./vcpkg integrate install
vcpkg install matplotlib-cpp
The matplotlib-cpp port in vcpkg is kept up to date by Microsoft team members and community contributors. If the version is out of date, please create an issue or pull request on the vcpkg repository.
C++11
Currently, c++11 is required to build matplotlib-cpp. The last working commit that did not have this requirement was 717e98e752260245407c5329846f5d62605eff08.
Note that support for c++98 was dropped more or less accidentally, so if you have to work with an ancient compiler and still want to enjoy the latest additional features, I'd probably merge a PR that restores support.
Why?
I initially started this library during my diploma thesis. The usual approach of writing data from the c++ algorithm to a file and afterwards parsing and plotting it in python using matplotlib proved insufficient: Keeping the algorithm and plotting code in sync requires a lot of effort when the C++ code frequently and substantially changes. Additionally, the python yaml parser was not able to cope with files that exceed a few hundred megabytes in size.
Therefore, I was looking for a C++ plotting library that was extremely easy to use and to add into an existing codebase, preferably header-only. When I found none, I decided to write one myself, which is basically a C++ wrapper around matplotlib. As you can see from the above examples, plotting data and saving it to an image file can be done as few as two lines of code.
The general approach of providing a simple C++ API for utilizing python code was later generalized and extracted into a separate, more powerful library in another project of mine, wrappy.