Skip to content

Extremely simple yet powerful header-only C++ plotting library built on the popular matplotlib

License

Notifications You must be signed in to change notification settings

aa-samad/matplotlib-cpp

 
 

Repository files navigation

matplotlib-cpp

This fork is a CMake-ready, ready-to-use adaptation of matplotlib-cpp using custom conda env paths.

Changes from original repo

  • Updated deprecated functions
  • Ability to include in code and use it in CMake easily
  • Ability to easily build against a custom conda env

Compiliation

  • add these three lines to your CMakeLists.txt file to:
    1. Add the lib to your code,
    2. Remove the glibc runtime library difference problem between conda and the system
    3. link your code against this library, ex:
target_include_directories(my_code__replace_me PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/matplotlib-cpp)
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/matplotlib-cpp)

set_target_properties(my_code__replace_me PROPERTIES
    LINK_FLAGS "-Wl,-rpath,/usr/lib/x86_64-linux-gnu"
)

target_link_libraries(my_code__replace_me PRIVATE matplotlib)

Usage

Complete minimal example:

#include "matplotlibcpp.h"
#include <vector>

namespace plt = matplotlibcpp;

int main()
{
    std::vector<double> x{1, 2, 3, 4, 5};
    std::vector<double> y{1, 4, 9, 16, 25};
    plt::figure_size(1200, 780);
    // Plot line from given x and y data. Color is selected automatically.
    plt::plot(x, y);
    // Set x-axis to interval [0,10]
    plt::xlim(0, 10);
    // Add graph title
    plt::title("Sample figure");
    plt::show();
}

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).

About

Extremely simple yet powerful header-only C++ plotting library built on the popular matplotlib

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • C++ 98.1%
  • CMake 1.9%