TransWikia.com

What's the simplest way to graph a 2d array generated in C++ using Windows 7?

Computational Science Asked by Hair of Slytherin on August 27, 2021

This was so much easier in Python (so much so that I’m considering going back to matplotlib for data analysis). But really, trying to figure out ROOT and Boost graph library and mathgl makes me want to die.

What I’m thinking for the short term is to somehow export my 2d array to a text file and then import that file into a numpy array as I already have that infrastructure coded. Then I’ll use matplotlib to visualize the data.

In the long term, I’d like to learn the “standard way”to visualize data from c++ to add to my list of marketable skills. I’ve been scared away from Root by some of my particle physics friends, disgusted away from mathgl by a Linux snob and like how gnuplot looks but am turned off by the fact that it’s command line operated. Is there nothing directly synonymous with matplotlib in c++ in functionality and ease of use?

4 Answers

There are a couple subtleties to your question that I think are important:

  • You're comparing an interpreted language (Python) to a compiled language (C++).
  • Most scientific and engineering software is developed with a heavy Linux (and UNIX) bias, and is not usually known for cross-platform compatibility or great user support (big libraries, of course, excepted)

If you really want to use a C++ library, you probably want something that has a CMake-based build system. PLplot is one such library that looks reputable and has a C++ interface (unfortunately, the others that seem to come up are ROOT and MathGL...).

However, given the points above, if using Cygwin or MinGW is going to drive you into a murderous rage, I'd stick with Python or a visualization program you could install from a binary (like Paraview or VisIt, for instance; other programs could also work). Here's why:

Interpreted languages are convenient for tweaking visualizations

The big advantage of doing your visualization in C++ is that you can append it to whatever C++ code you might be using for simulation work. That said, if you have to compile your code after every modification you make, it's going to make tweaking your visualizations that much more time-consuming. With an interpreted language, once the data is loaded, you can post-process and tweak visualizations on the command line. If you plot raw data and decide it might look better normalized, no problem; a few lines of code, and you can bring up a new plot in a minute or two without having to recompile.

Compiling scientific software on Windows is a pain

...because much of the time, whoever wrote it didn't use a cross-platform build system like CMake, so you either have to slog through and hack together a Makefile that works, or use Cygwin or MinGW to compile everything and hope that the software doesn't rely on Linux libraries (because after all, Cygwin is a POSIX compatibility layer). If a project uses CMake, it probably means that the developers have thought about these issues, and CMake will output the necessary build scripts to compile software with Visual Studio, which will make your life less painful. Otherwise, it's probably not worth the time and effort.

If you can install the visualization software you need from a binary, you don't have to worry about grappling with Visual Studio. There are at least two Python distributions I can think of that will supply Matplotlib (Continuum's Anaconda and Enthought Canopy) with a binary install of Python on multiple platforms. Paraview and VisIt are both powerful scientific visualization packages that, again, can be installed from binaries (they can also be built from source on Windows).

I don't think there is a "standard C++ way" to plot data

I haven't heard of a "standard C++ way" to plot data (it doesn't mean it doesn't exist); when I talk to data scientists, a lot of them stress how convenient and important it is to be fluent in an interpreted language like Python, R, or MATLAB because visualization and post-processing can be developed rapidly in those languages. Standard practice for a lot of fields is to dump data to some text format (column-delimited, CSV, HDF5, VTK, etc.) and then read in the data using one of those interpreted languages or a dedicated visualization program (like VisIt, Paraview, and for 2-D plots, gnuplot).

Correct answer by Geoff Oxberry on August 27, 2021

Well, for one, you can call gnuplot as a library. Pretty much every command you'd use at the prompt has a similarly-named API function.

Answered by Bill Barth on August 27, 2021

C++ does not have a standard library that contains the ability to generate plots (or, in fact, anything graphical). Nor does C++ have a system of packages with a package manager that allows you to import this functionality.

However, there are cross-platform libraries that facilitate what you want. The most widely used one, also available on Windows, is probably Qt. It will be a little bit more work than in Python to open a window and plot something, but it shouldn't be as difficult as with many of the other libraries you mention.

Answered by Wolfgang Bangerth on August 27, 2021

There is CImg, a header-only library that is fairly performant but not very tweakable. It is mainly about image processing, but it also offers some functions for drawing graphs and axes, albeit limited (you get the graph options you get, and that is it).

The biggest quirk with it is just getting used to the way the image is stored internally. The color channels are separated, so you've got contiguous bytes for blue, then green, then red. To output to a screen or file, you've got to interleave them back together. The sample code is good and makes it easy to figure things out.

I've used it (it can draw a few graphs fast enough to keep up with a live video stream at 60fps) but also look longingly at matplotlib and the like.

Answered by aggieNick02 on August 27, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP