{"id":13418403,"url":"https://github.com/lava/matplotlib-cpp","last_synced_at":"2025-04-12T03:44:12.969Z","repository":{"id":21616000,"uuid":"24936365","full_name":"lava/matplotlib-cpp","owner":"lava","description":"Extremely simple yet powerful header-only C++ plotting library built on the popular matplotlib","archived":false,"fork":false,"pushed_at":"2023-11-21T15:07:11.000Z","size":861,"stargazers_count":4578,"open_issues_count":225,"forks_count":1169,"subscribers_count":114,"default_branch":"master","last_synced_at":"2025-04-12T03:44:08.731Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"C++","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/lava.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null}},"created_at":"2014-10-08T10:48:27.000Z","updated_at":"2025-04-10T06:38:17.000Z","dependencies_parsed_at":"2023-02-11T19:30:45.779Z","dependency_job_id":"4bbf800c-22ff-460b-9f85-0b417fe96172","html_url":"https://github.com/lava/matplotlib-cpp","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lava%2Fmatplotlib-cpp","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lava%2Fmatplotlib-cpp/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lava%2Fmatplotlib-cpp/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lava%2Fmatplotlib-cpp/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/lava","download_url":"https://codeload.github.com/lava/matplotlib-cpp/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248514214,"owners_count":21116899,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-07-30T22:01:01.884Z","updated_at":"2025-04-12T03:44:12.949Z","avatar_url":"https://github.com/lava.png","language":"C++","readme":"matplotlib-cpp\n==============\n\nWelcome to matplotlib-cpp, possibly the simplest C++ plotting library.\nIt is built to resemble the plotting API used by Matlab and matplotlib.\n\n\n\nUsage\n-----\nComplete minimal example:\n```cpp\n#include \"matplotlibcpp.h\"\nnamespace plt = matplotlibcpp;\nint main() {\n    plt::plot({1,3,2,4});\n    plt::show();\n}\n```\n    g++ minimal.cpp -std=c++11 -I/usr/include/python2.7 -lpython2.7\n\n**Result:**\n\n![Minimal example](./examples/minimal.png)\n\nA more comprehensive example:\n```cpp\n#include \"matplotlibcpp.h\"\n#include \u003ccmath\u003e\n\nnamespace plt = matplotlibcpp;\n\nint main()\n{\n    // Prepare data.\n    int n = 5000;\n    std::vector\u003cdouble\u003e x(n), y(n), z(n), w(n,2);\n    for(int i=0; i\u003cn; ++i) {\n        x.at(i) = i*i;\n        y.at(i) = sin(2*M_PI*i/360.0);\n        z.at(i) = log(i);\n    }\n\n    // Set the size of output image to 1200x780 pixels\n    plt::figure_size(1200, 780);\n    // Plot line from given x and y data. Color is selected automatically.\n    plt::plot(x, y);\n    // Plot a red dashed line from given x and y data.\n    plt::plot(x, w,\"r--\");\n    // Plot a line whose name will show up as \"log(x)\" in the legend.\n    plt::named_plot(\"log(x)\", x, z);\n    // Set x-axis to interval [0,1000000]\n    plt::xlim(0, 1000*1000);\n    // Add graph title\n    plt::title(\"Sample figure\");\n    // Enable legend.\n    plt::legend();\n    // Save the image (file format is determined by the extension)\n    plt::save(\"./basic.png\");\n}\n```\n    g++ basic.cpp -I/usr/include/python2.7 -lpython2.7\n\n**Result:**\n\n![Basic example](./examples/basic.png)\n\nAlternatively, matplotlib-cpp also supports some C++11-powered syntactic sugar:\n```cpp\n#include \u003ccmath\u003e\n#include \"matplotlibcpp.h\"\n\nusing namespace std;\nnamespace plt = matplotlibcpp;\n\nint main()\n{\n    // Prepare data.\n    int n = 5000; // number of data points\n    vector\u003cdouble\u003e x(n),y(n);\n    for(int i=0; i\u003cn; ++i) {\n        double t = 2*M_PI*i/n;\n        x.at(i) = 16*sin(t)*sin(t)*sin(t);\n        y.at(i) = 13*cos(t) - 5*cos(2*t) - 2*cos(3*t) - cos(4*t);\n    }\n\n    // plot() takes an arbitrary number of (x,y,format)-triples.\n    // x must be iterable (that is, anything providing begin(x) and end(x)),\n    // y must either be callable (providing operator() const) or iterable.\n    plt::plot(x, y, \"r-\", x, [](double d) { return 12.5+abs(sin(d)); }, \"k-\");\n\n\n    // show plots\n    plt::show();\n}\n```\n    g++ modern.cpp -std=c++11 -I/usr/include/python2.7 -lpython\n\n**Result:**\n\n![Modern example](./examples/modern.png)\n\nOr some *funny-looking xkcd-styled* example:\n```cpp\n#include \"matplotlibcpp.h\"\n#include \u003cvector\u003e\n#include \u003ccmath\u003e\n\nnamespace plt = matplotlibcpp;\n\nint main() {\n    std::vector\u003cdouble\u003e t(1000);\n    std::vector\u003cdouble\u003e x(t.size());\n\n    for(size_t i = 0; i \u003c t.size(); i++) {\n        t[i] = i / 100.0;\n        x[i] = sin(2.0 * M_PI * 1.0 * t[i]);\n    }\n\n    plt::xkcd();\n    plt::plot(t, x);\n    plt::title(\"AN ORDINARY SIN WAVE\");\n    plt::save(\"xkcd.png\");\n}\n\n```\n    g++ xkcd.cpp -std=c++11 -I/usr/include/python2.7 -lpython2.7\n\n**Result:**\n\n![xkcd example](./examples/xkcd.png)\n\nWhen working with vector fields, you might be interested in quiver plots:\n```cpp\n#include \"../matplotlibcpp.h\"\n\nnamespace plt = matplotlibcpp;\n\nint main()\n{\n    // u and v are respectively the x and y components of the arrows we're plotting\n    std::vector\u003cint\u003e x, y, u, v;\n    for (int i = -5; i \u003c= 5; i++) {\n        for (int j = -5; j \u003c= 5; j++) {\n            x.push_back(i);\n            u.push_back(-i);\n            y.push_back(j);\n            v.push_back(-j);\n        }\n    }\n\n    plt::quiver(x, y, u, v);\n    plt::show();\n}\n```\n    g++ quiver.cpp -std=c++11 -I/usr/include/python2.7 -lpython2.7\n\n**Result:**\n\n![quiver example](./examples/quiver.png)\n\nWhen working with 3d functions, you might be interested in 3d plots:\n```cpp\n#include \"../matplotlibcpp.h\"\n\nnamespace plt = matplotlibcpp;\n\nint main()\n{\n    std::vector\u003cstd::vector\u003cdouble\u003e\u003e x, y, z;\n    for (double i = -5; i \u003c= 5;  i += 0.25) {\n        std::vector\u003cdouble\u003e x_row, y_row, z_row;\n        for (double j = -5; j \u003c= 5; j += 0.25) {\n            x_row.push_back(i);\n            y_row.push_back(j);\n            z_row.push_back(::std::sin(::std::hypot(i, j)));\n        }\n        x.push_back(x_row);\n        y.push_back(y_row);\n        z.push_back(z_row);\n    }\n\n    plt::plot_surface(x, y, z);\n    plt::show();\n}\n```\n\n**Result:**\n\n![surface example](./examples/surface.png)\n\nInstallation\n------------\n\nmatplotlib-cpp works by wrapping the popular python plotting library matplotlib. (matplotlib.org)\nThis means you have to have a working python installation, including development headers.\nOn Ubuntu:\n\n    sudo apt-get install python-matplotlib python-numpy python2.7-dev\n\nIf, for some reason, you're unable to get a working installation of numpy on your system,\nyou can define the macro `WITHOUT_NUMPY` before including the header file to erase this\ndependency.\n\nThe C++-part of the library consists of the single header file `matplotlibcpp.h` which\ncan be placed anywhere.\n\nSince a python interpreter is opened internally, it is necessary to link\nagainst `libpython` in order to user matplotlib-cpp. Most versions should\nwork, although python likes to randomly break compatibility from time to time\nso some caution is advised when using the bleeding edge.\n\n\n# CMake\n\nThe C++ code is compatible to both python2 and python3. However, the `CMakeLists.txt`\nfile is currently set up to use python3 by default, so if python2 is required this\nhas to be changed manually. (a PR that adds a cmake option for this would be highly\nwelcomed)\n\n**NOTE**: By design (of python), only a single python interpreter can be created per\nprocess. When using this library, *no other* library that is spawning a python\ninterpreter internally can be used.\n\nTo compile the code without using cmake, the compiler invocation should look like\nthis:\n\n    g++ example.cpp -I/usr/include/python2.7 -lpython2.7\n\nThis can also be used for linking against a custom build of python\n\n    g++ example.cpp -I/usr/local/include/fancy-python4 -L/usr/local/lib -lfancy-python4\n\n# Vcpkg\n\nYou can download and install matplotlib-cpp using the [vcpkg](https://github.com/Microsoft/vcpkg) dependency manager:\n\n    git clone https://github.com/Microsoft/vcpkg.git\n    cd vcpkg\n    ./bootstrap-vcpkg.sh\n    ./vcpkg integrate install\n    vcpkg install matplotlib-cpp\n  \nThe 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](https://github.com/Microsoft/vcpkg) on the vcpkg repository.\n\n\n# C++11\n\nCurrently, c++11 is required to build matplotlib-cpp. The last working commit that did\nnot have this requirement was `717e98e752260245407c5329846f5d62605eff08`.\n\nNote that support for c++98 was dropped more or less accidentally, so if you have to work\nwith an ancient compiler and still want to enjoy the latest additional features, I'd\nprobably merge a PR that restores support.\n\n\n\nWhy?\n----\nI initially started this library during my diploma thesis. The usual approach of\nwriting data from the c++ algorithm to a file and afterwards parsing and plotting\nit in python using matplotlib proved insufficient: Keeping the algorithm\nand plotting code in sync requires a lot of effort when the C++ code frequently and substantially\nchanges. Additionally, the python yaml parser was not able to cope with files that\nexceed a few hundred megabytes in size.\n\nTherefore, I was looking for a C++ plotting library that was extremely easy to use\nand to add into an existing codebase, preferably header-only. When I found\nnone, I decided to write one myself, which is basically a C++ wrapper around\nmatplotlib. As you can see from the above examples, plotting data and saving it\nto an image file can be done as few as two lines of code.\n\nThe general approach of providing a simple C++ API for utilizing python code\nwas later generalized and extracted into a separate, more powerful\nlibrary in another project of mine, [wrappy](http://www.github.com/lava/wrappy).\n\n\nTodo/Issues/Wishlist\n--------------------\n* This library is not thread safe. Protect all concurrent access with a mutex.\n  Sadly, this is not easy to fix since it is not caused by the library itself but\n  by the python interpreter, which is itself not thread-safe.\n\n* It would be nice to have a more object-oriented design with a Plot class which would allow\n  multiple independent plots per program.\n\n* Right now, only a small subset of matplotlibs functionality is exposed. Stuff like xlabel()/ylabel() etc. should\n  be easy to add.\n\n* 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\"\nin \"\".'\n\n* 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.\nSolution is described [here](https://stackoverflow.com/questions/21784641/installation-issue-with-matplotlib-python?noredirect=1\u0026lq=1), additional information can be found there too(see links in answers).\n","funding_links":[],"categories":["TODO scan for Android support in followings","Data visualization","C++","Maths","Mathematics","Tools"],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flava%2Fmatplotlib-cpp","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Flava%2Fmatplotlib-cpp","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flava%2Fmatplotlib-cpp/lists"}