lava/matplotlib-cpp — 4.8k★ on GitHub (C++). Extremely simple yet powerful header-only C++ plotting library built on the popular matplotlib
Snapshot summary built from the project's own GitHub metadata — there's no written TopGit review yet. The page will update automatically when a full review is published.
WHY NO REVIEW YET
TopGit writes full reviews for the most-starred, most-requested repositories. This page is a snapshot until then — see the READ ME tab for the original README in full.
#include "matplotlibcpp.h"
#include <cmath>
namespace plt = matplotlibcpp;
int main()
{
// Prepare data.
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);
}
// Set the size of output image to 1200x780 pixels
plt::figure_size(1200, 780);
// Plot line from given x and y data. Color is selected automatically.
plt::plot(x, y);
// Plot a red dashed line from given x and y data.
plt::plot(x, w,"r--");
// Plot a line whose name will show up as "log(x)" in the legend.
plt::named_plot("log(x)", x, z);
// Set x-axis to interval [0,1000000]
plt::xlim(0, 1000*1000);
// Add graph title
plt::title("Sample figure");
// Enable legend.
plt::legend();
// Save the image (file format is determined by the extension)
plt::save("./basic.png");
}
x is a std::vector<std::string> and y is numeric with matching length.
String labels are mapped as categorical x values by matplotlib.
Use plt::save(...) for headless runs or plt::show() for interactive runs.
Alternatively, matplotlib-cpp also supports some C++11-powered syntactic sugar:
#include <cmath>
#include "matplotlibcpp.h"
using namespace std;
namespace plt = matplotlibcpp;
int main()
{
// Prepare data.
int n = 5000; // number of data points
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() takes an arbitrary number of (x,y,format)-triples.
// x must be iterable (that is, anything providing begin(x) and end(x)),
// y must either be callable (providing operator() const) or iterable.
plt::plot(x, y, "r-", x, [](double d) { return 12.5+abs(sin(d)); }, "k-");
// show plots
plt::show();
}
When working with vector fields, you might be interested in quiver plots:
#include "../matplotlibcpp.h"
namespace plt = matplotlibcpp;
int main()
{
// u and v are respectively the x and y components of the arrows we're plotting
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();
}
When working with 3d functions, you might be interested in 3d plots:
#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();
}
Result:
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:
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. The CMakeLists.txt file uses python3 by default; pass -DUSING_PYTHON3=OFF to cmake to build against python2 instead.
Pass -DMATPLOTLIB_CPP_BUILD_EXAMPLES=OFF or -DMATPLOTLIB_CPP_BUILD_EXAMPLES=ON to control whether the examples are built; they are off by default when matplotlib-cpp is used as a subproject.
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:
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.
Governance
This project is currently using automated open-source governance. Pull requests
are reviewed by an AI model against acceptance criteria written by the
maintainer. Each pull request receives one of four verdicts:
Merge: the pull request is merged as-is.
Merge with fix-ups: the change is wanted but needs small adjustments. An AI
agent applies them, pushes the result, and merges.
Reject without prejudice: the pull request is closed in its current form, for
example because it is broken, outdated, or mixes unrelated changes. You are
welcome to reopen or resubmit it.
Reject with prejudice: the change is out of scope for the project and should
not be resubmitted.
Every verdict comes with a comment explaining the decision. The acceptance criteria
themselves are not published.
The maintainer remains responsible for the project and can override any decision.
Using a version without AI-reviewed code
If you prefer not to use code that was accepted through this process, use commit
ef0383f
(2021-04-13). It is the last commit made before AI review was introduced. Every
change before it was reviewed and merged by a human.
git clone https://github.com/lava/matplotlib-cpp.git
cd matplotlib-cpp
git checkout ef0383f1315d32e0156335e10b82e90b334f6d9f
With CMake's FetchContent, set GIT_TAG ef0383f1315d32e0156335e10b82e90b334f6d9f.
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.
Todo/Issues/Wishlist
This library is not thread safe. Protect all concurrent access with a mutex.
Sadly, this is not easy to fix since it is not caused by the library itself but
by the python interpreter, which is itself not thread-safe.
It would be nice to have a more object-oriented design with a Plot class which would allow
multiple independent plots per program.
Right now, only a small subset of matplotlibs functionality is exposed. Stuff like xlabel()/ylabel() etc. should
be easy to add.
If you use Anaconda on Windows, you might need to set PYTHONHOME to Anaconda home directory and QT_QPA_PLATFORM_PLUGIN_PATH to %PYTHONHOME%Library/plugins/platforms. The latter is for especially when you get the error which says 'This application failed to start because it could not find or load the Qt platform plugin "windows"
in "".'
MacOS: Unable to import matplotlib.pyplot. Cause: In mac os image rendering back end of matplotlib (what-is-a-backend to render using the API of Cocoa by default). There is Qt4Agg and GTKAgg and as a back-end is not the default. Set the back end of macosx that is differ compare with other windows or linux os.
Solution is described here, additional information can be found there too(see links in answers).
The most recent commit recorded on lava/matplotlib-cpp was 9 days ago, based on the GitHub push timestamp. The repository has 1.2k forks — one of the better signals of community interest.
How many stars does lava/matplotlib-cpp have?
lava/matplotlib-cpp has 4.8k GitHub stars — refresh the page for the live number, or check github.com/lava/matplotlib-cpp. TopGit mirrors GitHub's count but does not claim minute-by-minute accuracy.
Is lava/matplotlib-cpp open source?
Yes — lava/matplotlib-cpp ships under the MIT license, which makes its source code freely readable (and, depending on license terms, forkable and reusable). Source: github.com/lava/matplotlib-cpp.
What is lava/matplotlib-cpp?
lava/matplotlib-cpp (lava/matplotlib-cpp) is a C++ project on GitHub. From the project's own README: Extremely simple yet powerful header-only C++ plotting library built on the popular matplotlib
What language is lava/matplotlib-cpp written in?
lava/matplotlib-cpp is written primarily in C++. GitHub's language field is based on the largest share of bytes in the default branch.
What license does lava/matplotlib-cpp use?
lava/matplotlib-cpp is released under the MIT license. Always verify the LICENSE file directly on GitHub for the authoritative terms — license strings can be edited out of sync with a project's actual stance.
Where do I read more about lava/matplotlib-cpp?
This TopGit page is a snapshot — the READ ME tab shows the project's own README content (links stripped, images preserved). The GitHub repository at github.com/lava/matplotlib-cpp is the definitive source.
Read full README in the tab above.
Is matplotlib-cpp worth your time?
ChatGPT, Claude and Perplexity can all read this page. Ask one of them what it makes of matplotlib-cpp.