Matplotlib and Cpp

Dear all,

I was wondering if it is possible to use matplotlib from C++ directly
and I have found an example on how to do this on StackOverflow:

http://stackoverflow.com/questions/2509156/anyone-knows-a-matplotlib-equivalent-in-c-or-c

However, my question is about piping. Where could I find any tutorials
on how to pipe data directly to matplotlib so that I would not need to
right it to a file and then read it?

Also, does matplotlib benefit from usage of cython and if it does, are
there any things I should know before just trying to compile a
matplotlib script with cython?

All best,
Ignas A.

Dear all,

I was wondering if it is possible to use matplotlib from C++ directly
and I have found an example on how to do this on StackOverflow:

http://stackoverflow.com/questions/2509156/anyone-knows-a-matplotlib-equivalent-in-c-or-c

Well, using that "method", you could interface with any python libraries in
C++, but you wouldn't be able to do anything useful.

However, my question is about piping. Where could I find any tutorials
on how to pipe data directly to matplotlib so that I would not need to
right it to a file and then read it?

Not from C++, sorry. matplotlib is primarially python with some C++ stuff
for efficiency.

Also, does matplotlib benefit from usage of cython and if it does, are
there any things I should know before just trying to compile a
matplotlib script with cython?

matplotlib does not use Cython, it uses CXX for accelerating special parts
of the library. Maybe you can use matplotlib in cython (I have never
tried), but there are no cython bindings that I am aware of for matplotlib.

Sorry I could not be of more help.

Ben Root

···

On Fri, Apr 13, 2012 at 4:24 PM, Ignas Anikevicius <anikevicius@...287...>wrote:

You're trying to solve the wrong problem. If you want to use matplotlib to show your results this way the easiest thing to do would be to rework so that python was the driver. i.e wrap your C++ code so that a python program called it instead of trying to call python from C++.

The better solution here is probably to abandon matplotlib and use a plotting library that has C bindings. One example is DISLIN, but I'm sure there are others (and don't take this as a recommendation.)

Eric

···

-----Original Message-----
From: Ignas Anikevicius [mailto:anikevicius@…287…]
Sent: Friday, April 13, 2012 4:25 PM
To: matplotlib-users@lists.sourceforge.net
Subject: [Matplotlib-users] Matplotlib and Cpp

Dear all,

I was wondering if it is possible to use matplotlib from C++ directly
and I have found an example on how to do this on StackOverflow:

http://stackoverflow.com/questions/2509156/anyone-knows-a-matplotlib-
equivalent-in-c-or-c

However, my question is about piping. Where could I find any tutorials
on how to pipe data directly to matplotlib so that I would not need to
right it to a file and then read it?

Also, does matplotlib benefit from usage of cython and if it does, are
there any things I should know before just trying to compile a
matplotlib script with cython?

All best,
Ignas A.

Hi,

if I understand well, you have a program producing data which usually are stored in a file,

then you load such file into a matplotlib script.

Your goal is trasfering data from your program to matplotlib.

If this is the case you can follow two routes:

  1. Modify the progam in order to print the information on standard output.

Then, through the function popen, you execute your program into matplotlib, reading directly

the data without any external file.

  1. if you cannot mpdify you program, you can execute in popen something like:

program file.out; cat file.out.

Again, your data are on the displayed on the standard output which is captured by popen

Francesco

2012/4/27 Moore, Eric (NIH/NIDDK) [F] <eric.moore2@…3928…>

···

-----Original Message-----

From: Ignas Anikevicius [mailto:anikevicius@…287…]

Sent: Friday, April 13, 2012 4:25 PM

To: matplotlib-users@lists.sourceforge.net

Subject: [Matplotlib-users] Matplotlib and Cpp

Dear all,

I was wondering if it is possible to use matplotlib from C++ directly

and I have found an example on how to do this on StackOverflow:

http://stackoverflow.com/questions/2509156/anyone-knows-a-matplotlib-

equivalent-in-c-or-c

However, my question is about piping. Where could I find any tutorials

on how to pipe data directly to matplotlib so that I would not need to

right it to a file and then read it?

Also, does matplotlib benefit from usage of cython and if it does, are

there any things I should know before just trying to compile a

matplotlib script with cython?

All best,

Ignas A.

You’re trying to solve the wrong problem. If you want to use matplotlib to show your results this way the easiest thing to do would be to rework so that python was the driver. i.e wrap your C++ code so that a python program called it instead of trying to call python from C++.

The better solution here is probably to abandon matplotlib and use a plotting library that has C bindings. One example is DISLIN, but I’m sure there are others (and don’t take this as a recommendation.)

Eric


Live Security Virtual Conference

Exclusive live event will cover all the ways today’s security and

threat landscape has changed and how IT managers can respond. Discussions

will include endpoint security, mobile security and the latest in malware

threats. http://www.accelacomm.com/jaw/sfrnl04242012/114/50122263/


Matplotlib-users mailing list

Matplotlib-users@lists.sourceforge.net

https://lists.sourceforge.net/lists/listinfo/matplotlib-users


Cordiali saluti, Dr.Oteri Francesco

First cpp stands for C Pre Processor, this tool usually does macro
substitution in c, objective c, c++. Hence Cpp in the object is pretty
much confusing when it seems to be talking about C++.

2012/4/27 Benjamin Root <ben.root@...1304...>:

Dear all,

I was wondering if it is possible to use matplotlib from C++ directly
and I have found an example on how to do this on StackOverflow:

Hello,

http://stackoverflow.com/questions/2509156/anyone-knows-a-matplotlib-equivalent-in-c-or-c

Well, using that "method", you could interface with any python libraries in
C++, but you wouldn't be able to do anything useful.

I dont quite get it Ben.

The stackoverflow is pretty revelant.

using #include <python.h> is smart: you can avoid all the free/malloc
in GC byt using python data structure and use this in your C code. And
I know C/C++ dev using this trick in real open source project since
python.h is less complicated to use than all the kombinat from boost.
But, I would not use this method. (Question of taste and of
complexity).

There is another simpler solution however :
Do everything in python : python is a very powerfull gluing langage,
the GIL ensuring that non thread-safe code is thread safe, it is very
forgiving with code not design for concurrency.

***** ctypes permits the direct loading and binding with C++ classes,
or C functions.

**** Another solution that seems impressive, is writing C/C++
extensions in python. It is very easy (I self learnt it in one day
even though IQ test rank me 20 points below average).

The benefit is you could use your C++ code to return native python
object (set, array, dict, string, unicode, int (arbitrary long int)
...).
My advice for big stream of data is you can return an iterator on the
stream of data, thus limiting the memory usage if it is important
(dont if you dont need it, premature optimization is the root of all
evil).

if your c++ code becomes an extension of python, it's deployment in an
ecosystem of python instances will be made easier through the use of
distribute.

This gluing will ensure your data will flow without all the quirks
related to popen (caching of stdout/in, no control on mermory use ,
interruptions, portability issues ...).

The first solution is easy to achieve, the second solution requires
more time but is funnier.

Cheers;

···

On Fri, Apr 13, 2012 at 4:24 PM, Ignas Anikevicius <anikevicius@...287...> > wrote:

--
Jul

Hello everybody,

First cpp stands for C Pre Processor, this tool usually does macro

substitution in c, objective c, c++. Hence Cpp in the object is pretty

much confusing when it seems to be talking about C++.

Sorry for my ignorance, will know it in the future

There is another simpler solution however :

Do everything in python : python is a very powerfull gluing langage,

the GIL ensuring that non thread-safe code is thread safe, it is very

forgiving with code not design for concurrency.

I wanted to write in Python as much code as possible, it is just some plot commands are quite slow in matplotlib. For example, I want to work in polar coordinate system to plot a function of 2 variables. For that I want to use the np.meshgrid function and the plot the results with the pcolor command from pyplot. The problem is that with a lot of data points it becomes very slow, which is not acceptable if one wants to draw a lot of plots using this function. Because the array is not ordered and points in space are at irregular intervals, I could not use imshow, which is much much faster. So I was thinking if there is some internal C++ API which I could use and maybe speed up the plotting itself a bit.

It would actually be very nice if I could do that as most of the toolkits, which can interface with C/C++ do not have LaTeX capabilites. Gnuplot can be used in C/C++, but as far as I remember it is not the most elegant way of doing it…

Going back to the topic, is there any potential to speed up some commands (e.g. pcolor) by rewriting/extending them in Cython or C++? If yes, then maybe I might tinker with the code at some point, when I have more free time.

Anyway, thanks for such detailed answers.

All best,
Ignas A.

···

On 28 April 2012 12:13, julien tayon <julien@…4072…> wrote:

Use pcolormesh(). much faster if you can assume certain things about the domain.

Ben Root

···

On Sunday, April 29, 2012, Ignas Anikevičius wrote:

Hello everybody,

On 28 April 2012 12:13, julien tayon <julien@…4072…> wrote:

First cpp stands for C Pre Processor, this tool usually does macro

substitution in c, objective c, c++. Hence Cpp in the object is pretty

much confusing when it seems to be talking about C++.

Sorry for my ignorance, will know it in the future

There is another simpler solution however :

Do everything in python : python is a very powerfull gluing langage,

the GIL ensuring that non thread-safe code is thread safe, it is very

forgiving with code not design for concurrency.

I wanted to write in Python as much code as possible, it is just some plot commands are quite slow in matplotlib. For example, I want to work in polar coordinate system to plot a function of 2 variables. For that I want to use the np.meshgrid function and the plot the results with the pcolor command from pyplot. The problem is that with a lot of data points it becomes very slow, which is not acceptable if one wants to draw a lot of plots using this function. Because the array is not ordered and points in space are at irregular intervals, I could not use imshow, which is much much faster. So I was thinking if there is some internal C++ API which I could use and maybe speed up the plotting itself a bit.

It would actually be very nice if I could do that as most of the toolkits, which can interface with C/C++ do not have LaTeX capabilites. Gnuplot can be used in C/C++, but as far as I remember it is not the most elegant way of doing it…

Going back to the topic, is there any potential to speed up some commands (e.g. pcolor) by rewriting/extending them in Cython or C++? If yes, then maybe I might tinker with the code at some point, when I have more free time.

Anyway, thanks for such detailed answers.

All best,
Ignas A.

Thank you very much!

Ignas

···

On 30/04/12 02:15, Benjamin Root wrote:

Use pcolormesh(). *much* faster if you can assume certain things about
the domain.

Ben Root