GithubHelp home page GithubHelp logo

cpp-matplotlib's Introduction

Contents

About

An easy-to-use C++11 library for simple plotting from C++ via a ZeroMQ bridge to an IPython kernel.

It provides the ability to send NumPy array compatible data to an IPython kernel session as well as execute arbitrary code.

Execution of arbitrary code is "protected" by IPython's kernel HMAC signing mechanism: only code signed by a secret key provided by IPython will run. As of 2015-03-19 it has been tested with IPython version 1.2.1 on Ubuntu 14.04.

Usage

First, create an IPython kernel session

$ ipython kernel --pylab
NOTE: When using the `ipython kernel` entry point, Ctrl-C will not work.

... blah blah blah ...

To connect another client to this kernel, use:
    --existing kernel-NNN.json

It is important to remember that NNN in the last line, which is the PID of the kernel. This JSON file is stored somewhere in your $HOME, exactly where can vary. Find it with find ~/ -name kernel-NNN.json.

Here we create some 1D data and plot it. The numpy.array "MyData" will be available for working with in the IPython session, even after the C++ program finishes.

All library code lives in the cppmpl namespace.

#include "cpp_mpl.hpp"

int main() {
  // ...

  cppmpl::CppMatplotlib mpl{"/path/to/kernel-NNN.json"};
  mpl.Connect();

  // Create a nice curve  
  std::vector<cppmpl::NumpyArray::dtype> raw_data;
  double x = 0.0;
  while (x < 3.14159 * 4) {
    raw_data.push_back(std::sin(x));
    x += 0.05;
  }

  // Send it to IPython for plotting
  cppmpl::NumpyArray data("MyData", raw_data);
  mpl.SendData(data);
  mpl.RunCode("plot(MyData)\n"
              "title('f(x) = sin(x)')\n"
              "xlabel('x')\n"
              "ylabel('f(x)')\n");

  // NOTE: if you want to store the python in an external file, use the 
  // convenience function LoadFile("my_code.py"), as in, 
  // mpl.RunCode(cppmpl::LoadFile("plotting_code.py"));

  // ...
}

And the result is Screenshot

See src/main.cc for a complete program.

To work with "MyData" you can connect to the kernel using an IPython console, notebook, or qtconsole:

$ ipython console --existing kernel-NNN.json
Python 2.7.6 (default, Mar 22 2014, 22:59:56) 
Type "copyright", "credits" or "license" for more information.

IPython 1.2.1 -- An enhanced Interactive Python.
?         -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help      -> Python's own help system.
object?   -> Details about 'object', use 'object??' for extra details.

In [1]: MyData *= 4

In [84]: print MyData[9]
[ 1.73986214]

Compiling / Linking

When compiling you must link against libcpp_mpl.so, which, assuming you've installed it to somewhere in your library path:

$ g++ my_prog.cc -std=c++11 -lcpp_mpl -lyour_other_libs

Prereqs

Ubuntu 14.04

sudo apt-get install ipython python-matplotlib libzmq3-dev \
                     libjsoncpp-dev uuid-dev libssl-dev

Building

# git clone this repository to cpp-matplotlib/
cd cpp-matplotlib
mkdir build
cd build
cmake ..
make

Installing

make install

By default installs to /usr/local. Don't forget to run sudo ldconfig to update the library cache.

Generating the documentation

cd cpp-matplotlib
doxygen Doxyfile
# open html/index.html

Running the Example

In terminal 1:

ipython kernel --pylab
# this will print out the kernel PID to connect to, NNN below.

In terminal 2:

# Once per kernel invocation:
export KERNEL_CONFIG=`find ~/ -name kernel-NNN.json`

# Each time you run the program
build/cpp-matplotlib-example $KERNEL_CONFIG

In terminal 3 (if desired):

ipython console --existing kernel-NNN.json

cpp-matplotlib's People

Contributors

daemacles avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.