Matplotlib in C++ with CMake

Matplotlib-cpp is a header only library that exposes the functionalities of python matplotlib library to C++. The following example plots a figure as shown below.

1
2
3
4
5
6
7
8
    #include "matplotlibcpp.h"
    namespace plt = matplotlibcpp;
    int main()
    {
        plt::plot({1,3,2,4});
        plt::show();
    }
    


The command line code to build the program is as follows

g++ minimal.cpp -o minimal -std=c++11 -I/usr/include/python2.7 -lpython2.7

To make the same work with CMake, the required lines in CMakeLists.txt are

cmake_minimum_required (VERSION 3.0)
project(minimal)

find_package(PythonLibs REQUIRED)
find_package(PkgConfig REQUIRED)

include_directories(
    ${PYTHON2_INCLUDE_DIRS}
    "/usr/include/python2.7"
)

add_executable(
    ${PROJECT_NAME}
    minimal.cpp
)

target_link_libraries(
    ${PROJECT_NAME}
    python2.7
)



Enjoy Reading This Article?

Here are some more articles you might like to read next:

  • Part2: Developing a ROS C++ package
  • a post with advanced image components
  • Part1: Supervised Learning: Basic linear regression
  • Custom Inverse Kinematics MoveIt Plugin
  • Part1: Developing a ROS C++ package