Giter Site home page Giter Site logo

fdcl-gwu / cpp-plots Goto Github PK

View Code? Open in Web Editor NEW
5.0 3.0 2.0 54 KB

Examples for plotting with C++, including real-time plots

License: MIT License

CMake 8.06% C++ 77.22% Python 14.72%
c-plus-plus cpp matplotlib plotting real-time real-time-plotting multithreading gtkmm gtkmm-plplot examples

cpp-plots's Introduction

Plotting with C++

C++ is an extremely useful language, especially when it comes to working with hardware. One of the limitations with using C++ is it's inflexibility when it comes to plotting data. This repository explores different ways to plot with C++.

This repository uses the below three different apporaches, and each may be useful in different scenarios.

  1. Matplotlib wrapper
  2. gtkmm
  3. Local server-client connection

Matplotlib Wrapper

This method uses the matplotlib-cpp written by Benno Evers. This library basically gives you a Python like functions to be used directly inside C++ code.

Dependencies

  • Matplotlib Wrapper

    sudo apt-get install python-tk python-numpy python2.7-dev python-dev3
    pip install matplotlib numpy
    

    If you already don't have one, create a file named matplotlibrc in ~/.matplotlib and add this line to the file.

    backend: TkAgg
    

Running the Code

  • Compiling the code
    cd 01_matplotlib-wrapper 
    mkdir build && cd build
    cmake ../
    make
    
  • Running the program:
    • Draw a line plot with static data: ./plot_line
    • Draw a plot with changing data: ./animate

gtkmm

This method uses gtkmm-plplot by Tom Schoonjans. This library uses PLplot, and brings it's functions to gtkmm. This has been successfully tested in both Ubuntu 18.04 and macOS Mojave.

Dependencies

  • Linux

    sudo apt-get install libgtkmm-3.0-dev
    sudo apt-get install libltdl3-dev libqhull-dev libpango1.0-0 libplplot-dev
    
  • MacOS

    brew install gtkmm gtkmm3 glade plplot
    brew install tschoonj/tap/gtkmm-plplot
    

Running the Code

  • Compiling the code
    cd 02_gtkmm 
    mkdir build && cd build
    cmake ../
    make
    
  • Running the program:
    • Hello world (just an application screen with a button): ./hello_world
    • A copy of a PLplot example which includes some interactive plots: ./hello_world_plot
    • A plot example with minimum requirements: ./minimal_working_example
    • A plot which utlizes multithreading: ./updating_plot

Known Issues

  • At least in macOS Mojave, the main GUI window needs to called in the main thread. So, using multithreading requires the threads to not to be joined. Users need to stick to other measures to make sure that the program does not prematurely end once a single thread exits.
  • The app crashes randomly when the mouse is being moved over the drawing canvas area.

Quick Troubleshooting

  • OSX:
    • Package 'libffi', required by 'gobject-2.0', not found: export PKG_CONFIG_PATH="/usr/local/opt/libffi/lib/pkgconfig"
    • Package 'icu4c' not found: export PKG_CONFIG_PATH="/usr/local/opt/icu4c/lib/pkgconfig

Local Server-Client Connection

All the methods discussed so far use different libraries developed in C++. In contrast, this method uses a server-client communication between C++ code and Python code so that all the plots are drawn using Python's Matplotlib. This does not suffer from issues related to multithreading as discussed above, and also can be used for plotting real-time data while talking to hardware.

This method has the potential to explore all the Matplotlib functions even after the updates, without waiting for some third party developers (most of whome develop them as a service to others in spite of working on real jobs) to update their libraries.

This method works as follows:

  • C++ code: This is the server connection. C++ code does some calculations and send those data which requires poltting to the Python code.
  • Python code: This is the client connection. Python code receives the data from the C++ code and plots them.

Both of these codes utilize multithreading so that the data transmission and plotting work independently.

C++ code

cd 03_server-client 
mkdir build && cd build
cmake ../
make
./server

NOTE: Server (C++ code) must be run first.

Python code

Below instructions assume that you have installed Anaconda (or atleast Python with numpy and matplotlib) in your system.

cd 03_server-client/scripts
python client.py

NOTE: Client (Python code) must be run while the server (C++ code) is running.

cpp-plots's People

Contributors

kanishkegb avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

cpp-plots's Issues

gtkmm-plplot known issues

Hi!

I am the author of gtkmm-plplot and stumbled onto your repository just now.

I would just like to comment on the Known issues you reported on the README.md page:

At least in macOS Mojave, the main GUI window needs to called in the main thread. So, using multithreading requires the threads to not to be joined. Users need to stick to other measures to make sure that the program does not prematurely end once a single thread exits.

This is a constraint imposed by Gtk and has nothing to do with Gtkmm-PLplot. If you would like to update the GUI from a different thread, you will need to call gdk_threads_add_idle. This is true on all operating systems, not just macOS.

The app crashes randomly when the mouse is being moved over the drawing canvas area.

This is a bug and should be fixed: does it occur with the example in this repo?

Best,

Tom

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.