@@ -116,12 +116,9 @@ struct _interpreter {
116
116
117
117
PyObject* matplotlibname = PyString_FromString (" matplotlib" );
118
118
PyObject* pyplotname = PyString_FromString (" matplotlib.pyplot" );
119
- PyObject* mpl_toolkits = PyString_FromString (" mpl_toolkits" );
120
- PyObject* axis3d = PyString_FromString (" mpl_toolkits.mplot3d" );
121
- PyObject* pylabname = PyString_FromString (" pylab" );
122
119
PyObject* cmname = PyString_FromString (" matplotlib.cm" );
123
- if (!pyplotname || ! pylabname || !matplotlibname || !mpl_toolkits ||
124
- !axis3d || !cmname) {
120
+ PyObject* pylabname = PyString_FromString ( " pylab " );
121
+ if (!pyplotname || !pylabname || !matplotlibname || !cmname) {
125
122
throw std::runtime_error (" couldnt create string" );
126
123
}
127
124
@@ -147,14 +144,6 @@ struct _interpreter {
147
144
Py_DECREF (pylabname);
148
145
if (!pylabmod) { throw std::runtime_error (" Error loading module pylab!" ); }
149
146
150
- PyObject* mpl_toolkitsmod = PyImport_Import (mpl_toolkits);
151
- Py_DECREF (mpl_toolkitsmod);
152
- if (!mpl_toolkitsmod) { throw std::runtime_error (" Error loading module mpl_toolkits!" ); }
153
-
154
- PyObject* axis3dmod = PyImport_Import (axis3d);
155
- Py_DECREF (axis3dmod);
156
- if (!axis3dmod) { throw std::runtime_error (" Error loading module mpl_toolkits.mplot3d!" ); }
157
-
158
147
s_python_function_show = PyObject_GetAttrString (pymod, " show" );
159
148
s_python_function_close = PyObject_GetAttrString (pymod, " close" );
160
149
s_python_function_draw = PyObject_GetAttrString (pymod, " draw" );
@@ -414,7 +403,29 @@ void plot_surface(const std::vector<::std::vector<Numeric>> &x,
414
403
const std::vector<::std::vector<Numeric>> &y,
415
404
const std::vector<::std::vector<Numeric>> &z,
416
405
const std::map<std::string, std::string> &keywords =
417
- std::map<std::string, std::string>()) {
406
+ std::map<std::string, std::string>())
407
+ {
408
+ // We lazily load the modules here the first time this function is called
409
+ // because I'm not sure that we can assume "matplotlib installed" implies
410
+ // "mpl_toolkits installed" on all platforms, and we don't want to require
411
+ // it for people who don't need 3d plots.
412
+ static PyObject *mpl_toolkitsmod = nullptr , *axis3dmod = nullptr ;
413
+ if (!mpl_toolkitsmod) {
414
+ detail::_interpreter::get ();
415
+
416
+ PyObject* mpl_toolkits = PyString_FromString (" mpl_toolkits" );
417
+ PyObject* axis3d = PyString_FromString (" mpl_toolkits.mplot3d" );
418
+ if (!mpl_toolkits || !axis3d) { throw std::runtime_error (" couldnt create string" ); }
419
+
420
+ mpl_toolkitsmod = PyImport_Import (mpl_toolkits);
421
+ Py_DECREF (mpl_toolkits);
422
+ if (!mpl_toolkitsmod) { throw std::runtime_error (" Error loading module mpl_toolkits!" ); }
423
+
424
+ axis3dmod = PyImport_Import (axis3d);
425
+ Py_DECREF (axis3d);
426
+ if (!axis3dmod) { throw std::runtime_error (" Error loading module mpl_toolkits.mplot3d!" ); }
427
+ }
428
+
418
429
assert (x.size () == y.size ());
419
430
assert (y.size () == z.size ());
420
431
0 commit comments