Running python matplotlibcpp in Visual Studio 2019

Viewed 1079

I would like to install a plotting functionality to a c++ program. I chose matplotlibcpp due to its simplicity and online reviews. However, I cannot produce a plot and get numerous cryptic errors (I am new to C++). After updating the Include paths for VS 2019, I was able to get error free on compile and linking but the execution listed numerous errors.

Below is a simple code snippet which produces the errors.

#include <matplotlibcpp.h>
#include <string>
using namespace std;
namespace plt = matplotlibcpp;


int main()
{
    plt::plot({ 1,2,2,4 });
    plt::show();
}

I get the following output in the output window:

Fatal Python error: initfsencoding: unable to load the file system codec ModuleNotFoundError: No module named 'encodings'

Current thread 0x000018c0 (most recent call first)

Any ideas on how to get matplotlibcpp to run in VS 2019?

Running VS 2019 on Windows 10 x64.

2 Answers

I had trouble to find a nice plotting library in c++. I decided to try matplotlibcpp and after a while, i succeed to make it works under windows 10, visual studio 2019, Python 3.9.

First, i was trying to make it works with conda. i gave up because of trouble with setting of PYTHONHOME variable. I thus continue with a classical installation of python (3.9).

First (1), one has to be sure that python can be accessed via command prompt or powershell by typing:

python --version

Second (2), I have installed numpy and matplolib :

python -m pip install matplotlib
python -m pip install numpy

Then (3) i created a new project in cpp and I pasted those simples lines (https://github.com/lava/matplotlib-cpp) in a new cpp file:

#include <iostream>
#include "matplotlibcpp.h"
#include <vector>

namespace plt = matplotlibcpp;
int main() {
    std::vector<double> y = { 1, 3, 2, 4 };
    plt::plot(y);

    plt::show();

}

then (4), i downloaded the matplotlibcpp.h (https://github.com/lava/matplotlib-cpp/blob/master/matplotlibcpp.h) and added it in my project One .cpp and one .h

then (5), I went in the properties of the project by doing project, properties and then add in C/C++ properties, Additionnal include directories: E:\Python39\include;E:\Python39\Lib\site-packages\numpy\core\include

set additionnal include directories

then(6), in the linker, general, Additionnal library directories, i added: E:\Python39\libs

Additionnal library directories

then (7) in the linker, input, additionnal dependencies, i added: E:\Python39\libs\python39.lib be sure to do not delete "%(AdditionalDependencies)". AdditionalDependencies

then , I compiled and get two different errors that were classical and not hard to solve:

(8) one was related to 4 lines in the matplotlibcpp.h: a comment("Sanity checks") just above those four lines, tells you that they should be comment if trouble. I did it and it works: the error doesn't appear again:

those 4 lines needed to be commented

(9) the compiler also complains about something related to standard in c++. I follow guideline found on the net and just went in the properties, C/C++, langage and choose iso C++ 17 standard. Then this second error was solved.

choose iso C++ 17

(10) then i compiled again and i remembered that i also have complained about python39_d.lib or something like that. I just need to choose "release" and not "debug" in visual studio (and also be sure to be in X64).

(11) then, i added the python39.dll in the X64/release folder where the .exe is generated (i also added the python3.dll but i think its useless; only python39 (3x if one have different version) should be needed).

dont forget to add the dll

(12) then it works.

working configuration

I had the same problem, I installed vcpkg Manager and followed the instructions applied analogously for matplotlib-cpp (see https://www.youtube.com/watch?v=b7SdgK7Y510). Inlcude vcpkg lib and include and your correct Python installation in project attribution. Then uncommented the std::stod errors. Now it's working.

Related