使用 matplotlibcpp.h 在 C++ 代码中绘制图形plt::subplot();程序抛出运行时错误,
PyObject* res = PyObject_CallObject(detail::_interpreter::get().s_python_function_subplot, args);
if(!res) throw std::runtime_error("Call to subplot() failed.");报错位置。
解决方法:在matplotlibcpp.h文件中把
PyTuple_SetItem(args, 0, PyFloat_FromDouble(nrows));
PyTuple_SetItem(args, 1, PyFloat_FromDouble(ncols));
PyTuple_SetItem(args, 2, PyFloat_FromDouble(plot_number));改为
PyTuple_SetItem(args, 0, PyLong_FromDouble(nrows));
PyTuple_SetItem(args, 1, PyLong_FromDouble(ncols));
PyTuple_SetItem(args, 2, PyLong_FromDouble(plot_number));
解决C++使用matplotlibcpp.h调用subplot()时的运行时错误

在C++代码中使用matplotlibcpp.h绘制图形时遇到subplot()函数引发的运行时错误,原因是将参数传递为浮点数。通过将PyTuple_SetItem中的PyFloat_FromDouble替换为PyLong_FromDouble,解决了因类型不匹配导致的失败问题。
8591





