Issue
I tried to use matplotlibcpp.h for plotting graph in c++ code. Normal graphs are plotted well. However, when I write plt::subplot(); the program throw runtime error with "Call to subplot() failed". How can solve this problem?
Below is my source code.
#include <iostream>
#include <vector>
#include <map>
#include <string>
#include <matplotlib.h>
namespace plt = matplotlibcpp;
using std::cout;
using std::map;
using std::string;
using std::vector;
int main()
{
vector<int> x1(10);
vector<int> x2(10);
vector<int> y1(10);
vector<int> y2(10);
for(int i = 0; i <10; ++i)
{
x1[i] = i;
y1[i] = i;
x2[i] = i;
y2[i] = i*2;
}
plt::subplot(1,2,1); // <- error raised point
plt::plot(x1,y1);
plt::title("y=x");
plt::subplot(1,2,2);
plt::plot(x2,y2,"k-");
plt::title("y=2x");
plt::show();
}
The full error message is
terminate called after throwing an instance of 'std::runtime_error'
what(): Call to subplot() failed.
Aborted
And, my compile option is
g++ matplotlib_test.cpp -I/usr/include/python3.8 -lpython3.8 -o matplotlib_test
My running environment is wsl2(windows-subsystem-linux) and ubuntu-20.04-LTS
Lastly, could you recommend what is the best way for plotting graph with c++ code?
Solution
I had the same problem. I found a solution to overcome these issue in the issue area of matplotlib-cpp. I needed to change the matplotlibcpp.h from:
PyTuple_SetItem(args, 0, PyFloat_FromDouble(nrows));
PyTuple_SetItem(args, 1, PyFloat_FromDouble(ncols));
PyTuple_SetItem(args, 2, PyFloat_FromDouble(plot_number));
to:
PyTuple_SetItem(args, 0, PyLong_FromDouble(nrows));
PyTuple_SetItem(args, 1, PyLong_FromDouble(ncols));
PyTuple_SetItem(args, 2, PyLong_FromDouble(plot_number));
I don't know the consequences of changing the header file, but it solved my problems.
Answered By - klanky
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.