1
0

matplotlibcpp.h 74 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336
  1. #pragma once
  2. // Python headers must be included before any system headers, since
  3. // they define _POSIX_C_SOURCE
  4. #include <Python.h>
  5. #include <vector>
  6. #include <map>
  7. #include <array>
  8. #include <numeric>
  9. #include <algorithm>
  10. #include <stdexcept>
  11. #include <iostream>
  12. #include <cstdint> // <cstdint> requires c++11 support
  13. #include <functional>
  14. #ifndef WITHOUT_NUMPY
  15. # define NPY_NO_DEPRECATED_API NPY_1_7_API_VERSION
  16. # include <numpy/arrayobject.h>
  17. # ifdef WITH_OPENCV
  18. # include <opencv2/opencv.hpp>
  19. # endif // WITH_OPENCV
  20. /*
  21. * A bunch of constants were removed in OpenCV 4 in favour of enum classes, so
  22. * define the ones we need here.
  23. */
  24. # if CV_MAJOR_VERSION > 3
  25. # define CV_BGR2RGB cv::COLOR_BGR2RGB
  26. # define CV_BGRA2RGBA cv::COLOR_BGRA2RGBA
  27. # endif
  28. #endif // WITHOUT_NUMPY
  29. #if PY_MAJOR_VERSION >= 3
  30. # define PyString_FromString PyUnicode_FromString
  31. # define PyInt_FromLong PyLong_FromLong
  32. # define PyString_FromString PyUnicode_FromString
  33. #endif
  34. namespace matplotlibcpp {
  35. namespace detail {
  36. static std::string s_backend;
  37. struct _interpreter {
  38. PyObject *s_python_function_show;
  39. PyObject *s_python_function_close;
  40. PyObject *s_python_function_draw;
  41. PyObject *s_python_function_pause;
  42. PyObject *s_python_function_save;
  43. PyObject *s_python_function_figure;
  44. PyObject *s_python_function_fignum_exists;
  45. PyObject *s_python_function_plot;
  46. PyObject *s_python_function_quiver;
  47. PyObject *s_python_function_semilogx;
  48. PyObject *s_python_function_semilogy;
  49. PyObject *s_python_function_loglog;
  50. PyObject *s_python_function_fill;
  51. PyObject *s_python_function_fill_between;
  52. PyObject *s_python_function_hist;
  53. PyObject *s_python_function_imshow;
  54. PyObject *s_python_function_scatter;
  55. PyObject *s_python_function_boxplot;
  56. PyObject *s_python_function_subplot;
  57. PyObject *s_python_function_subplot2grid;
  58. PyObject *s_python_function_legend;
  59. PyObject *s_python_function_xlim;
  60. PyObject *s_python_function_ion;
  61. PyObject *s_python_function_ginput;
  62. PyObject *s_python_function_ylim;
  63. PyObject *s_python_function_title;
  64. PyObject *s_python_function_axis;
  65. PyObject *s_python_function_axvline;
  66. PyObject *s_python_function_xlabel;
  67. PyObject *s_python_function_ylabel;
  68. PyObject *s_python_function_gca;
  69. PyObject *s_python_function_xticks;
  70. PyObject *s_python_function_yticks;
  71. PyObject *s_python_function_tick_params;
  72. PyObject *s_python_function_grid;
  73. PyObject *s_python_function_clf;
  74. PyObject *s_python_function_errorbar;
  75. PyObject *s_python_function_annotate;
  76. PyObject *s_python_function_tight_layout;
  77. PyObject *s_python_colormap;
  78. PyObject *s_python_empty_tuple;
  79. PyObject *s_python_function_stem;
  80. PyObject *s_python_function_xkcd;
  81. PyObject *s_python_function_text;
  82. PyObject *s_python_function_suptitle;
  83. PyObject *s_python_function_bar;
  84. PyObject *s_python_function_colorbar;
  85. PyObject *s_python_function_subplots_adjust;
  86. /* For now, _interpreter is implemented as a singleton since its currently not possible to have
  87. multiple independent embedded python interpreters without patching the python source code
  88. or starting a separate process for each.
  89. http://bytes.com/topic/python/answers/793370-multiple-independent-python-interpreters-c-c-program
  90. */
  91. static _interpreter& get() {
  92. static _interpreter ctx;
  93. return ctx;
  94. }
  95. PyObject* safe_import(PyObject* module, std::string fname) {
  96. PyObject* fn = PyObject_GetAttrString(module, fname.c_str());
  97. if (!fn)
  98. throw std::runtime_error(std::string("Couldn't find required function: ") + fname);
  99. if (!PyFunction_Check(fn))
  100. throw std::runtime_error(fname + std::string(" is unexpectedly not a PyFunction."));
  101. return fn;
  102. }
  103. private:
  104. #ifndef WITHOUT_NUMPY
  105. # if PY_MAJOR_VERSION >= 3
  106. void *import_numpy() {
  107. import_array(); // initialize C-API
  108. return NULL;
  109. }
  110. # else
  111. void import_numpy() {
  112. import_array(); // initialize C-API
  113. }
  114. # endif
  115. #endif
  116. _interpreter() {
  117. // optional but recommended
  118. #if PY_MAJOR_VERSION >= 3
  119. wchar_t name[] = L"plotting";
  120. #else
  121. char name[] = "plotting";
  122. #endif
  123. Py_SetProgramName(name);
  124. Py_Initialize();
  125. #ifndef WITHOUT_NUMPY
  126. import_numpy(); // initialize numpy C-API
  127. #endif
  128. PyObject* matplotlibname = PyString_FromString("matplotlib");
  129. PyObject* pyplotname = PyString_FromString("matplotlib.pyplot");
  130. PyObject* cmname = PyString_FromString("matplotlib.cm");
  131. PyObject* pylabname = PyString_FromString("pylab");
  132. if (!pyplotname || !pylabname || !matplotlibname || !cmname) {
  133. throw std::runtime_error("couldnt create string");
  134. }
  135. PyObject* matplotlib = PyImport_Import(matplotlibname);
  136. Py_DECREF(matplotlibname);
  137. if (!matplotlib) {
  138. PyErr_Print();
  139. throw std::runtime_error("Error loading module matplotlib!");
  140. }
  141. // matplotlib.use() must be called *before* pylab, matplotlib.pyplot,
  142. // or matplotlib.backends is imported for the first time
  143. if (!s_backend.empty()) {
  144. PyObject_CallMethod(matplotlib, const_cast<char*>("use"), const_cast<char*>("s"), s_backend.c_str());
  145. }
  146. PyObject* pymod = PyImport_Import(pyplotname);
  147. Py_DECREF(pyplotname);
  148. if (!pymod) { throw std::runtime_error("Error loading module matplotlib.pyplot!"); }
  149. s_python_colormap = PyImport_Import(cmname);
  150. Py_DECREF(cmname);
  151. if (!s_python_colormap) { throw std::runtime_error("Error loading module matplotlib.cm!"); }
  152. PyObject* pylabmod = PyImport_Import(pylabname);
  153. Py_DECREF(pylabname);
  154. if (!pylabmod) { throw std::runtime_error("Error loading module pylab!"); }
  155. s_python_function_show = safe_import(pymod, "show");
  156. s_python_function_close = safe_import(pymod, "close");
  157. s_python_function_draw = safe_import(pymod, "draw");
  158. s_python_function_pause = safe_import(pymod, "pause");
  159. s_python_function_figure = safe_import(pymod, "figure");
  160. s_python_function_fignum_exists = safe_import(pymod, "fignum_exists");
  161. s_python_function_plot = safe_import(pymod, "plot");
  162. s_python_function_quiver = safe_import(pymod, "quiver");
  163. s_python_function_semilogx = safe_import(pymod, "semilogx");
  164. s_python_function_semilogy = safe_import(pymod, "semilogy");
  165. s_python_function_loglog = safe_import(pymod, "loglog");
  166. s_python_function_fill = safe_import(pymod, "fill");
  167. s_python_function_fill_between = safe_import(pymod, "fill_between");
  168. s_python_function_hist = safe_import(pymod,"hist");
  169. s_python_function_scatter = safe_import(pymod,"scatter");
  170. s_python_function_boxplot = safe_import(pymod,"boxplot");
  171. s_python_function_subplot = safe_import(pymod, "subplot");
  172. s_python_function_subplot2grid = safe_import(pymod, "subplot2grid");
  173. s_python_function_legend = safe_import(pymod, "legend");
  174. s_python_function_ylim = safe_import(pymod, "ylim");
  175. s_python_function_title = safe_import(pymod, "title");
  176. s_python_function_axis = safe_import(pymod, "axis");
  177. s_python_function_axvline = safe_import(pymod, "axvline");
  178. s_python_function_xlabel = safe_import(pymod, "xlabel");
  179. s_python_function_ylabel = safe_import(pymod, "ylabel");
  180. s_python_function_gca = safe_import(pymod, "gca");
  181. s_python_function_xticks = safe_import(pymod, "xticks");
  182. s_python_function_yticks = safe_import(pymod, "yticks");
  183. s_python_function_tick_params = safe_import(pymod, "tick_params");
  184. s_python_function_grid = safe_import(pymod, "grid");
  185. s_python_function_xlim = safe_import(pymod, "xlim");
  186. s_python_function_ion = safe_import(pymod, "ion");
  187. s_python_function_ginput = safe_import(pymod, "ginput");
  188. s_python_function_save = safe_import(pylabmod, "savefig");
  189. s_python_function_annotate = safe_import(pymod,"annotate");
  190. s_python_function_clf = safe_import(pymod, "clf");
  191. s_python_function_errorbar = safe_import(pymod, "errorbar");
  192. s_python_function_tight_layout = safe_import(pymod, "tight_layout");
  193. s_python_function_stem = safe_import(pymod, "stem");
  194. s_python_function_xkcd = safe_import(pymod, "xkcd");
  195. s_python_function_text = safe_import(pymod, "text");
  196. s_python_function_suptitle = safe_import(pymod, "suptitle");
  197. s_python_function_bar = safe_import(pymod,"bar");
  198. s_python_function_colorbar = PyObject_GetAttrString(pymod, "colorbar");
  199. s_python_function_subplots_adjust = safe_import(pymod,"subplots_adjust");
  200. #ifndef WITHOUT_NUMPY
  201. s_python_function_imshow = safe_import(pymod, "imshow");
  202. #endif
  203. s_python_empty_tuple = PyTuple_New(0);
  204. }
  205. ~_interpreter() {
  206. Py_Finalize();
  207. }
  208. };
  209. } // end namespace detail
  210. /// Select the backend
  211. ///
  212. /// **NOTE:** This must be called before the first plot command to have
  213. /// any effect.
  214. ///
  215. /// Mainly useful to select the non-interactive 'Agg' backend when running
  216. /// matplotlibcpp in headless mode, for example on a machine with no display.
  217. ///
  218. /// See also: https://matplotlib.org/2.0.2/api/matplotlib_configuration_api.html#matplotlib.use
  219. inline void backend(const std::string& name)
  220. {
  221. detail::s_backend = name;
  222. }
  223. inline bool annotate(std::string annotation, double x, double y)
  224. {
  225. detail::_interpreter::get();
  226. PyObject * xy = PyTuple_New(2);
  227. PyObject * str = PyString_FromString(annotation.c_str());
  228. PyTuple_SetItem(xy,0,PyFloat_FromDouble(x));
  229. PyTuple_SetItem(xy,1,PyFloat_FromDouble(y));
  230. PyObject* kwargs = PyDict_New();
  231. PyDict_SetItemString(kwargs, "xy", xy);
  232. PyObject* args = PyTuple_New(1);
  233. PyTuple_SetItem(args, 0, str);
  234. PyObject* res = PyObject_Call(detail::_interpreter::get().s_python_function_annotate, args, kwargs);
  235. Py_DECREF(args);
  236. Py_DECREF(kwargs);
  237. if(res) Py_DECREF(res);
  238. return res;
  239. }
  240. namespace detail {
  241. #ifndef WITHOUT_NUMPY
  242. // Type selector for numpy array conversion
  243. template <typename T> struct select_npy_type { const static NPY_TYPES type = NPY_NOTYPE; }; //Default
  244. template <> struct select_npy_type<double> { const static NPY_TYPES type = NPY_DOUBLE; };
  245. template <> struct select_npy_type<float> { const static NPY_TYPES type = NPY_FLOAT; };
  246. template <> struct select_npy_type<bool> { const static NPY_TYPES type = NPY_BOOL; };
  247. template <> struct select_npy_type<int8_t> { const static NPY_TYPES type = NPY_INT8; };
  248. template <> struct select_npy_type<int16_t> { const static NPY_TYPES type = NPY_SHORT; };
  249. template <> struct select_npy_type<int32_t> { const static NPY_TYPES type = NPY_INT; };
  250. template <> struct select_npy_type<int64_t> { const static NPY_TYPES type = NPY_INT64; };
  251. template <> struct select_npy_type<uint8_t> { const static NPY_TYPES type = NPY_UINT8; };
  252. template <> struct select_npy_type<uint16_t> { const static NPY_TYPES type = NPY_USHORT; };
  253. template <> struct select_npy_type<uint32_t> { const static NPY_TYPES type = NPY_ULONG; };
  254. template <> struct select_npy_type<uint64_t> { const static NPY_TYPES type = NPY_UINT64; };
  255. // Sanity checks; comment them out or change the numpy type below if you're compiling on
  256. // a platform where they don't apply
  257. static_assert(sizeof(long long) == 8);
  258. template <> struct select_npy_type<long long> { const static NPY_TYPES type = NPY_INT64; };
  259. static_assert(sizeof(unsigned long long) == 8);
  260. template <> struct select_npy_type<unsigned long long> { const static NPY_TYPES type = NPY_UINT64; };
  261. // TODO: add int, long, etc.
  262. template<typename Numeric>
  263. PyObject* get_array(const std::vector<Numeric>& v)
  264. {
  265. npy_intp vsize = v.size();
  266. NPY_TYPES type = select_npy_type<Numeric>::type;
  267. if (type == NPY_NOTYPE) {
  268. size_t memsize = v.size()*sizeof(double);
  269. double* dp = static_cast<double*>(::malloc(memsize));
  270. for (size_t i=0; i<v.size(); ++i)
  271. dp[i] = v[i];
  272. PyObject* varray = PyArray_SimpleNewFromData(1, &vsize, NPY_DOUBLE, dp);
  273. PyArray_UpdateFlags(reinterpret_cast<PyArrayObject*>(varray), NPY_ARRAY_OWNDATA);
  274. return varray;
  275. }
  276. PyObject* varray = PyArray_SimpleNewFromData(1, &vsize, type, (void*)(v.data()));
  277. return varray;
  278. }
  279. template<typename Numeric>
  280. PyObject* get_2darray(const std::vector<::std::vector<Numeric>>& v)
  281. {
  282. if (v.size() < 1) throw std::runtime_error("get_2d_array v too small");
  283. npy_intp vsize[2] = {static_cast<npy_intp>(v.size()),
  284. static_cast<npy_intp>(v[0].size())};
  285. PyArrayObject *varray =
  286. (PyArrayObject *)PyArray_SimpleNew(2, vsize, NPY_DOUBLE);
  287. double *vd_begin = static_cast<double *>(PyArray_DATA(varray));
  288. for (const ::std::vector<Numeric> &v_row : v) {
  289. if (v_row.size() != static_cast<size_t>(vsize[1]))
  290. throw std::runtime_error("Missmatched array size");
  291. std::copy(v_row.begin(), v_row.end(), vd_begin);
  292. vd_begin += vsize[1];
  293. }
  294. return reinterpret_cast<PyObject *>(varray);
  295. }
  296. #else // fallback if we don't have numpy: copy every element of the given vector
  297. template<typename Numeric>
  298. PyObject* get_array(const std::vector<Numeric>& v)
  299. {
  300. PyObject* list = PyList_New(v.size());
  301. for(size_t i = 0; i < v.size(); ++i) {
  302. PyList_SetItem(list, i, PyFloat_FromDouble(v.at(i)));
  303. }
  304. return list;
  305. }
  306. #endif // WITHOUT_NUMPY
  307. // sometimes, for labels and such, we need string arrays
  308. inline PyObject * get_array(const std::vector<std::string>& strings)
  309. {
  310. PyObject* list = PyList_New(strings.size());
  311. for (std::size_t i = 0; i < strings.size(); ++i) {
  312. PyList_SetItem(list, i, PyString_FromString(strings[i].c_str()));
  313. }
  314. return list;
  315. }
  316. // not all matplotlib need 2d arrays, some prefer lists of lists
  317. template<typename Numeric>
  318. PyObject* get_listlist(const std::vector<std::vector<Numeric>>& ll)
  319. {
  320. PyObject* listlist = PyList_New(ll.size());
  321. for (std::size_t i = 0; i < ll.size(); ++i) {
  322. PyList_SetItem(listlist, i, get_array(ll[i]));
  323. }
  324. return listlist;
  325. }
  326. } // namespace detail
  327. /// Plot a line through the given x and y data points..
  328. ///
  329. /// See: https://matplotlib.org/3.2.1/api/_as_gen/matplotlib.pyplot.plot.html
  330. template<typename Numeric>
  331. bool plot(const std::vector<Numeric> &x, const std::vector<Numeric> &y, const std::map<std::string, std::string>& keywords)
  332. {
  333. assert(x.size() == y.size());
  334. detail::_interpreter::get();
  335. // using numpy arrays
  336. PyObject* xarray = detail::get_array(x);
  337. PyObject* yarray = detail::get_array(y);
  338. // construct positional args
  339. PyObject* args = PyTuple_New(2);
  340. PyTuple_SetItem(args, 0, xarray);
  341. PyTuple_SetItem(args, 1, yarray);
  342. // construct keyword args
  343. PyObject* kwargs = PyDict_New();
  344. for(std::map<std::string, std::string>::const_iterator it = keywords.begin(); it != keywords.end(); ++it)
  345. {
  346. PyDict_SetItemString(kwargs, it->first.c_str(), PyString_FromString(it->second.c_str()));
  347. }
  348. PyObject* res = PyObject_Call(detail::_interpreter::get().s_python_function_plot, args, kwargs);
  349. Py_DECREF(args);
  350. Py_DECREF(kwargs);
  351. if(res) Py_DECREF(res);
  352. return res;
  353. }
  354. // TODO - it should be possible to make this work by implementing
  355. // a non-numpy alternative for `detail::get_2darray()`.
  356. #ifndef WITHOUT_NUMPY
  357. template <typename Numeric>
  358. void plot_surface(const std::vector<::std::vector<Numeric>> &x,
  359. const std::vector<::std::vector<Numeric>> &y,
  360. const std::vector<::std::vector<Numeric>> &z,
  361. const std::map<std::string, std::string> &keywords =
  362. std::map<std::string, std::string>())
  363. {
  364. detail::_interpreter::get();
  365. // We lazily load the modules here the first time this function is called
  366. // because I'm not sure that we can assume "matplotlib installed" implies
  367. // "mpl_toolkits installed" on all platforms, and we don't want to require
  368. // it for people who don't need 3d plots.
  369. static PyObject *mpl_toolkitsmod = nullptr, *axis3dmod = nullptr;
  370. if (!mpl_toolkitsmod) {
  371. detail::_interpreter::get();
  372. PyObject* mpl_toolkits = PyString_FromString("mpl_toolkits");
  373. PyObject* axis3d = PyString_FromString("mpl_toolkits.mplot3d");
  374. if (!mpl_toolkits || !axis3d) { throw std::runtime_error("couldnt create string"); }
  375. mpl_toolkitsmod = PyImport_Import(mpl_toolkits);
  376. Py_DECREF(mpl_toolkits);
  377. if (!mpl_toolkitsmod) { throw std::runtime_error("Error loading module mpl_toolkits!"); }
  378. axis3dmod = PyImport_Import(axis3d);
  379. Py_DECREF(axis3d);
  380. if (!axis3dmod) { throw std::runtime_error("Error loading module mpl_toolkits.mplot3d!"); }
  381. }
  382. assert(x.size() == y.size());
  383. assert(y.size() == z.size());
  384. // using numpy arrays
  385. PyObject *xarray = detail::get_2darray(x);
  386. PyObject *yarray = detail::get_2darray(y);
  387. PyObject *zarray = detail::get_2darray(z);
  388. // construct positional args
  389. PyObject *args = PyTuple_New(3);
  390. PyTuple_SetItem(args, 0, xarray);
  391. PyTuple_SetItem(args, 1, yarray);
  392. PyTuple_SetItem(args, 2, zarray);
  393. // Build up the kw args.
  394. PyObject *kwargs = PyDict_New();
  395. PyDict_SetItemString(kwargs, "rstride", PyInt_FromLong(1));
  396. PyDict_SetItemString(kwargs, "cstride", PyInt_FromLong(1));
  397. PyObject *python_colormap_coolwarm = PyObject_GetAttrString(
  398. detail::_interpreter::get().s_python_colormap, "coolwarm");
  399. PyDict_SetItemString(kwargs, "cmap", python_colormap_coolwarm);
  400. for (std::map<std::string, std::string>::const_iterator it = keywords.begin();
  401. it != keywords.end(); ++it) {
  402. PyDict_SetItemString(kwargs, it->first.c_str(),
  403. PyString_FromString(it->second.c_str()));
  404. }
  405. PyObject *fig =
  406. PyObject_CallObject(detail::_interpreter::get().s_python_function_figure,
  407. detail::_interpreter::get().s_python_empty_tuple);
  408. if (!fig) throw std::runtime_error("Call to figure() failed.");
  409. PyObject *gca_kwargs = PyDict_New();
  410. PyDict_SetItemString(gca_kwargs, "projection", PyString_FromString("3d"));
  411. PyObject *gca = PyObject_GetAttrString(fig, "gca");
  412. if (!gca) throw std::runtime_error("No gca");
  413. Py_INCREF(gca);
  414. PyObject *axis = PyObject_Call(
  415. gca, detail::_interpreter::get().s_python_empty_tuple, gca_kwargs);
  416. if (!axis) throw std::runtime_error("No axis");
  417. Py_INCREF(axis);
  418. Py_DECREF(gca);
  419. Py_DECREF(gca_kwargs);
  420. PyObject *plot_surface = PyObject_GetAttrString(axis, "plot_surface");
  421. if (!plot_surface) throw std::runtime_error("No surface");
  422. Py_INCREF(plot_surface);
  423. PyObject *res = PyObject_Call(plot_surface, args, kwargs);
  424. if (!res) throw std::runtime_error("failed surface");
  425. Py_DECREF(plot_surface);
  426. Py_DECREF(axis);
  427. Py_DECREF(args);
  428. Py_DECREF(kwargs);
  429. if (res) Py_DECREF(res);
  430. }
  431. #endif // WITHOUT_NUMPY
  432. template <typename Numeric>
  433. void plot3(const std::vector<Numeric> &x,
  434. const std::vector<Numeric> &y,
  435. const std::vector<Numeric> &z,
  436. const std::map<std::string, std::string> &keywords =
  437. std::map<std::string, std::string>())
  438. {
  439. detail::_interpreter::get();
  440. // Same as with plot_surface: We lazily load the modules here the first time
  441. // this function is called because I'm not sure that we can assume "matplotlib
  442. // installed" implies "mpl_toolkits installed" on all platforms, and we don't
  443. // want to require it for people who don't need 3d plots.
  444. static PyObject *mpl_toolkitsmod = nullptr, *axis3dmod = nullptr;
  445. if (!mpl_toolkitsmod) {
  446. detail::_interpreter::get();
  447. PyObject* mpl_toolkits = PyString_FromString("mpl_toolkits");
  448. PyObject* axis3d = PyString_FromString("mpl_toolkits.mplot3d");
  449. if (!mpl_toolkits || !axis3d) { throw std::runtime_error("couldnt create string"); }
  450. mpl_toolkitsmod = PyImport_Import(mpl_toolkits);
  451. Py_DECREF(mpl_toolkits);
  452. if (!mpl_toolkitsmod) { throw std::runtime_error("Error loading module mpl_toolkits!"); }
  453. axis3dmod = PyImport_Import(axis3d);
  454. Py_DECREF(axis3d);
  455. if (!axis3dmod) { throw std::runtime_error("Error loading module mpl_toolkits.mplot3d!"); }
  456. }
  457. assert(x.size() == y.size());
  458. assert(y.size() == z.size());
  459. PyObject *xarray = detail::get_array(x);
  460. PyObject *yarray = detail::get_array(y);
  461. PyObject *zarray = detail::get_array(z);
  462. // construct positional args
  463. PyObject *args = PyTuple_New(3);
  464. PyTuple_SetItem(args, 0, xarray);
  465. PyTuple_SetItem(args, 1, yarray);
  466. PyTuple_SetItem(args, 2, zarray);
  467. // Build up the kw args.
  468. PyObject *kwargs = PyDict_New();
  469. for (std::map<std::string, std::string>::const_iterator it = keywords.begin();
  470. it != keywords.end(); ++it) {
  471. PyDict_SetItemString(kwargs, it->first.c_str(),
  472. PyString_FromString(it->second.c_str()));
  473. }
  474. PyObject *fig =
  475. PyObject_CallObject(detail::_interpreter::get().s_python_function_figure,
  476. detail::_interpreter::get().s_python_empty_tuple);
  477. if (!fig) throw std::runtime_error("Call to figure() failed.");
  478. PyObject *gca_kwargs = PyDict_New();
  479. PyDict_SetItemString(gca_kwargs, "projection", PyString_FromString("3d"));
  480. PyObject *gca = PyObject_GetAttrString(fig, "gca");
  481. if (!gca) throw std::runtime_error("No gca");
  482. Py_INCREF(gca);
  483. PyObject *axis = PyObject_Call(
  484. gca, detail::_interpreter::get().s_python_empty_tuple, gca_kwargs);
  485. if (!axis) throw std::runtime_error("No axis");
  486. Py_INCREF(axis);
  487. Py_DECREF(gca);
  488. Py_DECREF(gca_kwargs);
  489. PyObject *plot3 = PyObject_GetAttrString(axis, "plot");
  490. if (!plot3) throw std::runtime_error("No 3D line plot");
  491. Py_INCREF(plot3);
  492. PyObject *res = PyObject_Call(plot3, args, kwargs);
  493. if (!res) throw std::runtime_error("Failed 3D line plot");
  494. Py_DECREF(plot3);
  495. Py_DECREF(axis);
  496. Py_DECREF(args);
  497. Py_DECREF(kwargs);
  498. if (res) Py_DECREF(res);
  499. }
  500. template<typename Numeric>
  501. bool stem(const std::vector<Numeric> &x, const std::vector<Numeric> &y, const std::map<std::string, std::string>& keywords)
  502. {
  503. assert(x.size() == y.size());
  504. detail::_interpreter::get();
  505. // using numpy arrays
  506. PyObject* xarray = detail::get_array(x);
  507. PyObject* yarray = detail::get_array(y);
  508. // construct positional args
  509. PyObject* args = PyTuple_New(2);
  510. PyTuple_SetItem(args, 0, xarray);
  511. PyTuple_SetItem(args, 1, yarray);
  512. // construct keyword args
  513. PyObject* kwargs = PyDict_New();
  514. for (std::map<std::string, std::string>::const_iterator it =
  515. keywords.begin(); it != keywords.end(); ++it) {
  516. PyDict_SetItemString(kwargs, it->first.c_str(),
  517. PyString_FromString(it->second.c_str()));
  518. }
  519. PyObject* res = PyObject_Call(
  520. detail::_interpreter::get().s_python_function_stem, args, kwargs);
  521. Py_DECREF(args);
  522. Py_DECREF(kwargs);
  523. if (res)
  524. Py_DECREF(res);
  525. return res;
  526. }
  527. template< typename Numeric >
  528. bool fill(const std::vector<Numeric>& x, const std::vector<Numeric>& y, const std::map<std::string, std::string>& keywords)
  529. {
  530. assert(x.size() == y.size());
  531. detail::_interpreter::get();
  532. // using numpy arrays
  533. PyObject* xarray = detail::get_array(x);
  534. PyObject* yarray = detail::get_array(y);
  535. // construct positional args
  536. PyObject* args = PyTuple_New(2);
  537. PyTuple_SetItem(args, 0, xarray);
  538. PyTuple_SetItem(args, 1, yarray);
  539. // construct keyword args
  540. PyObject* kwargs = PyDict_New();
  541. for (auto it = keywords.begin(); it != keywords.end(); ++it) {
  542. PyDict_SetItemString(kwargs, it->first.c_str(), PyUnicode_FromString(it->second.c_str()));
  543. }
  544. PyObject* res = PyObject_Call(detail::_interpreter::get().s_python_function_fill, args, kwargs);
  545. Py_DECREF(args);
  546. Py_DECREF(kwargs);
  547. if (res) Py_DECREF(res);
  548. return res;
  549. }
  550. template< typename Numeric >
  551. bool fill_between(const std::vector<Numeric>& x, const std::vector<Numeric>& y1, const std::vector<Numeric>& y2, const std::map<std::string, std::string>& keywords)
  552. {
  553. assert(x.size() == y1.size());
  554. assert(x.size() == y2.size());
  555. detail::_interpreter::get();
  556. // using numpy arrays
  557. PyObject* xarray = detail::get_array(x);
  558. PyObject* y1array = detail::get_array(y1);
  559. PyObject* y2array = detail::get_array(y2);
  560. // construct positional args
  561. PyObject* args = PyTuple_New(3);
  562. PyTuple_SetItem(args, 0, xarray);
  563. PyTuple_SetItem(args, 1, y1array);
  564. PyTuple_SetItem(args, 2, y2array);
  565. // construct keyword args
  566. PyObject* kwargs = PyDict_New();
  567. for(std::map<std::string, std::string>::const_iterator it = keywords.begin(); it != keywords.end(); ++it) {
  568. PyDict_SetItemString(kwargs, it->first.c_str(), PyUnicode_FromString(it->second.c_str()));
  569. }
  570. PyObject* res = PyObject_Call(detail::_interpreter::get().s_python_function_fill_between, args, kwargs);
  571. Py_DECREF(args);
  572. Py_DECREF(kwargs);
  573. if(res) Py_DECREF(res);
  574. return res;
  575. }
  576. template< typename Numeric>
  577. bool hist(const std::vector<Numeric>& y, long bins=10,std::string color="b",
  578. double alpha=1.0, bool cumulative=false)
  579. {
  580. detail::_interpreter::get();
  581. PyObject* yarray = detail::get_array(y);
  582. PyObject* kwargs = PyDict_New();
  583. PyDict_SetItemString(kwargs, "bins", PyLong_FromLong(bins));
  584. PyDict_SetItemString(kwargs, "color", PyString_FromString(color.c_str()));
  585. PyDict_SetItemString(kwargs, "alpha", PyFloat_FromDouble(alpha));
  586. PyDict_SetItemString(kwargs, "cumulative", cumulative ? Py_True : Py_False);
  587. PyObject* plot_args = PyTuple_New(1);
  588. PyTuple_SetItem(plot_args, 0, yarray);
  589. PyObject* res = PyObject_Call(detail::_interpreter::get().s_python_function_hist, plot_args, kwargs);
  590. Py_DECREF(plot_args);
  591. Py_DECREF(kwargs);
  592. if(res) Py_DECREF(res);
  593. return res;
  594. }
  595. #ifndef WITHOUT_NUMPY
  596. namespace detail {
  597. inline void imshow(void *ptr, const NPY_TYPES type, const int rows, const int columns, const int colors, const std::map<std::string, std::string> &keywords, PyObject** out)
  598. {
  599. assert(type == NPY_UINT8 || type == NPY_FLOAT);
  600. assert(colors == 1 || colors == 3 || colors == 4);
  601. detail::_interpreter::get();
  602. // construct args
  603. npy_intp dims[3] = { rows, columns, colors };
  604. PyObject *args = PyTuple_New(1);
  605. PyTuple_SetItem(args, 0, PyArray_SimpleNewFromData(colors == 1 ? 2 : 3, dims, type, ptr));
  606. // construct keyword args
  607. PyObject* kwargs = PyDict_New();
  608. for(std::map<std::string, std::string>::const_iterator it = keywords.begin(); it != keywords.end(); ++it)
  609. {
  610. PyDict_SetItemString(kwargs, it->first.c_str(), PyUnicode_FromString(it->second.c_str()));
  611. }
  612. PyObject *res = PyObject_Call(detail::_interpreter::get().s_python_function_imshow, args, kwargs);
  613. Py_DECREF(args);
  614. Py_DECREF(kwargs);
  615. if (!res)
  616. throw std::runtime_error("Call to imshow() failed");
  617. if (out)
  618. *out = res;
  619. else
  620. Py_DECREF(res);
  621. }
  622. } // namespace detail
  623. inline void imshow(const unsigned char *ptr, const int rows, const int columns, const int colors, const std::map<std::string, std::string> &keywords = {}, PyObject** out = nullptr)
  624. {
  625. detail::imshow((void *) ptr, NPY_UINT8, rows, columns, colors, keywords, out);
  626. }
  627. inline void imshow(const float *ptr, const int rows, const int columns, const int colors, const std::map<std::string, std::string> &keywords = {}, PyObject** out = nullptr)
  628. {
  629. detail::imshow((void *) ptr, NPY_FLOAT, rows, columns, colors, keywords, out);
  630. }
  631. #ifdef WITH_OPENCV
  632. void imshow(const cv::Mat &image, const std::map<std::string, std::string> &keywords = {})
  633. {
  634. // Convert underlying type of matrix, if needed
  635. cv::Mat image2;
  636. NPY_TYPES npy_type = NPY_UINT8;
  637. switch (image.type() & CV_MAT_DEPTH_MASK) {
  638. case CV_8U:
  639. image2 = image;
  640. break;
  641. case CV_32F:
  642. image2 = image;
  643. npy_type = NPY_FLOAT;
  644. break;
  645. default:
  646. image.convertTo(image2, CV_MAKETYPE(CV_8U, image.channels()));
  647. }
  648. // If color image, convert from BGR to RGB
  649. switch (image2.channels()) {
  650. case 3:
  651. cv::cvtColor(image2, image2, CV_BGR2RGB);
  652. break;
  653. case 4:
  654. cv::cvtColor(image2, image2, CV_BGRA2RGBA);
  655. }
  656. detail::imshow(image2.data, npy_type, image2.rows, image2.cols, image2.channels(), keywords);
  657. }
  658. #endif // WITH_OPENCV
  659. #endif // WITHOUT_NUMPY
  660. template<typename NumericX, typename NumericY>
  661. bool scatter(const std::vector<NumericX>& x,
  662. const std::vector<NumericY>& y,
  663. const double s=1.0, // The marker size in points**2
  664. const std::map<std::string, std::string> & keywords = {})
  665. {
  666. detail::_interpreter::get();
  667. assert(x.size() == y.size());
  668. PyObject* xarray = detail::get_array(x);
  669. PyObject* yarray = detail::get_array(y);
  670. PyObject* kwargs = PyDict_New();
  671. PyDict_SetItemString(kwargs, "s", PyLong_FromLong(s));
  672. for (const auto& it : keywords)
  673. {
  674. PyDict_SetItemString(kwargs, it.first.c_str(), PyString_FromString(it.second.c_str()));
  675. }
  676. PyObject* plot_args = PyTuple_New(2);
  677. PyTuple_SetItem(plot_args, 0, xarray);
  678. PyTuple_SetItem(plot_args, 1, yarray);
  679. PyObject* res = PyObject_Call(detail::_interpreter::get().s_python_function_scatter, plot_args, kwargs);
  680. Py_DECREF(plot_args);
  681. Py_DECREF(kwargs);
  682. if(res) Py_DECREF(res);
  683. return res;
  684. }
  685. template<typename Numeric>
  686. bool boxplot(const std::vector<std::vector<Numeric>>& data,
  687. const std::vector<std::string>& labels = {},
  688. const std::map<std::string, std::string> & keywords = {})
  689. {
  690. detail::_interpreter::get();
  691. PyObject* listlist = detail::get_listlist(data);
  692. PyObject* args = PyTuple_New(1);
  693. PyTuple_SetItem(args, 0, listlist);
  694. PyObject* kwargs = PyDict_New();
  695. // kwargs needs the labels, if there are (the correct number of) labels
  696. if (!labels.empty() && labels.size() == data.size()) {
  697. PyDict_SetItemString(kwargs, "labels", detail::get_array(labels));
  698. }
  699. // take care of the remaining keywords
  700. for (const auto& it : keywords)
  701. {
  702. PyDict_SetItemString(kwargs, it.first.c_str(), PyString_FromString(it.second.c_str()));
  703. }
  704. PyObject* res = PyObject_Call(detail::_interpreter::get().s_python_function_boxplot, args, kwargs);
  705. Py_DECREF(args);
  706. Py_DECREF(kwargs);
  707. if(res) Py_DECREF(res);
  708. return res;
  709. }
  710. template<typename Numeric>
  711. bool boxplot(const std::vector<Numeric>& data,
  712. const std::map<std::string, std::string> & keywords = {})
  713. {
  714. detail::_interpreter::get();
  715. PyObject* vector = detail::get_array(data);
  716. PyObject* args = PyTuple_New(1);
  717. PyTuple_SetItem(args, 0, vector);
  718. PyObject* kwargs = PyDict_New();
  719. for (const auto& it : keywords)
  720. {
  721. PyDict_SetItemString(kwargs, it.first.c_str(), PyString_FromString(it.second.c_str()));
  722. }
  723. PyObject* res = PyObject_Call(detail::_interpreter::get().s_python_function_boxplot, args, kwargs);
  724. Py_DECREF(args);
  725. Py_DECREF(kwargs);
  726. if(res) Py_DECREF(res);
  727. return res;
  728. }
  729. template <typename Numeric>
  730. bool bar(const std::vector<Numeric> & x,
  731. const std::vector<Numeric> & y,
  732. std::string ec = "black",
  733. std::string ls = "-",
  734. double lw = 1.0,
  735. const std::map<std::string, std::string> & keywords = {})
  736. {
  737. detail::_interpreter::get();
  738. PyObject * xarray = detail::get_array(x);
  739. PyObject * yarray = detail::get_array(y);
  740. PyObject * kwargs = PyDict_New();
  741. PyDict_SetItemString(kwargs, "ec", PyString_FromString(ec.c_str()));
  742. PyDict_SetItemString(kwargs, "ls", PyString_FromString(ls.c_str()));
  743. PyDict_SetItemString(kwargs, "lw", PyFloat_FromDouble(lw));
  744. for (std::map<std::string, std::string>::const_iterator it =
  745. keywords.begin();
  746. it != keywords.end();
  747. ++it) {
  748. PyDict_SetItemString(
  749. kwargs, it->first.c_str(), PyUnicode_FromString(it->second.c_str()));
  750. }
  751. PyObject * plot_args = PyTuple_New(2);
  752. PyTuple_SetItem(plot_args, 0, xarray);
  753. PyTuple_SetItem(plot_args, 1, yarray);
  754. PyObject * res = PyObject_Call(
  755. detail::_interpreter::get().s_python_function_bar, plot_args, kwargs);
  756. Py_DECREF(plot_args);
  757. Py_DECREF(kwargs);
  758. if (res) Py_DECREF(res);
  759. return res;
  760. }
  761. template <typename Numeric>
  762. bool bar(const std::vector<Numeric> & y,
  763. std::string ec = "black",
  764. std::string ls = "-",
  765. double lw = 1.0,
  766. const std::map<std::string, std::string> & keywords = {})
  767. {
  768. using T = typename std::remove_reference<decltype(y)>::type::value_type;
  769. detail::_interpreter::get();
  770. std::vector<T> x;
  771. for (std::size_t i = 0; i < y.size(); i++) { x.push_back(i); }
  772. return bar(x, y, ec, ls, lw, keywords);
  773. }
  774. inline bool subplots_adjust(const std::map<std::string, double>& keywords = {})
  775. {
  776. detail::_interpreter::get();
  777. PyObject* kwargs = PyDict_New();
  778. for (std::map<std::string, double>::const_iterator it =
  779. keywords.begin(); it != keywords.end(); ++it) {
  780. PyDict_SetItemString(kwargs, it->first.c_str(),
  781. PyFloat_FromDouble(it->second));
  782. }
  783. PyObject* plot_args = PyTuple_New(0);
  784. PyObject* res = PyObject_Call(detail::_interpreter::get().s_python_function_subplots_adjust, plot_args, kwargs);
  785. Py_DECREF(plot_args);
  786. Py_DECREF(kwargs);
  787. if(res) Py_DECREF(res);
  788. return res;
  789. }
  790. template< typename Numeric>
  791. bool named_hist(std::string label,const std::vector<Numeric>& y, long bins=10, std::string color="b", double alpha=1.0)
  792. {
  793. detail::_interpreter::get();
  794. PyObject* yarray = detail::get_array(y);
  795. PyObject* kwargs = PyDict_New();
  796. PyDict_SetItemString(kwargs, "label", PyString_FromString(label.c_str()));
  797. PyDict_SetItemString(kwargs, "bins", PyLong_FromLong(bins));
  798. PyDict_SetItemString(kwargs, "color", PyString_FromString(color.c_str()));
  799. PyDict_SetItemString(kwargs, "alpha", PyFloat_FromDouble(alpha));
  800. PyObject* plot_args = PyTuple_New(1);
  801. PyTuple_SetItem(plot_args, 0, yarray);
  802. PyObject* res = PyObject_Call(detail::_interpreter::get().s_python_function_hist, plot_args, kwargs);
  803. Py_DECREF(plot_args);
  804. Py_DECREF(kwargs);
  805. if(res) Py_DECREF(res);
  806. return res;
  807. }
  808. template<typename NumericX, typename NumericY>
  809. bool plot(const std::vector<NumericX>& x, const std::vector<NumericY>& y, const std::string& s = "")
  810. {
  811. assert(x.size() == y.size());
  812. detail::_interpreter::get();
  813. PyObject* xarray = detail::get_array(x);
  814. PyObject* yarray = detail::get_array(y);
  815. PyObject* pystring = PyString_FromString(s.c_str());
  816. PyObject* plot_args = PyTuple_New(3);
  817. PyTuple_SetItem(plot_args, 0, xarray);
  818. PyTuple_SetItem(plot_args, 1, yarray);
  819. PyTuple_SetItem(plot_args, 2, pystring);
  820. PyObject* res = PyObject_CallObject(detail::_interpreter::get().s_python_function_plot, plot_args);
  821. Py_DECREF(plot_args);
  822. if(res) Py_DECREF(res);
  823. return res;
  824. }
  825. template<typename NumericX, typename NumericY, typename NumericU, typename NumericW>
  826. bool quiver(const std::vector<NumericX>& x, const std::vector<NumericY>& y, const std::vector<NumericU>& u, const std::vector<NumericW>& w, const std::map<std::string, std::string>& keywords = {})
  827. {
  828. assert(x.size() == y.size() && x.size() == u.size() && u.size() == w.size());
  829. detail::_interpreter::get();
  830. PyObject* xarray = detail::get_array(x);
  831. PyObject* yarray = detail::get_array(y);
  832. PyObject* uarray = detail::get_array(u);
  833. PyObject* warray = detail::get_array(w);
  834. PyObject* plot_args = PyTuple_New(4);
  835. PyTuple_SetItem(plot_args, 0, xarray);
  836. PyTuple_SetItem(plot_args, 1, yarray);
  837. PyTuple_SetItem(plot_args, 2, uarray);
  838. PyTuple_SetItem(plot_args, 3, warray);
  839. // construct keyword args
  840. PyObject* kwargs = PyDict_New();
  841. for(std::map<std::string, std::string>::const_iterator it = keywords.begin(); it != keywords.end(); ++it)
  842. {
  843. PyDict_SetItemString(kwargs, it->first.c_str(), PyUnicode_FromString(it->second.c_str()));
  844. }
  845. PyObject* res = PyObject_Call(
  846. detail::_interpreter::get().s_python_function_quiver, plot_args, kwargs);
  847. Py_DECREF(kwargs);
  848. Py_DECREF(plot_args);
  849. if (res)
  850. Py_DECREF(res);
  851. return res;
  852. }
  853. template<typename NumericX, typename NumericY>
  854. bool stem(const std::vector<NumericX>& x, const std::vector<NumericY>& y, const std::string& s = "")
  855. {
  856. assert(x.size() == y.size());
  857. detail::_interpreter::get();
  858. PyObject* xarray = detail::get_array(x);
  859. PyObject* yarray = detail::get_array(y);
  860. PyObject* pystring = PyString_FromString(s.c_str());
  861. PyObject* plot_args = PyTuple_New(3);
  862. PyTuple_SetItem(plot_args, 0, xarray);
  863. PyTuple_SetItem(plot_args, 1, yarray);
  864. PyTuple_SetItem(plot_args, 2, pystring);
  865. PyObject* res = PyObject_CallObject(
  866. detail::_interpreter::get().s_python_function_stem, plot_args);
  867. Py_DECREF(plot_args);
  868. if (res)
  869. Py_DECREF(res);
  870. return res;
  871. }
  872. template<typename NumericX, typename NumericY>
  873. bool semilogx(const std::vector<NumericX>& x, const std::vector<NumericY>& y, const std::string& s = "")
  874. {
  875. assert(x.size() == y.size());
  876. detail::_interpreter::get();
  877. PyObject* xarray = detail::get_array(x);
  878. PyObject* yarray = detail::get_array(y);
  879. PyObject* pystring = PyString_FromString(s.c_str());
  880. PyObject* plot_args = PyTuple_New(3);
  881. PyTuple_SetItem(plot_args, 0, xarray);
  882. PyTuple_SetItem(plot_args, 1, yarray);
  883. PyTuple_SetItem(plot_args, 2, pystring);
  884. PyObject* res = PyObject_CallObject(detail::_interpreter::get().s_python_function_semilogx, plot_args);
  885. Py_DECREF(plot_args);
  886. if(res) Py_DECREF(res);
  887. return res;
  888. }
  889. template<typename NumericX, typename NumericY>
  890. bool semilogy(const std::vector<NumericX>& x, const std::vector<NumericY>& y, const std::string& s = "")
  891. {
  892. assert(x.size() == y.size());
  893. detail::_interpreter::get();
  894. PyObject* xarray = detail::get_array(x);
  895. PyObject* yarray = detail::get_array(y);
  896. PyObject* pystring = PyString_FromString(s.c_str());
  897. PyObject* plot_args = PyTuple_New(3);
  898. PyTuple_SetItem(plot_args, 0, xarray);
  899. PyTuple_SetItem(plot_args, 1, yarray);
  900. PyTuple_SetItem(plot_args, 2, pystring);
  901. PyObject* res = PyObject_CallObject(detail::_interpreter::get().s_python_function_semilogy, plot_args);
  902. Py_DECREF(plot_args);
  903. if(res) Py_DECREF(res);
  904. return res;
  905. }
  906. template<typename NumericX, typename NumericY>
  907. bool loglog(const std::vector<NumericX>& x, const std::vector<NumericY>& y, const std::string& s = "")
  908. {
  909. assert(x.size() == y.size());
  910. detail::_interpreter::get();
  911. PyObject* xarray = detail::get_array(x);
  912. PyObject* yarray = detail::get_array(y);
  913. PyObject* pystring = PyString_FromString(s.c_str());
  914. PyObject* plot_args = PyTuple_New(3);
  915. PyTuple_SetItem(plot_args, 0, xarray);
  916. PyTuple_SetItem(plot_args, 1, yarray);
  917. PyTuple_SetItem(plot_args, 2, pystring);
  918. PyObject* res = PyObject_CallObject(detail::_interpreter::get().s_python_function_loglog, plot_args);
  919. Py_DECREF(plot_args);
  920. if(res) Py_DECREF(res);
  921. return res;
  922. }
  923. template<typename NumericX, typename NumericY>
  924. bool errorbar(const std::vector<NumericX> &x, const std::vector<NumericY> &y, const std::vector<NumericX> &yerr, const std::map<std::string, std::string> &keywords = {})
  925. {
  926. assert(x.size() == y.size());
  927. detail::_interpreter::get();
  928. PyObject* xarray = detail::get_array(x);
  929. PyObject* yarray = detail::get_array(y);
  930. PyObject* yerrarray = detail::get_array(yerr);
  931. // construct keyword args
  932. PyObject* kwargs = PyDict_New();
  933. for(std::map<std::string, std::string>::const_iterator it = keywords.begin(); it != keywords.end(); ++it)
  934. {
  935. PyDict_SetItemString(kwargs, it->first.c_str(), PyString_FromString(it->second.c_str()));
  936. }
  937. PyDict_SetItemString(kwargs, "yerr", yerrarray);
  938. PyObject *plot_args = PyTuple_New(2);
  939. PyTuple_SetItem(plot_args, 0, xarray);
  940. PyTuple_SetItem(plot_args, 1, yarray);
  941. PyObject *res = PyObject_Call(detail::_interpreter::get().s_python_function_errorbar, plot_args, kwargs);
  942. Py_DECREF(kwargs);
  943. Py_DECREF(plot_args);
  944. if (res)
  945. Py_DECREF(res);
  946. else
  947. throw std::runtime_error("Call to errorbar() failed.");
  948. return res;
  949. }
  950. template<typename Numeric>
  951. bool named_plot(const std::string& name, const std::vector<Numeric>& y, const std::string& format = "")
  952. {
  953. detail::_interpreter::get();
  954. PyObject* kwargs = PyDict_New();
  955. PyDict_SetItemString(kwargs, "label", PyString_FromString(name.c_str()));
  956. PyObject* yarray = detail::get_array(y);
  957. PyObject* pystring = PyString_FromString(format.c_str());
  958. PyObject* plot_args = PyTuple_New(2);
  959. PyTuple_SetItem(plot_args, 0, yarray);
  960. PyTuple_SetItem(plot_args, 1, pystring);
  961. PyObject* res = PyObject_Call(detail::_interpreter::get().s_python_function_plot, plot_args, kwargs);
  962. Py_DECREF(kwargs);
  963. Py_DECREF(plot_args);
  964. if (res) Py_DECREF(res);
  965. return res;
  966. }
  967. template<typename Numeric>
  968. bool named_plot(const std::string& name, const std::vector<Numeric>& x, const std::vector<Numeric>& y, const std::string& format = "")
  969. {
  970. detail::_interpreter::get();
  971. PyObject* kwargs = PyDict_New();
  972. PyDict_SetItemString(kwargs, "label", PyString_FromString(name.c_str()));
  973. PyObject* xarray = detail::get_array(x);
  974. PyObject* yarray = detail::get_array(y);
  975. PyObject* pystring = PyString_FromString(format.c_str());
  976. PyObject* plot_args = PyTuple_New(3);
  977. PyTuple_SetItem(plot_args, 0, xarray);
  978. PyTuple_SetItem(plot_args, 1, yarray);
  979. PyTuple_SetItem(plot_args, 2, pystring);
  980. PyObject* res = PyObject_Call(detail::_interpreter::get().s_python_function_plot, plot_args, kwargs);
  981. Py_DECREF(kwargs);
  982. Py_DECREF(plot_args);
  983. if (res) Py_DECREF(res);
  984. return res;
  985. }
  986. template<typename Numeric>
  987. bool named_semilogx(const std::string& name, const std::vector<Numeric>& x, const std::vector<Numeric>& y, const std::string& format = "")
  988. {
  989. detail::_interpreter::get();
  990. PyObject* kwargs = PyDict_New();
  991. PyDict_SetItemString(kwargs, "label", PyString_FromString(name.c_str()));
  992. PyObject* xarray = detail::get_array(x);
  993. PyObject* yarray = detail::get_array(y);
  994. PyObject* pystring = PyString_FromString(format.c_str());
  995. PyObject* plot_args = PyTuple_New(3);
  996. PyTuple_SetItem(plot_args, 0, xarray);
  997. PyTuple_SetItem(plot_args, 1, yarray);
  998. PyTuple_SetItem(plot_args, 2, pystring);
  999. PyObject* res = PyObject_Call(detail::_interpreter::get().s_python_function_semilogx, plot_args, kwargs);
  1000. Py_DECREF(kwargs);
  1001. Py_DECREF(plot_args);
  1002. if (res) Py_DECREF(res);
  1003. return res;
  1004. }
  1005. template<typename Numeric>
  1006. bool named_semilogy(const std::string& name, const std::vector<Numeric>& x, const std::vector<Numeric>& y, const std::string& format = "")
  1007. {
  1008. detail::_interpreter::get();
  1009. PyObject* kwargs = PyDict_New();
  1010. PyDict_SetItemString(kwargs, "label", PyString_FromString(name.c_str()));
  1011. PyObject* xarray = detail::get_array(x);
  1012. PyObject* yarray = detail::get_array(y);
  1013. PyObject* pystring = PyString_FromString(format.c_str());
  1014. PyObject* plot_args = PyTuple_New(3);
  1015. PyTuple_SetItem(plot_args, 0, xarray);
  1016. PyTuple_SetItem(plot_args, 1, yarray);
  1017. PyTuple_SetItem(plot_args, 2, pystring);
  1018. PyObject* res = PyObject_Call(detail::_interpreter::get().s_python_function_semilogy, plot_args, kwargs);
  1019. Py_DECREF(kwargs);
  1020. Py_DECREF(plot_args);
  1021. if (res) Py_DECREF(res);
  1022. return res;
  1023. }
  1024. template<typename Numeric>
  1025. bool named_loglog(const std::string& name, const std::vector<Numeric>& x, const std::vector<Numeric>& y, const std::string& format = "")
  1026. {
  1027. detail::_interpreter::get();
  1028. PyObject* kwargs = PyDict_New();
  1029. PyDict_SetItemString(kwargs, "label", PyString_FromString(name.c_str()));
  1030. PyObject* xarray = detail::get_array(x);
  1031. PyObject* yarray = detail::get_array(y);
  1032. PyObject* pystring = PyString_FromString(format.c_str());
  1033. PyObject* plot_args = PyTuple_New(3);
  1034. PyTuple_SetItem(plot_args, 0, xarray);
  1035. PyTuple_SetItem(plot_args, 1, yarray);
  1036. PyTuple_SetItem(plot_args, 2, pystring);
  1037. PyObject* res = PyObject_Call(detail::_interpreter::get().s_python_function_loglog, plot_args, kwargs);
  1038. Py_DECREF(kwargs);
  1039. Py_DECREF(plot_args);
  1040. if (res) Py_DECREF(res);
  1041. return res;
  1042. }
  1043. template<typename Numeric>
  1044. bool plot(const std::vector<Numeric>& y, const std::string& format = "")
  1045. {
  1046. std::vector<Numeric> x(y.size());
  1047. for(size_t i=0; i<x.size(); ++i) x.at(i) = i;
  1048. return plot(x,y,format);
  1049. }
  1050. template<typename Numeric>
  1051. bool plot(const std::vector<Numeric>& y, const std::map<std::string, std::string>& keywords)
  1052. {
  1053. std::vector<Numeric> x(y.size());
  1054. for(size_t i=0; i<x.size(); ++i) x.at(i) = i;
  1055. return plot(x,y,keywords);
  1056. }
  1057. template<typename Numeric>
  1058. bool stem(const std::vector<Numeric>& y, const std::string& format = "")
  1059. {
  1060. std::vector<Numeric> x(y.size());
  1061. for (size_t i = 0; i < x.size(); ++i) x.at(i) = i;
  1062. return stem(x, y, format);
  1063. }
  1064. template<typename Numeric>
  1065. void text(Numeric x, Numeric y, const std::string& s = "")
  1066. {
  1067. detail::_interpreter::get();
  1068. PyObject* args = PyTuple_New(3);
  1069. PyTuple_SetItem(args, 0, PyFloat_FromDouble(x));
  1070. PyTuple_SetItem(args, 1, PyFloat_FromDouble(y));
  1071. PyTuple_SetItem(args, 2, PyString_FromString(s.c_str()));
  1072. PyObject* res = PyObject_CallObject(detail::_interpreter::get().s_python_function_text, args);
  1073. if(!res) throw std::runtime_error("Call to text() failed.");
  1074. Py_DECREF(args);
  1075. Py_DECREF(res);
  1076. }
  1077. inline void colorbar(PyObject* mappable = NULL, const std::map<std::string, float>& keywords = {})
  1078. {
  1079. if (mappable == NULL)
  1080. throw std::runtime_error("Must call colorbar with PyObject* returned from an image, contour, surface, etc.");
  1081. detail::_interpreter::get();
  1082. PyObject* args = PyTuple_New(1);
  1083. PyTuple_SetItem(args, 0, mappable);
  1084. PyObject* kwargs = PyDict_New();
  1085. for(std::map<std::string, float>::const_iterator it = keywords.begin(); it != keywords.end(); ++it)
  1086. {
  1087. PyDict_SetItemString(kwargs, it->first.c_str(), PyFloat_FromDouble(it->second));
  1088. }
  1089. PyObject* res = PyObject_Call(detail::_interpreter::get().s_python_function_colorbar, args, kwargs);
  1090. if(!res) throw std::runtime_error("Call to colorbar() failed.");
  1091. Py_DECREF(args);
  1092. Py_DECREF(kwargs);
  1093. Py_DECREF(res);
  1094. }
  1095. inline long figure(long number = -1)
  1096. {
  1097. detail::_interpreter::get();
  1098. PyObject *res;
  1099. if (number == -1)
  1100. res = PyObject_CallObject(detail::_interpreter::get().s_python_function_figure, detail::_interpreter::get().s_python_empty_tuple);
  1101. else {
  1102. assert(number > 0);
  1103. // Make sure interpreter is initialised
  1104. detail::_interpreter::get();
  1105. PyObject *args = PyTuple_New(1);
  1106. PyTuple_SetItem(args, 0, PyLong_FromLong(number));
  1107. res = PyObject_CallObject(detail::_interpreter::get().s_python_function_figure, args);
  1108. Py_DECREF(args);
  1109. }
  1110. if(!res) throw std::runtime_error("Call to figure() failed.");
  1111. PyObject* num = PyObject_GetAttrString(res, "number");
  1112. if (!num) throw std::runtime_error("Could not get number attribute of figure object");
  1113. const long figureNumber = PyLong_AsLong(num);
  1114. Py_DECREF(num);
  1115. Py_DECREF(res);
  1116. return figureNumber;
  1117. }
  1118. inline bool fignum_exists(long number)
  1119. {
  1120. detail::_interpreter::get();
  1121. PyObject *args = PyTuple_New(1);
  1122. PyTuple_SetItem(args, 0, PyLong_FromLong(number));
  1123. PyObject *res = PyObject_CallObject(detail::_interpreter::get().s_python_function_fignum_exists, args);
  1124. if(!res) throw std::runtime_error("Call to fignum_exists() failed.");
  1125. bool ret = PyObject_IsTrue(res);
  1126. Py_DECREF(res);
  1127. Py_DECREF(args);
  1128. return ret;
  1129. }
  1130. inline void figure_size(size_t w, size_t h)
  1131. {
  1132. detail::_interpreter::get();
  1133. const size_t dpi = 100;
  1134. PyObject* size = PyTuple_New(2);
  1135. PyTuple_SetItem(size, 0, PyFloat_FromDouble((double)w / dpi));
  1136. PyTuple_SetItem(size, 1, PyFloat_FromDouble((double)h / dpi));
  1137. PyObject* kwargs = PyDict_New();
  1138. PyDict_SetItemString(kwargs, "figsize", size);
  1139. PyDict_SetItemString(kwargs, "dpi", PyLong_FromSize_t(dpi));
  1140. PyObject* res = PyObject_Call(detail::_interpreter::get().s_python_function_figure,
  1141. detail::_interpreter::get().s_python_empty_tuple, kwargs);
  1142. Py_DECREF(kwargs);
  1143. if(!res) throw std::runtime_error("Call to figure_size() failed.");
  1144. Py_DECREF(res);
  1145. }
  1146. inline void legend()
  1147. {
  1148. detail::_interpreter::get();
  1149. PyObject* res = PyObject_CallObject(detail::_interpreter::get().s_python_function_legend, detail::_interpreter::get().s_python_empty_tuple);
  1150. if(!res) throw std::runtime_error("Call to legend() failed.");
  1151. Py_DECREF(res);
  1152. }
  1153. template<typename Numeric>
  1154. void ylim(Numeric left, Numeric right)
  1155. {
  1156. detail::_interpreter::get();
  1157. PyObject* list = PyList_New(2);
  1158. PyList_SetItem(list, 0, PyFloat_FromDouble(left));
  1159. PyList_SetItem(list, 1, PyFloat_FromDouble(right));
  1160. PyObject* args = PyTuple_New(1);
  1161. PyTuple_SetItem(args, 0, list);
  1162. PyObject* res = PyObject_CallObject(detail::_interpreter::get().s_python_function_ylim, args);
  1163. if(!res) throw std::runtime_error("Call to ylim() failed.");
  1164. Py_DECREF(args);
  1165. Py_DECREF(res);
  1166. }
  1167. template<typename Numeric>
  1168. void xlim(Numeric left, Numeric right)
  1169. {
  1170. detail::_interpreter::get();
  1171. PyObject* list = PyList_New(2);
  1172. PyList_SetItem(list, 0, PyFloat_FromDouble(left));
  1173. PyList_SetItem(list, 1, PyFloat_FromDouble(right));
  1174. PyObject* args = PyTuple_New(1);
  1175. PyTuple_SetItem(args, 0, list);
  1176. PyObject* res = PyObject_CallObject(detail::_interpreter::get().s_python_function_xlim, args);
  1177. if(!res) throw std::runtime_error("Call to xlim() failed.");
  1178. Py_DECREF(args);
  1179. Py_DECREF(res);
  1180. }
  1181. inline double* xlim()
  1182. {
  1183. detail::_interpreter::get();
  1184. PyObject* args = PyTuple_New(0);
  1185. PyObject* res = PyObject_CallObject(detail::_interpreter::get().s_python_function_xlim, args);
  1186. PyObject* left = PyTuple_GetItem(res,0);
  1187. PyObject* right = PyTuple_GetItem(res,1);
  1188. double* arr = new double[2];
  1189. arr[0] = PyFloat_AsDouble(left);
  1190. arr[1] = PyFloat_AsDouble(right);
  1191. if(!res) throw std::runtime_error("Call to xlim() failed.");
  1192. Py_DECREF(res);
  1193. return arr;
  1194. }
  1195. inline double* ylim()
  1196. {
  1197. detail::_interpreter::get();
  1198. PyObject* args = PyTuple_New(0);
  1199. PyObject* res = PyObject_CallObject(detail::_interpreter::get().s_python_function_ylim, args);
  1200. PyObject* left = PyTuple_GetItem(res,0);
  1201. PyObject* right = PyTuple_GetItem(res,1);
  1202. double* arr = new double[2];
  1203. arr[0] = PyFloat_AsDouble(left);
  1204. arr[1] = PyFloat_AsDouble(right);
  1205. if(!res) throw std::runtime_error("Call to ylim() failed.");
  1206. Py_DECREF(res);
  1207. return arr;
  1208. }
  1209. template<typename Numeric>
  1210. inline void xticks(const std::vector<Numeric> &ticks, const std::vector<std::string> &labels = {}, const std::map<std::string, std::string>& keywords = {})
  1211. {
  1212. assert(labels.size() == 0 || ticks.size() == labels.size());
  1213. detail::_interpreter::get();
  1214. // using numpy array
  1215. PyObject* ticksarray = detail::get_array(ticks);
  1216. PyObject* args;
  1217. if(labels.size() == 0) {
  1218. // construct positional args
  1219. args = PyTuple_New(1);
  1220. PyTuple_SetItem(args, 0, ticksarray);
  1221. } else {
  1222. // make tuple of tick labels
  1223. PyObject* labelstuple = PyTuple_New(labels.size());
  1224. for (size_t i = 0; i < labels.size(); i++)
  1225. PyTuple_SetItem(labelstuple, i, PyUnicode_FromString(labels[i].c_str()));
  1226. // construct positional args
  1227. args = PyTuple_New(2);
  1228. PyTuple_SetItem(args, 0, ticksarray);
  1229. PyTuple_SetItem(args, 1, labelstuple);
  1230. }
  1231. // construct keyword args
  1232. PyObject* kwargs = PyDict_New();
  1233. for(std::map<std::string, std::string>::const_iterator it = keywords.begin(); it != keywords.end(); ++it)
  1234. {
  1235. PyDict_SetItemString(kwargs, it->first.c_str(), PyString_FromString(it->second.c_str()));
  1236. }
  1237. PyObject* res = PyObject_Call(detail::_interpreter::get().s_python_function_xticks, args, kwargs);
  1238. Py_DECREF(args);
  1239. Py_DECREF(kwargs);
  1240. if(!res) throw std::runtime_error("Call to xticks() failed");
  1241. Py_DECREF(res);
  1242. }
  1243. template<typename Numeric>
  1244. inline void xticks(const std::vector<Numeric> &ticks, const std::map<std::string, std::string>& keywords)
  1245. {
  1246. xticks(ticks, {}, keywords);
  1247. }
  1248. template<typename Numeric>
  1249. inline void yticks(const std::vector<Numeric> &ticks, const std::vector<std::string> &labels = {}, const std::map<std::string, std::string>& keywords = {})
  1250. {
  1251. assert(labels.size() == 0 || ticks.size() == labels.size());
  1252. detail::_interpreter::get();
  1253. // using numpy array
  1254. PyObject* ticksarray = detail::get_array(ticks);
  1255. PyObject* args;
  1256. if(labels.size() == 0) {
  1257. // construct positional args
  1258. args = PyTuple_New(1);
  1259. PyTuple_SetItem(args, 0, ticksarray);
  1260. } else {
  1261. // make tuple of tick labels
  1262. PyObject* labelstuple = PyTuple_New(labels.size());
  1263. for (size_t i = 0; i < labels.size(); i++)
  1264. PyTuple_SetItem(labelstuple, i, PyUnicode_FromString(labels[i].c_str()));
  1265. // construct positional args
  1266. args = PyTuple_New(2);
  1267. PyTuple_SetItem(args, 0, ticksarray);
  1268. PyTuple_SetItem(args, 1, labelstuple);
  1269. }
  1270. // construct keyword args
  1271. PyObject* kwargs = PyDict_New();
  1272. for(std::map<std::string, std::string>::const_iterator it = keywords.begin(); it != keywords.end(); ++it)
  1273. {
  1274. PyDict_SetItemString(kwargs, it->first.c_str(), PyString_FromString(it->second.c_str()));
  1275. }
  1276. PyObject* res = PyObject_Call(detail::_interpreter::get().s_python_function_yticks, args, kwargs);
  1277. Py_DECREF(args);
  1278. Py_DECREF(kwargs);
  1279. if(!res) throw std::runtime_error("Call to yticks() failed");
  1280. Py_DECREF(res);
  1281. }
  1282. template<typename Numeric>
  1283. inline void yticks(const std::vector<Numeric> &ticks, const std::map<std::string, std::string>& keywords)
  1284. {
  1285. yticks(ticks, {}, keywords);
  1286. }
  1287. inline void tick_params(const std::map<std::string, std::string>& keywords, const std::string axis = "both")
  1288. {
  1289. detail::_interpreter::get();
  1290. // construct positional args
  1291. PyObject* args;
  1292. args = PyTuple_New(1);
  1293. PyTuple_SetItem(args, 0, PyString_FromString(axis.c_str()));
  1294. // construct keyword args
  1295. PyObject* kwargs = PyDict_New();
  1296. for (std::map<std::string, std::string>::const_iterator it = keywords.begin(); it != keywords.end(); ++it)
  1297. {
  1298. PyDict_SetItemString(kwargs, it->first.c_str(), PyString_FromString(it->second.c_str()));
  1299. }
  1300. PyObject* res = PyObject_Call(detail::_interpreter::get().s_python_function_tick_params, args, kwargs);
  1301. Py_DECREF(args);
  1302. Py_DECREF(kwargs);
  1303. if (!res) throw std::runtime_error("Call to tick_params() failed");
  1304. Py_DECREF(res);
  1305. }
  1306. inline void subplot(long nrows, long ncols, long plot_number)
  1307. {
  1308. detail::_interpreter::get();
  1309. // construct positional args
  1310. PyObject* args = PyTuple_New(3);
  1311. PyTuple_SetItem(args, 0, PyFloat_FromDouble(nrows));
  1312. PyTuple_SetItem(args, 1, PyFloat_FromDouble(ncols));
  1313. PyTuple_SetItem(args, 2, PyFloat_FromDouble(plot_number));
  1314. PyObject* res = PyObject_CallObject(detail::_interpreter::get().s_python_function_subplot, args);
  1315. if(!res) throw std::runtime_error("Call to subplot() failed.");
  1316. Py_DECREF(args);
  1317. Py_DECREF(res);
  1318. }
  1319. inline void subplot2grid(long nrows, long ncols, long rowid=0, long colid=0, long rowspan=1, long colspan=1)
  1320. {
  1321. detail::_interpreter::get();
  1322. PyObject* shape = PyTuple_New(2);
  1323. PyTuple_SetItem(shape, 0, PyLong_FromLong(nrows));
  1324. PyTuple_SetItem(shape, 1, PyLong_FromLong(ncols));
  1325. PyObject* loc = PyTuple_New(2);
  1326. PyTuple_SetItem(loc, 0, PyLong_FromLong(rowid));
  1327. PyTuple_SetItem(loc, 1, PyLong_FromLong(colid));
  1328. PyObject* args = PyTuple_New(4);
  1329. PyTuple_SetItem(args, 0, shape);
  1330. PyTuple_SetItem(args, 1, loc);
  1331. PyTuple_SetItem(args, 2, PyLong_FromLong(rowspan));
  1332. PyTuple_SetItem(args, 3, PyLong_FromLong(colspan));
  1333. PyObject* res = PyObject_CallObject(detail::_interpreter::get().s_python_function_subplot2grid, args);
  1334. if(!res) throw std::runtime_error("Call to subplot2grid() failed.");
  1335. Py_DECREF(shape);
  1336. Py_DECREF(loc);
  1337. Py_DECREF(args);
  1338. Py_DECREF(res);
  1339. }
  1340. inline void title(const std::string &titlestr, const std::map<std::string, std::string> &keywords = {})
  1341. {
  1342. detail::_interpreter::get();
  1343. PyObject* pytitlestr = PyString_FromString(titlestr.c_str());
  1344. PyObject* args = PyTuple_New(1);
  1345. PyTuple_SetItem(args, 0, pytitlestr);
  1346. PyObject* kwargs = PyDict_New();
  1347. for (auto it = keywords.begin(); it != keywords.end(); ++it) {
  1348. PyDict_SetItemString(kwargs, it->first.c_str(), PyUnicode_FromString(it->second.c_str()));
  1349. }
  1350. PyObject* res = PyObject_Call(detail::_interpreter::get().s_python_function_title, args, kwargs);
  1351. if(!res) throw std::runtime_error("Call to title() failed.");
  1352. Py_DECREF(args);
  1353. Py_DECREF(kwargs);
  1354. Py_DECREF(res);
  1355. }
  1356. inline void suptitle(const std::string &suptitlestr, const std::map<std::string, std::string> &keywords = {})
  1357. {
  1358. detail::_interpreter::get();
  1359. PyObject* pysuptitlestr = PyString_FromString(suptitlestr.c_str());
  1360. PyObject* args = PyTuple_New(1);
  1361. PyTuple_SetItem(args, 0, pysuptitlestr);
  1362. PyObject* kwargs = PyDict_New();
  1363. for (auto it = keywords.begin(); it != keywords.end(); ++it) {
  1364. PyDict_SetItemString(kwargs, it->first.c_str(), PyUnicode_FromString(it->second.c_str()));
  1365. }
  1366. PyObject* res = PyObject_Call(detail::_interpreter::get().s_python_function_suptitle, args, kwargs);
  1367. if(!res) throw std::runtime_error("Call to suptitle() failed.");
  1368. Py_DECREF(args);
  1369. Py_DECREF(kwargs);
  1370. Py_DECREF(res);
  1371. }
  1372. inline void axis(const std::string &axisstr)
  1373. {
  1374. detail::_interpreter::get();
  1375. PyObject* str = PyString_FromString(axisstr.c_str());
  1376. PyObject* args = PyTuple_New(1);
  1377. PyTuple_SetItem(args, 0, str);
  1378. PyObject* res = PyObject_CallObject(detail::_interpreter::get().s_python_function_axis, args);
  1379. if(!res) throw std::runtime_error("Call to title() failed.");
  1380. Py_DECREF(args);
  1381. Py_DECREF(res);
  1382. }
  1383. inline void axvline(double x, double ymin = 0., double ymax = 1., const std::map<std::string, std::string>& keywords = std::map<std::string, std::string>())
  1384. {
  1385. detail::_interpreter::get();
  1386. // construct positional args
  1387. PyObject* args = PyTuple_New(3);
  1388. PyTuple_SetItem(args, 0, PyFloat_FromDouble(x));
  1389. PyTuple_SetItem(args, 1, PyFloat_FromDouble(ymin));
  1390. PyTuple_SetItem(args, 2, PyFloat_FromDouble(ymax));
  1391. // construct keyword args
  1392. PyObject* kwargs = PyDict_New();
  1393. for(std::map<std::string, std::string>::const_iterator it = keywords.begin(); it != keywords.end(); ++it)
  1394. {
  1395. PyDict_SetItemString(kwargs, it->first.c_str(), PyString_FromString(it->second.c_str()));
  1396. }
  1397. PyObject* res = PyObject_Call(detail::_interpreter::get().s_python_function_axvline, args, kwargs);
  1398. Py_DECREF(args);
  1399. Py_DECREF(kwargs);
  1400. if(res) Py_DECREF(res);
  1401. }
  1402. inline void xlabel(const std::string &str, const std::map<std::string, std::string> &keywords = {})
  1403. {
  1404. detail::_interpreter::get();
  1405. PyObject* pystr = PyString_FromString(str.c_str());
  1406. PyObject* args = PyTuple_New(1);
  1407. PyTuple_SetItem(args, 0, pystr);
  1408. PyObject* kwargs = PyDict_New();
  1409. for (auto it = keywords.begin(); it != keywords.end(); ++it) {
  1410. PyDict_SetItemString(kwargs, it->first.c_str(), PyUnicode_FromString(it->second.c_str()));
  1411. }
  1412. PyObject* res = PyObject_Call(detail::_interpreter::get().s_python_function_xlabel, args, kwargs);
  1413. if(!res) throw std::runtime_error("Call to xlabel() failed.");
  1414. Py_DECREF(args);
  1415. Py_DECREF(kwargs);
  1416. Py_DECREF(res);
  1417. }
  1418. inline void ylabel(const std::string &str, const std::map<std::string, std::string>& keywords = {})
  1419. {
  1420. detail::_interpreter::get();
  1421. PyObject* pystr = PyString_FromString(str.c_str());
  1422. PyObject* args = PyTuple_New(1);
  1423. PyTuple_SetItem(args, 0, pystr);
  1424. PyObject* kwargs = PyDict_New();
  1425. for (auto it = keywords.begin(); it != keywords.end(); ++it) {
  1426. PyDict_SetItemString(kwargs, it->first.c_str(), PyUnicode_FromString(it->second.c_str()));
  1427. }
  1428. PyObject* res = PyObject_Call(detail::_interpreter::get().s_python_function_ylabel, args, kwargs);
  1429. if(!res) throw std::runtime_error("Call to ylabel() failed.");
  1430. Py_DECREF(args);
  1431. Py_DECREF(kwargs);
  1432. Py_DECREF(res);
  1433. }
  1434. inline void set_zlabel(const std::string &str, const std::map<std::string, std::string>& keywords = {})
  1435. {
  1436. detail::_interpreter::get();
  1437. // Same as with plot_surface: We lazily load the modules here the first time
  1438. // this function is called because I'm not sure that we can assume "matplotlib
  1439. // installed" implies "mpl_toolkits installed" on all platforms, and we don't
  1440. // want to require it for people who don't need 3d plots.
  1441. static PyObject *mpl_toolkitsmod = nullptr, *axis3dmod = nullptr;
  1442. if (!mpl_toolkitsmod) {
  1443. PyObject* mpl_toolkits = PyString_FromString("mpl_toolkits");
  1444. PyObject* axis3d = PyString_FromString("mpl_toolkits.mplot3d");
  1445. if (!mpl_toolkits || !axis3d) { throw std::runtime_error("couldnt create string"); }
  1446. mpl_toolkitsmod = PyImport_Import(mpl_toolkits);
  1447. Py_DECREF(mpl_toolkits);
  1448. if (!mpl_toolkitsmod) { throw std::runtime_error("Error loading module mpl_toolkits!"); }
  1449. axis3dmod = PyImport_Import(axis3d);
  1450. Py_DECREF(axis3d);
  1451. if (!axis3dmod) { throw std::runtime_error("Error loading module mpl_toolkits.mplot3d!"); }
  1452. }
  1453. PyObject* pystr = PyString_FromString(str.c_str());
  1454. PyObject* args = PyTuple_New(1);
  1455. PyTuple_SetItem(args, 0, pystr);
  1456. PyObject* kwargs = PyDict_New();
  1457. for (auto it = keywords.begin(); it != keywords.end(); ++it) {
  1458. PyDict_SetItemString(kwargs, it->first.c_str(), PyUnicode_FromString(it->second.c_str()));
  1459. }
  1460. PyObject *ax =
  1461. PyObject_CallObject(detail::_interpreter::get().s_python_function_gca,
  1462. detail::_interpreter::get().s_python_empty_tuple);
  1463. if (!ax) throw std::runtime_error("Call to gca() failed.");
  1464. Py_INCREF(ax);
  1465. PyObject *zlabel = PyObject_GetAttrString(ax, "set_zlabel");
  1466. if (!zlabel) throw std::runtime_error("Attribute set_zlabel not found.");
  1467. Py_INCREF(zlabel);
  1468. PyObject *res = PyObject_Call(zlabel, args, kwargs);
  1469. if (!res) throw std::runtime_error("Call to set_zlabel() failed.");
  1470. Py_DECREF(zlabel);
  1471. Py_DECREF(ax);
  1472. Py_DECREF(args);
  1473. Py_DECREF(kwargs);
  1474. if (res) Py_DECREF(res);
  1475. }
  1476. inline void grid(bool flag)
  1477. {
  1478. detail::_interpreter::get();
  1479. PyObject* pyflag = flag ? Py_True : Py_False;
  1480. Py_INCREF(pyflag);
  1481. PyObject* args = PyTuple_New(1);
  1482. PyTuple_SetItem(args, 0, pyflag);
  1483. PyObject* res = PyObject_CallObject(detail::_interpreter::get().s_python_function_grid, args);
  1484. if(!res) throw std::runtime_error("Call to grid() failed.");
  1485. Py_DECREF(args);
  1486. Py_DECREF(res);
  1487. }
  1488. inline void show(const bool block = true)
  1489. {
  1490. detail::_interpreter::get();
  1491. PyObject* res;
  1492. if(block)
  1493. {
  1494. res = PyObject_CallObject(
  1495. detail::_interpreter::get().s_python_function_show,
  1496. detail::_interpreter::get().s_python_empty_tuple);
  1497. }
  1498. else
  1499. {
  1500. PyObject *kwargs = PyDict_New();
  1501. PyDict_SetItemString(kwargs, "block", Py_False);
  1502. res = PyObject_Call( detail::_interpreter::get().s_python_function_show, detail::_interpreter::get().s_python_empty_tuple, kwargs);
  1503. Py_DECREF(kwargs);
  1504. }
  1505. if (!res) throw std::runtime_error("Call to show() failed.");
  1506. Py_DECREF(res);
  1507. }
  1508. inline void close()
  1509. {
  1510. detail::_interpreter::get();
  1511. PyObject* res = PyObject_CallObject(
  1512. detail::_interpreter::get().s_python_function_close,
  1513. detail::_interpreter::get().s_python_empty_tuple);
  1514. if (!res) throw std::runtime_error("Call to close() failed.");
  1515. Py_DECREF(res);
  1516. }
  1517. inline void xkcd() {
  1518. detail::_interpreter::get();
  1519. PyObject* res;
  1520. PyObject *kwargs = PyDict_New();
  1521. res = PyObject_Call(detail::_interpreter::get().s_python_function_xkcd,
  1522. detail::_interpreter::get().s_python_empty_tuple, kwargs);
  1523. Py_DECREF(kwargs);
  1524. if (!res)
  1525. throw std::runtime_error("Call to show() failed.");
  1526. Py_DECREF(res);
  1527. }
  1528. inline void draw()
  1529. {
  1530. detail::_interpreter::get();
  1531. PyObject* res = PyObject_CallObject(
  1532. detail::_interpreter::get().s_python_function_draw,
  1533. detail::_interpreter::get().s_python_empty_tuple);
  1534. if (!res) throw std::runtime_error("Call to draw() failed.");
  1535. Py_DECREF(res);
  1536. }
  1537. template<typename Numeric>
  1538. inline void pause(Numeric interval)
  1539. {
  1540. detail::_interpreter::get();
  1541. PyObject* args = PyTuple_New(1);
  1542. PyTuple_SetItem(args, 0, PyFloat_FromDouble(interval));
  1543. PyObject* res = PyObject_CallObject(detail::_interpreter::get().s_python_function_pause, args);
  1544. if(!res) throw std::runtime_error("Call to pause() failed.");
  1545. Py_DECREF(args);
  1546. Py_DECREF(res);
  1547. }
  1548. inline void save(const std::string& filename)
  1549. {
  1550. detail::_interpreter::get();
  1551. PyObject* pyfilename = PyString_FromString(filename.c_str());
  1552. PyObject* args = PyTuple_New(1);
  1553. PyTuple_SetItem(args, 0, pyfilename);
  1554. PyObject* res = PyObject_CallObject(detail::_interpreter::get().s_python_function_save, args);
  1555. if (!res) throw std::runtime_error("Call to save() failed.");
  1556. Py_DECREF(args);
  1557. Py_DECREF(res);
  1558. }
  1559. inline void clf() {
  1560. detail::_interpreter::get();
  1561. PyObject *res = PyObject_CallObject(
  1562. detail::_interpreter::get().s_python_function_clf,
  1563. detail::_interpreter::get().s_python_empty_tuple);
  1564. if (!res) throw std::runtime_error("Call to clf() failed.");
  1565. Py_DECREF(res);
  1566. }
  1567. inline void ion() {
  1568. detail::_interpreter::get();
  1569. PyObject *res = PyObject_CallObject(
  1570. detail::_interpreter::get().s_python_function_ion,
  1571. detail::_interpreter::get().s_python_empty_tuple);
  1572. if (!res) throw std::runtime_error("Call to ion() failed.");
  1573. Py_DECREF(res);
  1574. }
  1575. inline std::vector<std::array<double, 2>> ginput(const int numClicks = 1, const std::map<std::string, std::string>& keywords = {})
  1576. {
  1577. detail::_interpreter::get();
  1578. PyObject *args = PyTuple_New(1);
  1579. PyTuple_SetItem(args, 0, PyLong_FromLong(numClicks));
  1580. // construct keyword args
  1581. PyObject* kwargs = PyDict_New();
  1582. for(std::map<std::string, std::string>::const_iterator it = keywords.begin(); it != keywords.end(); ++it)
  1583. {
  1584. PyDict_SetItemString(kwargs, it->first.c_str(), PyUnicode_FromString(it->second.c_str()));
  1585. }
  1586. PyObject* res = PyObject_Call(
  1587. detail::_interpreter::get().s_python_function_ginput, args, kwargs);
  1588. Py_DECREF(kwargs);
  1589. Py_DECREF(args);
  1590. if (!res) throw std::runtime_error("Call to ginput() failed.");
  1591. const size_t len = PyList_Size(res);
  1592. std::vector<std::array<double, 2>> out;
  1593. out.reserve(len);
  1594. for (size_t i = 0; i < len; i++) {
  1595. PyObject *current = PyList_GetItem(res, i);
  1596. std::array<double, 2> position;
  1597. position[0] = PyFloat_AsDouble(PyTuple_GetItem(current, 0));
  1598. position[1] = PyFloat_AsDouble(PyTuple_GetItem(current, 1));
  1599. out.push_back(position);
  1600. }
  1601. Py_DECREF(res);
  1602. return out;
  1603. }
  1604. // Actually, is there any reason not to call this automatically for every plot?
  1605. inline void tight_layout() {
  1606. detail::_interpreter::get();
  1607. PyObject *res = PyObject_CallObject(
  1608. detail::_interpreter::get().s_python_function_tight_layout,
  1609. detail::_interpreter::get().s_python_empty_tuple);
  1610. if (!res) throw std::runtime_error("Call to tight_layout() failed.");
  1611. Py_DECREF(res);
  1612. }
  1613. // Support for variadic plot() and initializer lists:
  1614. namespace detail {
  1615. template<typename T>
  1616. using is_function = typename std::is_function<std::remove_pointer<std::remove_reference<T>>>::type;
  1617. template<bool obj, typename T>
  1618. struct is_callable_impl;
  1619. template<typename T>
  1620. struct is_callable_impl<false, T>
  1621. {
  1622. typedef is_function<T> type;
  1623. }; // a non-object is callable iff it is a function
  1624. template<typename T>
  1625. struct is_callable_impl<true, T>
  1626. {
  1627. struct Fallback { void operator()(); };
  1628. struct Derived : T, Fallback { };
  1629. template<typename U, U> struct Check;
  1630. template<typename U>
  1631. static std::true_type test( ... ); // use a variadic function to make sure (1) it accepts everything and (2) its always the worst match
  1632. template<typename U>
  1633. static std::false_type test( Check<void(Fallback::*)(), &U::operator()>* );
  1634. public:
  1635. typedef decltype(test<Derived>(nullptr)) type;
  1636. typedef decltype(&Fallback::operator()) dtype;
  1637. static constexpr bool value = type::value;
  1638. }; // an object is callable iff it defines operator()
  1639. template<typename T>
  1640. struct is_callable
  1641. {
  1642. // dispatch to is_callable_impl<true, T> or is_callable_impl<false, T> depending on whether T is of class type or not
  1643. typedef typename is_callable_impl<std::is_class<T>::value, T>::type type;
  1644. };
  1645. template<typename IsYDataCallable>
  1646. struct plot_impl { };
  1647. template<>
  1648. struct plot_impl<std::false_type>
  1649. {
  1650. template<typename IterableX, typename IterableY>
  1651. bool operator()(const IterableX& x, const IterableY& y, const std::string& format)
  1652. {
  1653. // 2-phase lookup for distance, begin, end
  1654. using std::distance;
  1655. using std::begin;
  1656. using std::end;
  1657. auto xs = distance(begin(x), end(x));
  1658. auto ys = distance(begin(y), end(y));
  1659. assert(xs == ys && "x and y data must have the same number of elements!");
  1660. PyObject* xlist = PyList_New(xs);
  1661. PyObject* ylist = PyList_New(ys);
  1662. PyObject* pystring = PyString_FromString(format.c_str());
  1663. auto itx = begin(x), ity = begin(y);
  1664. for(size_t i = 0; i < xs; ++i) {
  1665. PyList_SetItem(xlist, i, PyFloat_FromDouble(*itx++));
  1666. PyList_SetItem(ylist, i, PyFloat_FromDouble(*ity++));
  1667. }
  1668. PyObject* plot_args = PyTuple_New(3);
  1669. PyTuple_SetItem(plot_args, 0, xlist);
  1670. PyTuple_SetItem(plot_args, 1, ylist);
  1671. PyTuple_SetItem(plot_args, 2, pystring);
  1672. PyObject* res = PyObject_CallObject(detail::_interpreter::get().s_python_function_plot, plot_args);
  1673. Py_DECREF(plot_args);
  1674. if(res) Py_DECREF(res);
  1675. return res;
  1676. }
  1677. };
  1678. template<>
  1679. struct plot_impl<std::true_type>
  1680. {
  1681. template<typename Iterable, typename Callable>
  1682. bool operator()(const Iterable& ticks, const Callable& f, const std::string& format)
  1683. {
  1684. if(begin(ticks) == end(ticks)) return true;
  1685. // We could use additional meta-programming to deduce the correct element type of y,
  1686. // but all values have to be convertible to double anyways
  1687. std::vector<double> y;
  1688. for(auto x : ticks) y.push_back(f(x));
  1689. return plot_impl<std::false_type>()(ticks,y,format);
  1690. }
  1691. };
  1692. } // end namespace detail
  1693. // recursion stop for the above
  1694. template<typename... Args>
  1695. bool plot() { return true; }
  1696. template<typename A, typename B, typename... Args>
  1697. bool plot(const A& a, const B& b, const std::string& format, Args... args)
  1698. {
  1699. return detail::plot_impl<typename detail::is_callable<B>::type>()(a,b,format) && plot(args...);
  1700. }
  1701. /*
  1702. * This group of plot() functions is needed to support initializer lists, i.e. calling
  1703. * plot( {1,2,3,4} )
  1704. */
  1705. inline bool plot(const std::vector<double>& x, const std::vector<double>& y, const std::string& format = "") {
  1706. return plot<double,double>(x,y,format);
  1707. }
  1708. inline bool plot(const std::vector<double>& y, const std::string& format = "") {
  1709. return plot<double>(y,format);
  1710. }
  1711. inline bool plot(const std::vector<double>& x, const std::vector<double>& y, const std::map<std::string, std::string>& keywords) {
  1712. return plot<double>(x,y,keywords);
  1713. }
  1714. /*
  1715. * This class allows dynamic plots, ie changing the plotted data without clearing and re-plotting
  1716. */
  1717. class Plot
  1718. {
  1719. public:
  1720. // default initialization with plot label, some data and format
  1721. template<typename Numeric>
  1722. Plot(const std::string& name, const std::vector<Numeric>& x, const std::vector<Numeric>& y, const std::string& format = "") {
  1723. detail::_interpreter::get();
  1724. assert(x.size() == y.size());
  1725. PyObject* kwargs = PyDict_New();
  1726. if(name != "")
  1727. PyDict_SetItemString(kwargs, "label", PyString_FromString(name.c_str()));
  1728. PyObject* xarray = detail::get_array(x);
  1729. PyObject* yarray = detail::get_array(y);
  1730. PyObject* pystring = PyString_FromString(format.c_str());
  1731. PyObject* plot_args = PyTuple_New(3);
  1732. PyTuple_SetItem(plot_args, 0, xarray);
  1733. PyTuple_SetItem(plot_args, 1, yarray);
  1734. PyTuple_SetItem(plot_args, 2, pystring);
  1735. PyObject* res = PyObject_Call(detail::_interpreter::get().s_python_function_plot, plot_args, kwargs);
  1736. Py_DECREF(kwargs);
  1737. Py_DECREF(plot_args);
  1738. if(res)
  1739. {
  1740. line= PyList_GetItem(res, 0);
  1741. if(line)
  1742. set_data_fct = PyObject_GetAttrString(line,"set_data");
  1743. else
  1744. Py_DECREF(line);
  1745. Py_DECREF(res);
  1746. }
  1747. }
  1748. // shorter initialization with name or format only
  1749. // basically calls line, = plot([], [])
  1750. Plot(const std::string& name = "", const std::string& format = "")
  1751. : Plot(name, std::vector<double>(), std::vector<double>(), format) {}
  1752. template<typename Numeric>
  1753. bool update(const std::vector<Numeric>& x, const std::vector<Numeric>& y) {
  1754. assert(x.size() == y.size());
  1755. if(set_data_fct)
  1756. {
  1757. PyObject* xarray = detail::get_array(x);
  1758. PyObject* yarray = detail::get_array(y);
  1759. PyObject* plot_args = PyTuple_New(2);
  1760. PyTuple_SetItem(plot_args, 0, xarray);
  1761. PyTuple_SetItem(plot_args, 1, yarray);
  1762. PyObject* res = PyObject_CallObject(set_data_fct, plot_args);
  1763. if (res) Py_DECREF(res);
  1764. return res;
  1765. }
  1766. return false;
  1767. }
  1768. // clears the plot but keep it available
  1769. bool clear() {
  1770. return update(std::vector<double>(), std::vector<double>());
  1771. }
  1772. // definitely remove this line
  1773. void remove() {
  1774. if(line)
  1775. {
  1776. auto remove_fct = PyObject_GetAttrString(line,"remove");
  1777. PyObject* args = PyTuple_New(0);
  1778. PyObject* res = PyObject_CallObject(remove_fct, args);
  1779. if (res) Py_DECREF(res);
  1780. }
  1781. decref();
  1782. }
  1783. ~Plot() {
  1784. decref();
  1785. }
  1786. private:
  1787. void decref() {
  1788. if(line)
  1789. Py_DECREF(line);
  1790. if(set_data_fct)
  1791. Py_DECREF(set_data_fct);
  1792. }
  1793. PyObject* line = nullptr;
  1794. PyObject* set_data_fct = nullptr;
  1795. };
  1796. } // end namespace matplotlibcpp