matplotlib-cpp | Extremely simple yet powerful header-only C++ plotting | Data Visualization library
kandi X-RAY | matplotlib-cpp Summary
kandi X-RAY | matplotlib-cpp Summary
Welcome to matplotlib-cpp, possibly the simplest C++ plotting library. It is built to resemble the plotting API used by Matlab and matplotlib.
Support
Quality
Security
License
Reuse
Top functions reviewed by kandi - BETA
Currently covering the most popular Java, JavaScript and Python libraries. See a Sample of matplotlib-cpp
matplotlib-cpp Key Features
matplotlib-cpp Examples and Code Snippets
Community Discussions
Trending Discussions on matplotlib-cpp
QUESTION
I'm working on a project which uses this C++ matplotlib wrapper matplotlibcpp.h.
A minimal example using this original header file is
...ANSWER
Answered 2021-May-18 at 17:53I don't have an easy access to a Linux where I can test it, but I think I now understand what's happening.
matplotlibcpp
uses a static variable to hold the Python interpreter (see line 129 insideinterkeeper(bool should_kill)
). Like C++ static function variables, it's initialized on the first time the function is called and destructed on program's exit (reference).When
main
finishes,libc
runs cleanup routines for all the shared libraries and for your program (that's__run_exit_handlers
in the stacktrace). Since your program is a C++ program, part of its exit handler is destructing all the static variables that were used. One of them is the Python interpreter. Its destructor callsPy_Finalize()
which is Python's cleanup routine. Until now, everything's fine.Python has a similar
atexit
mechanism that allows Python code from everywhere to register functions that should be called during the interpreter shutdown. Apparently, the backend matplotlib chose to use here is PyQt5. It seems to register such atexit callbacks.PyQt5's callback gets called, and crashes. Notice that this is internal PyQt5 code now. Why does this crash? My "educated" guess is that Qt's library exit handler was already called in step 2, before your program's exit handler was called. This apparently causes some weird state in the library (maybe some objects were freed?) and crashes.
This leaves two interesting questions:
How to fix this? The solution should be to destruct
ctx
before your program exits, so the Python interpreter is destructed before any shared libraries terminate themselves. Static lifetimes are known for causing similar problems. If changing matplotlibcpp's interface to not use global static states is not a possible solution, I think you really have to manually callplt::detail::_interpreter::kill()
at the end of your main function. You should be able to useatexit()
and register a callback that kills the interpreter before the library teardown - I haven't tested it though.Why did this ever work? My guess is that maybe something in PyQt5's callbacks has changed that now causes this crash, or that you use a different backend in Python 2. If no other library is destructively terminating before the program exits, this is fine.
QUESTION
I have a C++ code which creates an array at each time. (C++ is needed because of the speed - if the code was built directly in Python, it would take 8 days to execute.) The structure of the code looks something like:
...ANSWER
Answered 2020-Aug-31 at 10:26The quick and dirty approach to such a problem is often to generate python code from your primary program. Here your C++ program. For example, the string [0, 3, 5, 1]
is a valid python fragment defining an array of four values.
So, if you have your std::array
or your T[N]
or std::vector
with your data, you can serialise them fairly easily to something that can be used as input to the python interpreter:
QUESTION
I have been trying to plot data using matplotlib-cpp. I cloned the repository and linked the library with CmakeLists.txt. Here's my cmakelists.txt :
...ANSWER
Answered 2020-Apr-08 at 13:18I solved it myself. Issue was in CmakeLists.txt:
link_libraries(C:/Python37/libs/python3.lib Python3::NumPy)
was replaced by
Community Discussions, Code Snippets contain sources that include Stack Exchange Network
Vulnerabilities
No vulnerabilities reported
Install matplotlib-cpp
Support
Reuse Trending Solutions
Find, review, and download reusable Libraries, Code Snippets, Cloud APIs from over 650 million Knowledge Items
Find more librariesStay Updated
Subscribe to our newsletter for trending solutions and developer bootcamps
Share this Page