matplotlib-cpp icon indicating copy to clipboard operation
matplotlib-cpp copied to clipboard

libc++abi.dylib: terminating with uncaught exception of type std::runtime_error: Error loading module mpl_toolkits!

Open cdalas2 opened this issue 4 years ago • 9 comments

I ran all the examples provided and they all worked except for the surface plot example. I run my code on a Mac using Xcode. The error occurs when I use plt::plot_surface(x,y,z) in the example. I plan on using Matplotlib-cpp to do surface plots and so its crucial that I get this to work. Please help!

cdalas2 avatar Jun 25 '20 14:06 cdalas2

Same on mac, I am on Intellij IDEA. Did you solve it?

fzyzcjy avatar Jul 29 '20 01:07 fzyzcjy

Unfortunately not. I ended up just transferring my data to MATLAB and plotting there. Hopefully once I chip away at some of my current workload, I'll come back and try to figure it out. If you end up fixing the issue before then, I hope you'll come back and post it here!

Best, Carlos Alas

cdalas2 avatar Jul 29 '20 01:07 cdalas2

Oh I finally find out the reason! In my case, I use the native python2 in mac. But it has matplotlib version 1.x.x. Of course it is too old.

However I did not succeed in updating the matplotlib in the system python2.

fzyzcjy avatar Jul 29 '20 03:07 fzyzcjy

Fantastic! Sounds like that could have been my issue as well. On my mac I do have python 2 running. I'm not sure what version of matplotlib I had but it must have been an older version. I'll give it a try with python3 and the newest version of matplotlib soon and report back to confirm their compatibility with matplotlib-cpp.

cdalas2 avatar Jul 30 '20 06:07 cdalas2

I meet the same problem in my Mac with sublime when I do the make in my terminal. Could author provide a solution for applying the python3 if it works?

HYX318 avatar Nov 22 '21 13:11 HYX318

Same problem here!

AnshG714 avatar Jun 23 '22 05:06 AnshG714

Matplotlibcpp works with the old package mpl_toolkit module which was installed separately from matplotlib. Now mpl_toolkit is bundled with matplotlib (I think as of version 2 and up). The 3D plotting features of matplotlibcpp are thus deprecated.

I have not come up with a clever work around.

mearlearl avatar Oct 25 '22 01:10 mearlearl

Here is the workaround:

In python, the way we would make 3d charts using mpl_toolkit would work like this.

fig = plt.figure(); ax = fig.gca(projection = '3d')

Nowadays in matplotlib you just do something like this.

fig = plt.figure(); ax = fig.add_subplot(projection = '3d')

In matplotlibcpp.h we just need to change the proper function calls that create the axis object. My use case is updating the scatter plot for 3D around line 1140:

Original:

PyObject *axis = PyObject_Call( gca, detail::_interpreter::get().s_python_empty_tuple, gca_kwargs); Fixed:

PyObject *axis = PyObject_Call( PyObject_GetAttrString(fig, "add_subplot"), detail::_interpreter::get().s_python_empty_tuple, gca_kwargs);

Hope this helps :)

mearlearl avatar Oct 25 '22 03:10 mearlearl

Worked for me.

bartholmberg avatar Dec 22 '22 22:12 bartholmberg