19#define NPY_NO_DEPRECATED_API NPY_1_7_API_VERSION
20#include <numpy/arrayobject.h>
23#include <opencv2/opencv.hpp>
30#if CV_MAJOR_VERSION > 3
31#define CV_BGR2RGB cv::COLOR_BGR2RGB
32#define CV_BGRA2RGBA cv::COLOR_BGRA2RGBA
36#if PY_MAJOR_VERSION >= 3
37#define PyString_FromString PyUnicode_FromString
38#define PyInt_FromLong PyLong_FromLong
39#define PyString_FromString PyUnicode_FromString
132 PyObject *fn = PyObject_GetAttrString(module, fname.c_str());
135 throw std::runtime_error(
136 std::string(
"Couldn't find required function: ") + fname);
138 if (!PyFunction_Check(fn))
139 throw std::runtime_error(
140 fname + std::string(
" is unexpectedly not a PyFunction."));
147#if PY_MAJOR_VERSION >= 3
166#if PY_MAJOR_VERSION >= 3
167 wchar_t name[] = L
"plotting";
169 char name[] =
"plotting";
171 Py_SetProgramName(name);
174 wchar_t const *dummy_args[] = {
177 wchar_t const **argv = dummy_args;
178 int argc =
sizeof(dummy_args) /
sizeof(dummy_args[0]) - 1;
180#if PY_MAJOR_VERSION >= 3
181 PySys_SetArgv(argc,
const_cast<wchar_t **
>(argv));
183 PySys_SetArgv(argc, (
char **)(argv));
190 PyObject *matplotlibname = PyString_FromString(
"matplotlib");
191 PyObject *pyplotname = PyString_FromString(
"matplotlib.pyplot");
192 PyObject *cmname = PyString_FromString(
"matplotlib.cm");
193 PyObject *pylabname = PyString_FromString(
"pylab");
194 if (!pyplotname || !pylabname || !matplotlibname || !cmname) {
195 throw std::runtime_error(
"couldnt create string");
198 PyObject *matplotlib = PyImport_Import(matplotlibname);
200 Py_DECREF(matplotlibname);
203 throw std::runtime_error(
"Error loading module matplotlib!");
209 PyObject_CallMethod(matplotlib,
const_cast<char *
>(
"use"),
210 const_cast<char *
>(
"s"),
s_backend.c_str());
213 PyObject *pymod = PyImport_Import(pyplotname);
214 Py_DECREF(pyplotname);
216 throw std::runtime_error(
"Error loading module matplotlib.pyplot!");
222 throw std::runtime_error(
"Error loading module matplotlib.cm!");
225 PyObject *pylabmod = PyImport_Import(pylabname);
226 Py_DECREF(pylabname);
228 throw std::runtime_error(
"Error loading module pylab!");
308inline bool annotate(std::string annotation,
double x,
double y) {
311 PyObject *xy = PyTuple_New(2);
312 PyObject *str = PyString_FromString(annotation.c_str());
314 PyTuple_SetItem(xy, 0, PyFloat_FromDouble(x));
315 PyTuple_SetItem(xy, 1, PyFloat_FromDouble(y));
317 PyObject *kwargs = PyDict_New();
318 PyDict_SetItemString(kwargs,
"xy", xy);
320 PyObject *args = PyTuple_New(1);
321 PyTuple_SetItem(args, 0, str);
323 PyObject *res = PyObject_Call(
340 const static NPY_TYPES
type = NPY_NOTYPE;
343 const static NPY_TYPES
type = NPY_DOUBLE;
346 const static NPY_TYPES
type = NPY_FLOAT;
349 const static NPY_TYPES
type = NPY_BOOL;
352 const static NPY_TYPES
type = NPY_INT8;
355 const static NPY_TYPES
type = NPY_SHORT;
358 const static NPY_TYPES
type = NPY_INT;
361 const static NPY_TYPES
type = NPY_INT64;
364 const static NPY_TYPES
type = NPY_UINT8;
367 const static NPY_TYPES
type = NPY_USHORT;
370 const static NPY_TYPES
type = NPY_ULONG;
373 const static NPY_TYPES
type = NPY_UINT64;
378static_assert(
sizeof(
long long) == 8);
380 const static NPY_TYPES
type = NPY_INT64;
382static_assert(
sizeof(
unsigned long long) == 8);
384 const static NPY_TYPES
type = NPY_UINT64;
387template <
typename Numeric> PyObject *
get_array(
const std::vector<Numeric> &v) {
388 npy_intp vsize = v.size();
390 if (type == NPY_NOTYPE) {
391 size_t memsize = v.size() *
sizeof(double);
392 double *dp =
static_cast<double *
>(::malloc(memsize));
393 for (
size_t i = 0; i < v.size(); ++i)
395 PyObject *varray = PyArray_SimpleNewFromData(1, &vsize, NPY_DOUBLE, dp);
396 PyArray_UpdateFlags(
reinterpret_cast<PyArrayObject *
>(varray),
402 PyArray_SimpleNewFromData(1, &vsize, type, (
void *)(v.data()));
406template <
typename Numeric>
407PyObject *
get_2darray(
const std::vector<::std::vector<Numeric>> &v) {
409 throw std::runtime_error(
"get_2d_array v too small");
411 npy_intp vsize[2] = {
static_cast<npy_intp
>(v.size()),
412 static_cast<npy_intp
>(v[0].size())};
414 PyArrayObject *varray =
415 (PyArrayObject *)PyArray_SimpleNew(2, vsize, NPY_DOUBLE);
417 double *vd_begin =
static_cast<double *
>(PyArray_DATA(varray));
419 for (const ::std::vector<Numeric> &v_row : v) {
420 if (v_row.size() !=
static_cast<size_t>(vsize[1]))
421 throw std::runtime_error(
"Missmatched array size");
422 std::copy(v_row.begin(), v_row.end(), vd_begin);
423 vd_begin += vsize[1];
426 return reinterpret_cast<PyObject *
>(varray);
431template <
typename Numeric> PyObject *
get_array(
const std::vector<Numeric> &v) {
432 PyObject *list = PyList_New(v.size());
433 for (
size_t i = 0; i < v.size(); ++i) {
434 PyList_SetItem(list, i, PyFloat_FromDouble(v.at(i)));
442inline PyObject *
get_array(
const std::vector<std::string> &strings) {
443 PyObject *list = PyList_New(strings.size());
444 for (std::size_t i = 0; i < strings.size(); ++i) {
445 PyList_SetItem(list, i, PyString_FromString(strings[i].c_str()));
451template <
typename Numeric>
453 PyObject *listlist = PyList_New(ll.size());
454 for (std::size_t i = 0; i < ll.size(); ++i) {
455 PyList_SetItem(listlist, i,
get_array(ll[i]));
465template <
typename Numeric>
466bool plot(
const std::vector<Numeric> &x,
const std::vector<Numeric> &y,
467 const std::map<std::string, std::string> &keywords) {
468 assert(x.size() == y.size());
477 PyObject *args = PyTuple_New(2);
478 PyTuple_SetItem(args, 0, xarray);
479 PyTuple_SetItem(args, 1, yarray);
482 PyObject *kwargs = PyDict_New();
483 for (std::map<std::string, std::string>::const_iterator it = keywords.begin();
484 it != keywords.end(); ++it) {
485 PyDict_SetItemString(kwargs, it->first.c_str(),
486 PyString_FromString(it->second.c_str()));
489 PyObject *res = PyObject_Call(
503template <
typename Numeric>
505 const std::vector<::std::vector<Numeric>> &y,
506 const std::vector<::std::vector<Numeric>> &z,
507 const std::map<std::string, std::string> &keywords =
508 std::map<std::string, std::string>(),
509 const long fig_number = 0) {
516 static PyObject *mpl_toolkitsmod =
nullptr, *axis3dmod =
nullptr;
517 if (!mpl_toolkitsmod) {
520 PyObject *mpl_toolkits = PyString_FromString(
"mpl_toolkits");
521 PyObject *axis3d = PyString_FromString(
"mpl_toolkits.mplot3d");
522 if (!mpl_toolkits || !axis3d) {
523 throw std::runtime_error(
"couldnt create string");
526 mpl_toolkitsmod = PyImport_Import(mpl_toolkits);
527 Py_DECREF(mpl_toolkits);
528 if (!mpl_toolkitsmod) {
529 throw std::runtime_error(
"Error loading module mpl_toolkits!");
532 axis3dmod = PyImport_Import(axis3d);
535 throw std::runtime_error(
"Error loading module mpl_toolkits.mplot3d!");
539 assert(x.size() == y.size());
540 assert(y.size() == z.size());
548 PyObject *args = PyTuple_New(3);
549 PyTuple_SetItem(args, 0, xarray);
550 PyTuple_SetItem(args, 1, yarray);
551 PyTuple_SetItem(args, 2, zarray);
554 PyObject *kwargs = PyDict_New();
555 PyDict_SetItemString(kwargs,
"rstride", PyInt_FromLong(1));
556 PyDict_SetItemString(kwargs,
"cstride", PyInt_FromLong(1));
558 PyObject *python_colormap_coolwarm = PyObject_GetAttrString(
561 PyDict_SetItemString(kwargs,
"cmap", python_colormap_coolwarm);
563 for (std::map<std::string, std::string>::const_iterator it = keywords.begin();
564 it != keywords.end(); ++it) {
565 if (it->first ==
"linewidth" || it->first ==
"alpha") {
566 PyDict_SetItemString(kwargs, it->first.c_str(),
567 PyFloat_FromDouble(std::stod(it->second)));
569 PyDict_SetItemString(kwargs, it->first.c_str(),
570 PyString_FromString(it->second.c_str()));
574 PyObject *fig_args = PyTuple_New(1);
575 PyObject *fig =
nullptr;
576 PyTuple_SetItem(fig_args, 0, PyLong_FromLong(fig_number));
577 PyObject *fig_exists = PyObject_CallObject(
579 if (!PyObject_IsTrue(fig_exists)) {
580 fig = PyObject_CallObject(
584 fig = PyObject_CallObject(
587 Py_DECREF(fig_exists);
589 throw std::runtime_error(
"Call to figure() failed.");
591 PyObject *gca_kwargs = PyDict_New();
592 PyDict_SetItemString(gca_kwargs,
"projection", PyString_FromString(
"3d"));
594 PyObject *gca = PyObject_GetAttrString(fig,
"gca");
596 throw std::runtime_error(
"No gca");
598 PyObject *
axis = PyObject_Call(
602 throw std::runtime_error(
"No axis");
606 Py_DECREF(gca_kwargs);
610 throw std::runtime_error(
"No surface");
612 PyObject *res = PyObject_Call(
plot_surface, args, kwargs);
614 throw std::runtime_error(
"failed surface");
624template <
typename Numeric>
625void contour(
const std::vector<::std::vector<Numeric>> &x,
626 const std::vector<::std::vector<Numeric>> &y,
627 const std::vector<::std::vector<Numeric>> &z,
628 const std::map<std::string, std::string> &keywords = {}) {
637 PyObject *args = PyTuple_New(3);
638 PyTuple_SetItem(args, 0, xarray);
639 PyTuple_SetItem(args, 1, yarray);
640 PyTuple_SetItem(args, 2, zarray);
643 PyObject *kwargs = PyDict_New();
645 PyObject *python_colormap_coolwarm = PyObject_GetAttrString(
648 PyDict_SetItemString(kwargs,
"cmap", python_colormap_coolwarm);
650 for (std::map<std::string, std::string>::const_iterator it = keywords.begin();
651 it != keywords.end(); ++it) {
652 PyDict_SetItemString(kwargs, it->first.c_str(),
653 PyString_FromString(it->second.c_str()));
656 PyObject *res = PyObject_Call(
659 throw std::runtime_error(
"failed contour");
667template <
typename Numeric>
668void spy(
const std::vector<::std::vector<Numeric>> &x,
669 const double markersize = -1,
670 const std::map<std::string, std::string> &keywords = {}) {
675 PyObject *kwargs = PyDict_New();
676 if (markersize != -1) {
677 PyDict_SetItemString(kwargs,
"markersize", PyFloat_FromDouble(markersize));
679 for (std::map<std::string, std::string>::const_iterator it = keywords.begin();
680 it != keywords.end(); ++it) {
681 PyDict_SetItemString(kwargs, it->first.c_str(),
682 PyString_FromString(it->second.c_str()));
685 PyObject *plot_args = PyTuple_New(1);
686 PyTuple_SetItem(plot_args, 0, xarray);
688 PyObject *res = PyObject_Call(
691 Py_DECREF(plot_args);
698template <
typename Numeric>
699void plot3(
const std::vector<Numeric> &x,
const std::vector<Numeric> &y,
700 const std::vector<Numeric> &z,
701 const std::map<std::string, std::string> &keywords =
702 std::map<std::string, std::string>(),
703 const long fig_number = 0) {
710 static PyObject *mpl_toolkitsmod =
nullptr, *axis3dmod =
nullptr;
711 if (!mpl_toolkitsmod) {
714 PyObject *mpl_toolkits = PyString_FromString(
"mpl_toolkits");
715 PyObject *axis3d = PyString_FromString(
"mpl_toolkits.mplot3d");
716 if (!mpl_toolkits || !axis3d) {
717 throw std::runtime_error(
"couldnt create string");
720 mpl_toolkitsmod = PyImport_Import(mpl_toolkits);
721 Py_DECREF(mpl_toolkits);
722 if (!mpl_toolkitsmod) {
723 throw std::runtime_error(
"Error loading module mpl_toolkits!");
726 axis3dmod = PyImport_Import(axis3d);
729 throw std::runtime_error(
"Error loading module mpl_toolkits.mplot3d!");
733 assert(x.size() == y.size());
734 assert(y.size() == z.size());
741 PyObject *args = PyTuple_New(3);
742 PyTuple_SetItem(args, 0, xarray);
743 PyTuple_SetItem(args, 1, yarray);
744 PyTuple_SetItem(args, 2, zarray);
747 PyObject *kwargs = PyDict_New();
749 for (std::map<std::string, std::string>::const_iterator it = keywords.begin();
750 it != keywords.end(); ++it) {
751 PyDict_SetItemString(kwargs, it->first.c_str(),
752 PyString_FromString(it->second.c_str()));
755 PyObject *fig_args = PyTuple_New(1);
756 PyObject *fig =
nullptr;
757 PyTuple_SetItem(fig_args, 0, PyLong_FromLong(fig_number));
758 PyObject *fig_exists = PyObject_CallObject(
760 if (!PyObject_IsTrue(fig_exists)) {
761 fig = PyObject_CallObject(
765 fig = PyObject_CallObject(
769 throw std::runtime_error(
"Call to figure() failed.");
771 PyObject *gca_kwargs = PyDict_New();
772 PyDict_SetItemString(gca_kwargs,
"projection", PyString_FromString(
"3d"));
774 PyObject *gca = PyObject_GetAttrString(fig,
"gca");
776 throw std::runtime_error(
"No gca");
778 PyObject *
axis = PyObject_Call(
782 throw std::runtime_error(
"No axis");
786 Py_DECREF(gca_kwargs);
788 PyObject *
plot3 = PyObject_GetAttrString(
axis,
"plot");
790 throw std::runtime_error(
"No 3D line plot");
792 PyObject *res = PyObject_Call(
plot3, args, kwargs);
794 throw std::runtime_error(
"Failed 3D line plot");
804template <
typename Numeric>
805bool stem(
const std::vector<Numeric> &x,
const std::vector<Numeric> &y,
806 const std::map<std::string, std::string> &keywords) {
807 assert(x.size() == y.size());
816 PyObject *args = PyTuple_New(2);
817 PyTuple_SetItem(args, 0, xarray);
818 PyTuple_SetItem(args, 1, yarray);
821 PyObject *kwargs = PyDict_New();
822 for (std::map<std::string, std::string>::const_iterator it = keywords.begin();
823 it != keywords.end(); ++it) {
824 PyDict_SetItemString(kwargs, it->first.c_str(),
825 PyString_FromString(it->second.c_str()));
828 PyObject *res = PyObject_Call(
839template <
typename Numeric>
840bool fill(
const std::vector<Numeric> &x,
const std::vector<Numeric> &y,
841 const std::map<std::string, std::string> &keywords) {
842 assert(x.size() == y.size());
851 PyObject *args = PyTuple_New(2);
852 PyTuple_SetItem(args, 0, xarray);
853 PyTuple_SetItem(args, 1, yarray);
856 PyObject *kwargs = PyDict_New();
857 for (
auto it = keywords.begin(); it != keywords.end(); ++it) {
858 PyDict_SetItemString(kwargs, it->first.c_str(),
859 PyUnicode_FromString(it->second.c_str()));
862 PyObject *res = PyObject_Call(
874template <
typename Numeric>
875bool fill_between(
const std::vector<Numeric> &x,
const std::vector<Numeric> &y1,
876 const std::vector<Numeric> &y2,
877 const std::map<std::string, std::string> &keywords) {
878 assert(x.size() == y1.size());
879 assert(x.size() == y2.size());
889 PyObject *args = PyTuple_New(3);
890 PyTuple_SetItem(args, 0, xarray);
891 PyTuple_SetItem(args, 1, y1array);
892 PyTuple_SetItem(args, 2, y2array);
895 PyObject *kwargs = PyDict_New();
896 for (std::map<std::string, std::string>::const_iterator it = keywords.begin();
897 it != keywords.end(); ++it) {
898 PyDict_SetItemString(kwargs, it->first.c_str(),
899 PyUnicode_FromString(it->second.c_str()));
902 PyObject *res = PyObject_Call(
913template <
typename Numeric>
914bool arrow(Numeric x, Numeric y, Numeric end_x, Numeric end_y,
915 const std::string &fc =
"r",
const std::string ec =
"k",
916 Numeric head_length = 0.25, Numeric head_width = 0.1625) {
917 PyObject *obj_x = PyFloat_FromDouble(x);
918 PyObject *obj_y = PyFloat_FromDouble(y);
919 PyObject *obj_end_x = PyFloat_FromDouble(end_x);
920 PyObject *obj_end_y = PyFloat_FromDouble(end_y);
922 PyObject *kwargs = PyDict_New();
923 PyDict_SetItemString(kwargs,
"fc", PyString_FromString(fc.c_str()));
924 PyDict_SetItemString(kwargs,
"ec", PyString_FromString(ec.c_str()));
925 PyDict_SetItemString(kwargs,
"head_width", PyFloat_FromDouble(head_width));
926 PyDict_SetItemString(kwargs,
"head_length", PyFloat_FromDouble(head_length));
928 PyObject *plot_args = PyTuple_New(4);
929 PyTuple_SetItem(plot_args, 0, obj_x);
930 PyTuple_SetItem(plot_args, 1, obj_y);
931 PyTuple_SetItem(plot_args, 2, obj_end_x);
932 PyTuple_SetItem(plot_args, 3, obj_end_y);
934 PyObject *res = PyObject_Call(
937 Py_DECREF(plot_args);
945template <
typename Numeric>
946bool hist(
const std::vector<Numeric> &y,
long bins = 10,
947 std::string color =
"b",
double alpha = 1.0,
948 bool cumulative =
false) {
953 PyObject *kwargs = PyDict_New();
954 PyDict_SetItemString(kwargs,
"bins", PyLong_FromLong(bins));
955 PyDict_SetItemString(kwargs,
"color", PyString_FromString(color.c_str()));
956 PyDict_SetItemString(kwargs,
"alpha", PyFloat_FromDouble(alpha));
957 PyDict_SetItemString(kwargs,
"cumulative", cumulative ? Py_True : Py_False);
959 PyObject *plot_args = PyTuple_New(1);
961 PyTuple_SetItem(plot_args, 0, yarray);
963 PyObject *res = PyObject_Call(
966 Py_DECREF(plot_args);
977inline void imshow(
void *ptr,
const NPY_TYPES type,
const int rows,
978 const int columns,
const int colors,
979 const std::map<std::string, std::string> &keywords,
981 assert(type == NPY_UINT8 || type == NPY_FLOAT);
982 assert(colors == 1 || colors == 3 || colors == 4);
987 npy_intp dims[3] = {rows, columns, colors};
988 PyObject *args = PyTuple_New(1);
990 args, 0, PyArray_SimpleNewFromData(colors == 1 ? 2 : 3, dims, type, ptr));
993 PyObject *kwargs = PyDict_New();
994 for (std::map<std::string, std::string>::const_iterator it = keywords.begin();
995 it != keywords.end(); ++it) {
996 PyDict_SetItemString(kwargs, it->first.c_str(),
997 PyUnicode_FromString(it->second.c_str()));
1000 PyObject *res = PyObject_Call(
1005 throw std::runtime_error(
"Call to imshow() failed");
1014inline void imshow(
const unsigned char *ptr,
const int rows,
const int columns,
1016 const std::map<std::string, std::string> &keywords = {},
1017 PyObject **out =
nullptr) {
1018 detail::imshow((
void *)ptr, NPY_UINT8, rows, columns, colors, keywords, out);
1021inline void imshow(
const float *ptr,
const int rows,
const int columns,
1023 const std::map<std::string, std::string> &keywords = {},
1024 PyObject **out =
nullptr) {
1025 detail::imshow((
void *)ptr, NPY_FLOAT, rows, columns, colors, keywords, out);
1029void imshow(
const cv::Mat &image,
1030 const std::map<std::string, std::string> &keywords = {}) {
1033 NPY_TYPES npy_type = NPY_UINT8;
1034 switch (image.type() & CV_MAT_DEPTH_MASK) {
1040 npy_type = NPY_FLOAT;
1043 image.convertTo(image2, CV_MAKETYPE(CV_8U, image.channels()));
1047 switch (image2.channels()) {
1049 cv::cvtColor(image2, image2, CV_BGR2RGB);
1052 cv::cvtColor(image2, image2, CV_BGRA2RGBA);
1056 image2.channels(), keywords);
1061template <
typename NumericX,
typename NumericY>
1062bool scatter(
const std::vector<NumericX> &x,
const std::vector<NumericY> &y,
1063 const double s = 1.0,
1064 const std::map<std::string, std::string> &keywords = {}) {
1067 assert(x.size() == y.size());
1072 PyObject *kwargs = PyDict_New();
1073 PyDict_SetItemString(kwargs,
"s", PyLong_FromLong(s));
1074 for (
const auto &it : keywords) {
1075 PyDict_SetItemString(kwargs, it.first.c_str(),
1076 PyString_FromString(it.second.c_str()));
1079 PyObject *plot_args = PyTuple_New(2);
1080 PyTuple_SetItem(plot_args, 0, xarray);
1081 PyTuple_SetItem(plot_args, 1, yarray);
1083 PyObject *res = PyObject_Call(
1086 Py_DECREF(plot_args);
1094template <
typename NumericX,
typename NumericY,
typename NumericColors>
1096 const std::vector<NumericY> &y,
1097 const std::vector<NumericColors> &colors,
1098 const double s = 1.0,
1099 const std::map<std::string, std::string> &keywords = {}) {
1102 assert(x.size() == y.size());
1108 PyObject *kwargs = PyDict_New();
1109 PyDict_SetItemString(kwargs,
"s", PyLong_FromLong(s));
1110 PyDict_SetItemString(kwargs,
"c", colors_array);
1112 for (
const auto &it : keywords) {
1113 PyDict_SetItemString(kwargs, it.first.c_str(),
1114 PyString_FromString(it.second.c_str()));
1117 PyObject *plot_args = PyTuple_New(2);
1118 PyTuple_SetItem(plot_args, 0, xarray);
1119 PyTuple_SetItem(plot_args, 1, yarray);
1121 PyObject *res = PyObject_Call(
1124 Py_DECREF(plot_args);
1132template <
typename NumericX,
typename NumericY,
typename NumericZ>
1133bool scatter(
const std::vector<NumericX> &x,
const std::vector<NumericY> &y,
1134 const std::vector<NumericZ> &z,
1135 const double s = 1.0,
1136 const std::map<std::string, std::string> &keywords = {},
1137 const long fig_number = 0) {
1144 static PyObject *mpl_toolkitsmod =
nullptr, *axis3dmod =
nullptr;
1145 if (!mpl_toolkitsmod) {
1148 PyObject *mpl_toolkits = PyString_FromString(
"mpl_toolkits");
1149 PyObject *axis3d = PyString_FromString(
"mpl_toolkits.mplot3d");
1150 if (!mpl_toolkits || !axis3d) {
1151 throw std::runtime_error(
"couldnt create string");
1154 mpl_toolkitsmod = PyImport_Import(mpl_toolkits);
1155 Py_DECREF(mpl_toolkits);
1156 if (!mpl_toolkitsmod) {
1157 throw std::runtime_error(
"Error loading module mpl_toolkits!");
1160 axis3dmod = PyImport_Import(axis3d);
1163 throw std::runtime_error(
"Error loading module mpl_toolkits.mplot3d!");
1167 assert(x.size() == y.size());
1168 assert(y.size() == z.size());
1175 PyObject *args = PyTuple_New(3);
1176 PyTuple_SetItem(args, 0, xarray);
1177 PyTuple_SetItem(args, 1, yarray);
1178 PyTuple_SetItem(args, 2, zarray);
1181 PyObject *kwargs = PyDict_New();
1183 for (std::map<std::string, std::string>::const_iterator it = keywords.begin();
1184 it != keywords.end(); ++it) {
1185 PyDict_SetItemString(kwargs, it->first.c_str(),
1186 PyString_FromString(it->second.c_str()));
1188 PyObject *fig_args = PyTuple_New(1);
1189 PyObject *fig =
nullptr;
1190 PyTuple_SetItem(fig_args, 0, PyLong_FromLong(fig_number));
1191 PyObject *fig_exists = PyObject_CallObject(
1193 if (!PyObject_IsTrue(fig_exists)) {
1194 fig = PyObject_CallObject(
1198 fig = PyObject_CallObject(
1201 Py_DECREF(fig_exists);
1203 throw std::runtime_error(
"Call to figure() failed.");
1205 PyObject *gca_kwargs = PyDict_New();
1206 PyDict_SetItemString(gca_kwargs,
"projection", PyString_FromString(
"3d"));
1208 PyObject *gca = PyObject_GetAttrString(fig,
"gca");
1210 throw std::runtime_error(
"No gca");
1212 PyObject *
axis = PyObject_Call(
1216 throw std::runtime_error(
"No axis");
1220 Py_DECREF(gca_kwargs);
1222 PyObject *
plot3 = PyObject_GetAttrString(
axis,
"scatter");
1224 throw std::runtime_error(
"No 3D line plot");
1226 PyObject *res = PyObject_Call(
plot3, args, kwargs);
1228 throw std::runtime_error(
"Failed 3D line plot");
1240template <
typename Numeric>
1241bool boxplot(
const std::vector<std::vector<Numeric>> &data,
1242 const std::vector<std::string> &labels = {},
1243 const std::map<std::string, std::string> &keywords = {}) {
1247 PyObject *args = PyTuple_New(1);
1248 PyTuple_SetItem(args, 0, listlist);
1250 PyObject *kwargs = PyDict_New();
1253 if (!labels.empty() && labels.size() == data.size()) {
1258 for (
const auto &it : keywords) {
1259 PyDict_SetItemString(kwargs, it.first.c_str(),
1260 PyString_FromString(it.second.c_str()));
1263 PyObject *res = PyObject_Call(
1275template <
typename Numeric>
1277 const std::map<std::string, std::string> &keywords = {}) {
1281 PyObject *args = PyTuple_New(1);
1282 PyTuple_SetItem(args, 0, vector);
1284 PyObject *kwargs = PyDict_New();
1285 for (
const auto &it : keywords) {
1286 PyDict_SetItemString(kwargs, it.first.c_str(),
1287 PyString_FromString(it.second.c_str()));
1290 PyObject *res = PyObject_Call(
1302template <
typename Numeric>
1303bool bar(
const std::vector<Numeric> &x,
const std::vector<Numeric> &y,
1304 std::string ec =
"black", std::string ls =
"-",
double lw = 1.0,
1305 const std::map<std::string, std::string> &keywords = {}) {
1311 PyObject *kwargs = PyDict_New();
1313 PyDict_SetItemString(kwargs,
"ec", PyString_FromString(ec.c_str()));
1314 PyDict_SetItemString(kwargs,
"ls", PyString_FromString(ls.c_str()));
1315 PyDict_SetItemString(kwargs,
"lw", PyFloat_FromDouble(lw));
1317 for (std::map<std::string, std::string>::const_iterator it = keywords.begin();
1318 it != keywords.end(); ++it) {
1319 PyDict_SetItemString(kwargs, it->first.c_str(),
1320 PyUnicode_FromString(it->second.c_str()));
1323 PyObject *plot_args = PyTuple_New(2);
1324 PyTuple_SetItem(plot_args, 0, xarray);
1325 PyTuple_SetItem(plot_args, 1, yarray);
1327 PyObject *res = PyObject_Call(
1330 Py_DECREF(plot_args);
1338template <
typename Numeric>
1339bool bar(
const std::vector<Numeric> &y, std::string ec =
"black",
1340 std::string ls =
"-",
double lw = 1.0,
1341 const std::map<std::string, std::string> &keywords = {}) {
1342 using T =
typename std::remove_reference<
decltype(y)>::type::value_type;
1347 for (std::size_t i = 0; i < y.size(); i++) {
1351 return bar(x, y, ec, ls, lw, keywords);
1354template <
typename Numeric>
1355bool barh(
const std::vector<Numeric> &x,
const std::vector<Numeric> &y,
1356 std::string ec =
"black", std::string ls =
"-",
double lw = 1.0,
1357 const std::map<std::string, std::string> &keywords = {}) {
1361 PyObject *kwargs = PyDict_New();
1363 PyDict_SetItemString(kwargs,
"ec", PyString_FromString(ec.c_str()));
1364 PyDict_SetItemString(kwargs,
"ls", PyString_FromString(ls.c_str()));
1365 PyDict_SetItemString(kwargs,
"lw", PyFloat_FromDouble(lw));
1367 for (std::map<std::string, std::string>::const_iterator it = keywords.begin();
1368 it != keywords.end(); ++it) {
1369 PyDict_SetItemString(kwargs, it->first.c_str(),
1370 PyUnicode_FromString(it->second.c_str()));
1373 PyObject *plot_args = PyTuple_New(2);
1374 PyTuple_SetItem(plot_args, 0, xarray);
1375 PyTuple_SetItem(plot_args, 1, yarray);
1377 PyObject *res = PyObject_Call(
1380 Py_DECREF(plot_args);
1392 PyObject *kwargs = PyDict_New();
1393 for (std::map<std::string, double>::const_iterator it = keywords.begin();
1394 it != keywords.end(); ++it) {
1395 PyDict_SetItemString(kwargs, it->first.c_str(),
1396 PyFloat_FromDouble(it->second));
1399 PyObject *plot_args = PyTuple_New(0);
1401 PyObject *res = PyObject_Call(
1405 Py_DECREF(plot_args);
1413template <
typename Numeric>
1415 long bins = 10, std::string color =
"b",
double alpha = 1.0) {
1420 PyObject *kwargs = PyDict_New();
1421 PyDict_SetItemString(kwargs,
"label", PyString_FromString(label.c_str()));
1422 PyDict_SetItemString(kwargs,
"bins", PyLong_FromLong(bins));
1423 PyDict_SetItemString(kwargs,
"color", PyString_FromString(color.c_str()));
1424 PyDict_SetItemString(kwargs,
"alpha", PyFloat_FromDouble(alpha));
1426 PyObject *plot_args = PyTuple_New(1);
1427 PyTuple_SetItem(plot_args, 0, yarray);
1429 PyObject *res = PyObject_Call(
1432 Py_DECREF(plot_args);
1440template <
typename NumericX,
typename NumericY>
1441bool plot(
const std::vector<NumericX> &x,
const std::vector<NumericY> &y,
1442 const std::string &s =
"") {
1443 assert(x.size() == y.size());
1450 PyObject *pystring = PyString_FromString(s.c_str());
1452 PyObject *plot_args = PyTuple_New(3);
1453 PyTuple_SetItem(plot_args, 0, xarray);
1454 PyTuple_SetItem(plot_args, 1, yarray);
1455 PyTuple_SetItem(plot_args, 2, pystring);
1457 PyObject *res = PyObject_CallObject(
1460 Py_DECREF(plot_args);
1467template <
typename NumericX,
typename NumericY,
typename NumericZ>
1468bool contour(
const std::vector<NumericX> &x,
const std::vector<NumericY> &y,
1469 const std::vector<NumericZ> &z,
1470 const std::map<std::string, std::string> &keywords = {}) {
1471 assert(x.size() == y.size() && x.size() == z.size());
1477 PyObject *plot_args = PyTuple_New(3);
1478 PyTuple_SetItem(plot_args, 0, xarray);
1479 PyTuple_SetItem(plot_args, 1, yarray);
1480 PyTuple_SetItem(plot_args, 2, zarray);
1483 PyObject *kwargs = PyDict_New();
1484 for (std::map<std::string, std::string>::const_iterator it = keywords.begin();
1485 it != keywords.end(); ++it) {
1486 PyDict_SetItemString(kwargs, it->first.c_str(),
1487 PyUnicode_FromString(it->second.c_str()));
1490 PyObject *res = PyObject_Call(
1494 Py_DECREF(plot_args);
1501template <
typename NumericX,
typename NumericY,
typename NumericU,
1503bool quiver(
const std::vector<NumericX> &x,
const std::vector<NumericY> &y,
1504 const std::vector<NumericU> &u,
const std::vector<NumericW> &w,
1505 const std::map<std::string, std::string> &keywords = {}) {
1506 assert(x.size() == y.size() && x.size() == u.size() && u.size() == w.size());
1515 PyObject *plot_args = PyTuple_New(4);
1516 PyTuple_SetItem(plot_args, 0, xarray);
1517 PyTuple_SetItem(plot_args, 1, yarray);
1518 PyTuple_SetItem(plot_args, 2, uarray);
1519 PyTuple_SetItem(plot_args, 3, warray);
1522 PyObject *kwargs = PyDict_New();
1523 for (std::map<std::string, std::string>::const_iterator it = keywords.begin();
1524 it != keywords.end(); ++it) {
1525 PyDict_SetItemString(kwargs, it->first.c_str(),
1526 PyUnicode_FromString(it->second.c_str()));
1529 PyObject *res = PyObject_Call(
1533 Py_DECREF(plot_args);
1540template <
typename NumericX,
typename NumericY,
typename NumericZ,
1541 typename NumericU,
typename NumericW,
typename NumericV>
1542bool quiver(
const std::vector<NumericX> &x,
const std::vector<NumericY> &y,
1543 const std::vector<NumericZ> &z,
const std::vector<NumericU> &u,
1544 const std::vector<NumericW> &w,
const std::vector<NumericV> &v,
1545 const std::map<std::string, std::string> &keywords = {}) {
1547 static PyObject *mpl_toolkitsmod =
nullptr, *axis3dmod =
nullptr;
1548 if (!mpl_toolkitsmod) {
1551 PyObject *mpl_toolkits = PyString_FromString(
"mpl_toolkits");
1552 PyObject *axis3d = PyString_FromString(
"mpl_toolkits.mplot3d");
1553 if (!mpl_toolkits || !axis3d) {
1554 throw std::runtime_error(
"couldnt create string");
1557 mpl_toolkitsmod = PyImport_Import(mpl_toolkits);
1558 Py_DECREF(mpl_toolkits);
1559 if (!mpl_toolkitsmod) {
1560 throw std::runtime_error(
"Error loading module mpl_toolkits!");
1563 axis3dmod = PyImport_Import(axis3d);
1566 throw std::runtime_error(
"Error loading module mpl_toolkits.mplot3d!");
1571 assert(x.size() == y.size() && x.size() == u.size() && u.size() == w.size() &&
1572 x.size() == z.size() && x.size() == v.size() && u.size() == v.size());
1584 PyObject *plot_args = PyTuple_New(6);
1585 PyTuple_SetItem(plot_args, 0, xarray);
1586 PyTuple_SetItem(plot_args, 1, yarray);
1587 PyTuple_SetItem(plot_args, 2, zarray);
1588 PyTuple_SetItem(plot_args, 3, uarray);
1589 PyTuple_SetItem(plot_args, 4, warray);
1590 PyTuple_SetItem(plot_args, 5, varray);
1593 PyObject *kwargs = PyDict_New();
1594 for (std::map<std::string, std::string>::const_iterator it = keywords.begin();
1595 it != keywords.end(); ++it) {
1596 PyDict_SetItemString(kwargs, it->first.c_str(),
1597 PyUnicode_FromString(it->second.c_str()));
1605 throw std::runtime_error(
"Call to figure() failed.");
1607 PyObject *gca_kwargs = PyDict_New();
1608 PyDict_SetItemString(gca_kwargs,
"projection", PyString_FromString(
"3d"));
1610 PyObject *gca = PyObject_GetAttrString(fig,
"gca");
1612 throw std::runtime_error(
"No gca");
1614 PyObject *
axis = PyObject_Call(
1618 throw std::runtime_error(
"No axis");
1621 Py_DECREF(gca_kwargs);
1624 PyObject *
plot3 = PyObject_GetAttrString(
axis,
"quiver");
1626 throw std::runtime_error(
"No 3D line plot");
1628 PyObject *res = PyObject_Call(
plot3, plot_args, kwargs);
1630 throw std::runtime_error(
"Failed 3D plot");
1634 Py_DECREF(plot_args);
1641template <
typename NumericX,
typename NumericY>
1642bool stem(
const std::vector<NumericX> &x,
const std::vector<NumericY> &y,
1643 const std::string &s =
"") {
1644 assert(x.size() == y.size());
1651 PyObject *pystring = PyString_FromString(s.c_str());
1653 PyObject *plot_args = PyTuple_New(3);
1654 PyTuple_SetItem(plot_args, 0, xarray);
1655 PyTuple_SetItem(plot_args, 1, yarray);
1656 PyTuple_SetItem(plot_args, 2, pystring);
1658 PyObject *res = PyObject_CallObject(
1661 Py_DECREF(plot_args);
1668template <
typename NumericX,
typename NumericY>
1669bool semilogx(
const std::vector<NumericX> &x,
const std::vector<NumericY> &y,
1670 const std::string &s =
"") {
1671 assert(x.size() == y.size());
1678 PyObject *pystring = PyString_FromString(s.c_str());
1680 PyObject *plot_args = PyTuple_New(3);
1681 PyTuple_SetItem(plot_args, 0, xarray);
1682 PyTuple_SetItem(plot_args, 1, yarray);
1683 PyTuple_SetItem(plot_args, 2, pystring);
1685 PyObject *res = PyObject_CallObject(
1688 Py_DECREF(plot_args);
1695template <
typename NumericX,
typename NumericY>
1696bool semilogy(
const std::vector<NumericX> &x,
const std::vector<NumericY> &y,
1697 const std::string &s =
"") {
1698 assert(x.size() == y.size());
1705 PyObject *pystring = PyString_FromString(s.c_str());
1707 PyObject *plot_args = PyTuple_New(3);
1708 PyTuple_SetItem(plot_args, 0, xarray);
1709 PyTuple_SetItem(plot_args, 1, yarray);
1710 PyTuple_SetItem(plot_args, 2, pystring);
1712 PyObject *res = PyObject_CallObject(
1715 Py_DECREF(plot_args);
1722template <
typename NumericX,
typename NumericY>
1723bool loglog(
const std::vector<NumericX> &x,
const std::vector<NumericY> &y,
1724 const std::string &s =
"") {
1725 assert(x.size() == y.size());
1732 PyObject *pystring = PyString_FromString(s.c_str());
1734 PyObject *plot_args = PyTuple_New(3);
1735 PyTuple_SetItem(plot_args, 0, xarray);
1736 PyTuple_SetItem(plot_args, 1, yarray);
1737 PyTuple_SetItem(plot_args, 2, pystring);
1739 PyObject *res = PyObject_CallObject(
1742 Py_DECREF(plot_args);
1749template <
typename NumericX,
typename NumericY>
1750bool errorbar(
const std::vector<NumericX> &x,
const std::vector<NumericY> &y,
1751 const std::vector<NumericX> &yerr,
1752 const std::map<std::string, std::string> &keywords = {}) {
1753 assert(x.size() == y.size());
1762 PyObject *kwargs = PyDict_New();
1763 for (std::map<std::string, std::string>::const_iterator it = keywords.begin();
1764 it != keywords.end(); ++it) {
1765 PyDict_SetItemString(kwargs, it->first.c_str(),
1766 PyString_FromString(it->second.c_str()));
1769 PyDict_SetItemString(kwargs,
"yerr", yerrarray);
1771 PyObject *plot_args = PyTuple_New(2);
1772 PyTuple_SetItem(plot_args, 0, xarray);
1773 PyTuple_SetItem(plot_args, 1, yarray);
1780 Py_DECREF(plot_args);
1785 throw std::runtime_error(
"Call to errorbar() failed.");
1790template <
typename Numeric>
1791bool named_plot(
const std::string &name,
const std::vector<Numeric> &y,
1792 const std::string &format =
"") {
1795 PyObject *kwargs = PyDict_New();
1796 PyDict_SetItemString(kwargs,
"label", PyString_FromString(name.c_str()));
1800 PyObject *pystring = PyString_FromString(format.c_str());
1802 PyObject *plot_args = PyTuple_New(2);
1804 PyTuple_SetItem(plot_args, 0, yarray);
1805 PyTuple_SetItem(plot_args, 1, pystring);
1807 PyObject *res = PyObject_Call(
1811 Py_DECREF(plot_args);
1818template <
typename NumericX,
typename NumericY>
1819bool named_plot(
const std::string &name,
const std::vector<NumericX> &x,
1820 const std::vector<NumericY> &y,
1821 const std::string &format =
"") {
1824 PyObject *kwargs = PyDict_New();
1825 PyDict_SetItemString(kwargs,
"label", PyString_FromString(name.c_str()));
1830 PyObject *pystring = PyString_FromString(format.c_str());
1832 PyObject *plot_args = PyTuple_New(3);
1833 PyTuple_SetItem(plot_args, 0, xarray);
1834 PyTuple_SetItem(plot_args, 1, yarray);
1835 PyTuple_SetItem(plot_args, 2, pystring);
1837 PyObject *res = PyObject_Call(
1841 Py_DECREF(plot_args);
1848template <
typename NumericX,
typename NumericY>
1850 const std::vector<NumericY> &y,
1851 const std::string &format =
"") {
1854 PyObject *kwargs = PyDict_New();
1855 PyDict_SetItemString(kwargs,
"label", PyString_FromString(name.c_str()));
1860 PyObject *pystring = PyString_FromString(format.c_str());
1862 PyObject *plot_args = PyTuple_New(3);
1863 PyTuple_SetItem(plot_args, 0, xarray);
1864 PyTuple_SetItem(plot_args, 1, yarray);
1865 PyTuple_SetItem(plot_args, 2, pystring);
1872 Py_DECREF(plot_args);
1879template <
typename NumericX,
typename NumericY>
1881 const std::vector<NumericY> &y,
1882 const std::string &format =
"") {
1885 PyObject *kwargs = PyDict_New();
1886 PyDict_SetItemString(kwargs,
"label", PyString_FromString(name.c_str()));
1891 PyObject *pystring = PyString_FromString(format.c_str());
1893 PyObject *plot_args = PyTuple_New(3);
1894 PyTuple_SetItem(plot_args, 0, xarray);
1895 PyTuple_SetItem(plot_args, 1, yarray);
1896 PyTuple_SetItem(plot_args, 2, pystring);
1903 Py_DECREF(plot_args);
1910template <
typename NumericX,
typename NumericY>
1912 const std::vector<NumericY> &y,
1913 const std::string &format =
"") {
1916 PyObject *kwargs = PyDict_New();
1917 PyDict_SetItemString(kwargs,
"label", PyString_FromString(name.c_str()));
1922 PyObject *pystring = PyString_FromString(format.c_str());
1924 PyObject *plot_args = PyTuple_New(3);
1925 PyTuple_SetItem(plot_args, 0, xarray);
1926 PyTuple_SetItem(plot_args, 1, yarray);
1927 PyTuple_SetItem(plot_args, 2, pystring);
1928 PyObject *res = PyObject_Call(
1932 Py_DECREF(plot_args);
1939template <
typename Numeric>
1940bool plot(
const std::vector<Numeric> &y,
const std::string &format =
"") {
1941 std::vector<Numeric> x(y.size());
1942 for (
size_t i = 0; i < x.size(); ++i)
1944 return plot(x, y, format);
1947template <
typename Numeric>
1948bool plot(
const std::vector<Numeric> &y,
1949 const std::map<std::string, std::string> &keywords) {
1950 std::vector<Numeric> x(y.size());
1951 for (
size_t i = 0; i < x.size(); ++i)
1953 return plot(x, y, keywords);
1956template <
typename Numeric>
1957bool stem(
const std::vector<Numeric> &y,
const std::string &format =
"") {
1958 std::vector<Numeric> x(y.size());
1959 for (
size_t i = 0; i < x.size(); ++i)
1961 return stem(x, y, format);
1964template <
typename Numeric>
1965void text(Numeric x, Numeric y,
const std::string &s =
"") {
1968 PyObject *args = PyTuple_New(3);
1969 PyTuple_SetItem(args, 0, PyFloat_FromDouble(x));
1970 PyTuple_SetItem(args, 1, PyFloat_FromDouble(y));
1971 PyTuple_SetItem(args, 2, PyString_FromString(s.c_str()));
1973 PyObject *res = PyObject_CallObject(
1976 throw std::runtime_error(
"Call to text() failed.");
1983 const std::map<std::string, float> &keywords = {}) {
1984 if (mappable == NULL)
1985 throw std::runtime_error(
"Must call colorbar with PyObject* returned from "
1986 "an image, contour, surface, etc.");
1990 PyObject *args = PyTuple_New(1);
1991 PyTuple_SetItem(args, 0, mappable);
1993 PyObject *kwargs = PyDict_New();
1994 for (std::map<std::string, float>::const_iterator it = keywords.begin();
1995 it != keywords.end(); ++it) {
1996 PyDict_SetItemString(kwargs, it->first.c_str(),
1997 PyFloat_FromDouble(it->second));
2000 PyObject *res = PyObject_Call(
2003 throw std::runtime_error(
"Call to colorbar() failed.");
2015 res = PyObject_CallObject(
2024 PyObject *args = PyTuple_New(1);
2025 PyTuple_SetItem(args, 0, PyLong_FromLong(number));
2026 res = PyObject_CallObject(
2032 throw std::runtime_error(
"Call to figure() failed.");
2034 PyObject *num = PyObject_GetAttrString(res,
"number");
2036 throw std::runtime_error(
"Could not get number attribute of figure object");
2037 const long figureNumber = PyLong_AsLong(num);
2042 return figureNumber;
2048 PyObject *args = PyTuple_New(1);
2049 PyTuple_SetItem(args, 0, PyLong_FromLong(number));
2050 PyObject *res = PyObject_CallObject(
2053 throw std::runtime_error(
"Call to fignum_exists() failed.");
2055 bool ret = PyObject_IsTrue(res);
2065 const size_t dpi = 100;
2066 PyObject *size = PyTuple_New(2);
2067 PyTuple_SetItem(size, 0, PyFloat_FromDouble((
double)w / dpi));
2068 PyTuple_SetItem(size, 1, PyFloat_FromDouble((
double)h / dpi));
2070 PyObject *kwargs = PyDict_New();
2071 PyDict_SetItemString(kwargs,
"figsize", size);
2072 PyDict_SetItemString(kwargs,
"dpi", PyLong_FromSize_t(dpi));
2081 throw std::runtime_error(
"Call to figure_size() failed.");
2092 throw std::runtime_error(
"Call to legend() failed.");
2097inline void legend(
const std::map<std::string, std::string> &keywords) {
2101 PyObject *kwargs = PyDict_New();
2102 for (std::map<std::string, std::string>::const_iterator it = keywords.begin();
2103 it != keywords.end(); ++it) {
2104 PyDict_SetItemString(kwargs, it->first.c_str(),
2105 PyString_FromString(it->second.c_str()));
2112 throw std::runtime_error(
"Call to legend() failed.");
2118template <
typename Numeric>
inline void set_aspect(Numeric ratio) {
2121 PyObject *args = PyTuple_New(1);
2122 PyTuple_SetItem(args, 0, PyFloat_FromDouble(ratio));
2123 PyObject *kwargs = PyDict_New();
2129 throw std::runtime_error(
"Call to gca() failed.");
2132 PyObject *
set_aspect = PyObject_GetAttrString(ax,
"set_aspect");
2134 throw std::runtime_error(
"Attribute set_aspect not found.");
2137 PyObject *res = PyObject_Call(
set_aspect, args, kwargs);
2139 throw std::runtime_error(
"Call to set_aspect() failed.");
2151 PyObject *args = PyTuple_New(1);
2152 PyTuple_SetItem(args, 0, PyString_FromString(
"equal"));
2153 PyObject *kwargs = PyDict_New();
2159 throw std::runtime_error(
"Call to gca() failed.");
2162 PyObject *
set_aspect = PyObject_GetAttrString(ax,
"set_aspect");
2164 throw std::runtime_error(
"Attribute set_aspect not found.");
2167 PyObject *res = PyObject_Call(
set_aspect, args, kwargs);
2169 throw std::runtime_error(
"Call to set_aspect() failed.");
2177template <
typename Numeric>
void ylim(Numeric left, Numeric right) {
2180 PyObject *list = PyList_New(2);
2181 PyList_SetItem(list, 0, PyFloat_FromDouble(left));
2182 PyList_SetItem(list, 1, PyFloat_FromDouble(right));
2184 PyObject *args = PyTuple_New(1);
2185 PyTuple_SetItem(args, 0, list);
2187 PyObject *res = PyObject_CallObject(
2190 throw std::runtime_error(
"Call to ylim() failed.");
2196template <
typename Numeric>
void xlim(Numeric left, Numeric right) {
2199 PyObject *list = PyList_New(2);
2200 PyList_SetItem(list, 0, PyFloat_FromDouble(left));
2201 PyList_SetItem(list, 1, PyFloat_FromDouble(right));
2203 PyObject *args = PyTuple_New(1);
2204 PyTuple_SetItem(args, 0, list);
2206 PyObject *res = PyObject_CallObject(
2209 throw std::runtime_error(
"Call to xlim() failed.");
2215inline std::array<double, 2>
xlim() {
2216 PyObject *args = PyTuple_New(0);
2217 PyObject *res = PyObject_CallObject(
2221 throw std::runtime_error(
"Call to xlim() failed.");
2225 PyObject *left = PyTuple_GetItem(res, 0);
2226 PyObject *right = PyTuple_GetItem(res, 1);
2227 return {PyFloat_AsDouble(left), PyFloat_AsDouble(right)};
2230inline std::array<double, 2>
ylim() {
2231 PyObject *args = PyTuple_New(0);
2232 PyObject *res = PyObject_CallObject(
2236 throw std::runtime_error(
"Call to ylim() failed.");
2240 PyObject *left = PyTuple_GetItem(res, 0);
2241 PyObject *right = PyTuple_GetItem(res, 1);
2242 return {PyFloat_AsDouble(left), PyFloat_AsDouble(right)};
2245template <
typename Numeric>
2246inline void xticks(
const std::vector<Numeric> &ticks,
2247 const std::vector<std::string> &labels = {},
2248 const std::map<std::string, std::string> &keywords = {}) {
2249 assert(labels.size() == 0 || ticks.size() == labels.size());
2257 if (labels.size() == 0) {
2259 args = PyTuple_New(1);
2260 PyTuple_SetItem(args, 0, ticksarray);
2263 PyObject *labelstuple = PyTuple_New(labels.size());
2264 for (
size_t i = 0; i < labels.size(); i++)
2265 PyTuple_SetItem(labelstuple, i, PyUnicode_FromString(labels[i].c_str()));
2268 args = PyTuple_New(2);
2269 PyTuple_SetItem(args, 0, ticksarray);
2270 PyTuple_SetItem(args, 1, labelstuple);
2274 PyObject *kwargs = PyDict_New();
2275 for (std::map<std::string, std::string>::const_iterator it = keywords.begin();
2276 it != keywords.end(); ++it) {
2277 PyDict_SetItemString(kwargs, it->first.c_str(),
2278 PyString_FromString(it->second.c_str()));
2281 PyObject *res = PyObject_Call(
2287 throw std::runtime_error(
"Call to xticks() failed");
2292template <
typename Numeric>
2293inline void xticks(
const std::vector<Numeric> &ticks,
2294 const std::map<std::string, std::string> &keywords) {
2295 xticks(ticks, {}, keywords);
2298template <
typename Numeric>
2299inline void yticks(
const std::vector<Numeric> &ticks,
2300 const std::vector<std::string> &labels = {},
2301 const std::map<std::string, std::string> &keywords = {}) {
2302 assert(labels.size() == 0 || ticks.size() == labels.size());
2310 if (labels.size() == 0) {
2312 args = PyTuple_New(1);
2313 PyTuple_SetItem(args, 0, ticksarray);
2316 PyObject *labelstuple = PyTuple_New(labels.size());
2317 for (
size_t i = 0; i < labels.size(); i++)
2318 PyTuple_SetItem(labelstuple, i, PyUnicode_FromString(labels[i].c_str()));
2321 args = PyTuple_New(2);
2322 PyTuple_SetItem(args, 0, ticksarray);
2323 PyTuple_SetItem(args, 1, labelstuple);
2327 PyObject *kwargs = PyDict_New();
2328 for (std::map<std::string, std::string>::const_iterator it = keywords.begin();
2329 it != keywords.end(); ++it) {
2330 PyDict_SetItemString(kwargs, it->first.c_str(),
2331 PyString_FromString(it->second.c_str()));
2334 PyObject *res = PyObject_Call(
2340 throw std::runtime_error(
"Call to yticks() failed");
2345template <
typename Numeric>
2346inline void yticks(
const std::vector<Numeric> &ticks,
2347 const std::map<std::string, std::string> &keywords) {
2348 yticks(ticks, {}, keywords);
2351template <
typename Numeric>
inline void margins(Numeric margin) {
2353 PyObject *args = PyTuple_New(1);
2354 PyTuple_SetItem(args, 0, PyFloat_FromDouble(margin));
2356 PyObject *res = PyObject_CallObject(
2359 throw std::runtime_error(
"Call to margins() failed.");
2365template <
typename Numeric>
2366inline void margins(Numeric margin_x, Numeric margin_y) {
2368 PyObject *args = PyTuple_New(2);
2369 PyTuple_SetItem(args, 0, PyFloat_FromDouble(margin_x));
2370 PyTuple_SetItem(args, 1, PyFloat_FromDouble(margin_y));
2372 PyObject *res = PyObject_CallObject(
2375 throw std::runtime_error(
"Call to margins() failed.");
2381inline void tick_params(
const std::map<std::string, std::string> &keywords,
2382 const std::string
axis =
"both") {
2387 args = PyTuple_New(1);
2388 PyTuple_SetItem(args, 0, PyString_FromString(
axis.c_str()));
2391 PyObject *kwargs = PyDict_New();
2392 for (std::map<std::string, std::string>::const_iterator it = keywords.begin();
2393 it != keywords.end(); ++it) {
2394 PyDict_SetItemString(kwargs, it->first.c_str(),
2395 PyString_FromString(it->second.c_str()));
2398 PyObject *res = PyObject_Call(
2404 throw std::runtime_error(
"Call to tick_params() failed");
2409inline void subplot(
long nrows,
long ncols,
long plot_number) {
2413 PyObject *args = PyTuple_New(3);
2414 PyTuple_SetItem(args, 0, PyFloat_FromDouble(nrows));
2415 PyTuple_SetItem(args, 1, PyFloat_FromDouble(ncols));
2416 PyTuple_SetItem(args, 2, PyFloat_FromDouble(plot_number));
2418 PyObject *res = PyObject_CallObject(
2421 throw std::runtime_error(
"Call to subplot() failed.");
2427inline void subplot2grid(
long nrows,
long ncols,
long rowid = 0,
long colid = 0,
2428 long rowspan = 1,
long colspan = 1) {
2431 PyObject *shape = PyTuple_New(2);
2432 PyTuple_SetItem(shape, 0, PyLong_FromLong(nrows));
2433 PyTuple_SetItem(shape, 1, PyLong_FromLong(ncols));
2435 PyObject *loc = PyTuple_New(2);
2436 PyTuple_SetItem(loc, 0, PyLong_FromLong(rowid));
2437 PyTuple_SetItem(loc, 1, PyLong_FromLong(colid));
2439 PyObject *args = PyTuple_New(4);
2440 PyTuple_SetItem(args, 0, shape);
2441 PyTuple_SetItem(args, 1, loc);
2442 PyTuple_SetItem(args, 2, PyLong_FromLong(rowspan));
2443 PyTuple_SetItem(args, 3, PyLong_FromLong(colspan));
2445 PyObject *res = PyObject_CallObject(
2448 throw std::runtime_error(
"Call to subplot2grid() failed.");
2456inline void title(
const std::string &titlestr,
2457 const std::map<std::string, std::string> &keywords = {}) {
2460 PyObject *pytitlestr = PyString_FromString(titlestr.c_str());
2461 PyObject *args = PyTuple_New(1);
2462 PyTuple_SetItem(args, 0, pytitlestr);
2464 PyObject *kwargs = PyDict_New();
2465 for (
auto it = keywords.begin(); it != keywords.end(); ++it) {
2466 PyDict_SetItemString(kwargs, it->first.c_str(),
2467 PyUnicode_FromString(it->second.c_str()));
2470 PyObject *res = PyObject_Call(
2473 throw std::runtime_error(
"Call to title() failed.");
2481 const std::map<std::string, std::string> &keywords = {}) {
2484 PyObject *pysuptitlestr = PyString_FromString(suptitlestr.c_str());
2485 PyObject *args = PyTuple_New(1);
2486 PyTuple_SetItem(args, 0, pysuptitlestr);
2488 PyObject *kwargs = PyDict_New();
2489 for (
auto it = keywords.begin(); it != keywords.end(); ++it) {
2490 PyDict_SetItemString(kwargs, it->first.c_str(),
2491 PyUnicode_FromString(it->second.c_str()));
2494 PyObject *res = PyObject_Call(
2497 throw std::runtime_error(
"Call to suptitle() failed.");
2504inline void axis(
const std::string &axisstr) {
2507 PyObject *str = PyString_FromString(axisstr.c_str());
2508 PyObject *args = PyTuple_New(1);
2509 PyTuple_SetItem(args, 0, str);
2511 PyObject *res = PyObject_CallObject(
2514 throw std::runtime_error(
"Call to title() failed.");
2520inline void axhline(
double y,
double xmin = 0.,
double xmax = 1.,
2521 const std::map<std::string, std::string> &keywords =
2522 std::map<std::string, std::string>()) {
2526 PyObject *args = PyTuple_New(3);
2527 PyTuple_SetItem(args, 0, PyFloat_FromDouble(y));
2528 PyTuple_SetItem(args, 1, PyFloat_FromDouble(xmin));
2529 PyTuple_SetItem(args, 2, PyFloat_FromDouble(xmax));
2532 PyObject *kwargs = PyDict_New();
2533 for (std::map<std::string, std::string>::const_iterator it = keywords.begin();
2534 it != keywords.end(); ++it) {
2535 PyDict_SetItemString(kwargs, it->first.c_str(),
2536 PyString_FromString(it->second.c_str()));
2539 PyObject *res = PyObject_Call(
2549inline void axvline(
double x,
double ymin = 0.,
double ymax = 1.,
2550 const std::map<std::string, std::string> &keywords =
2551 std::map<std::string, std::string>()) {
2555 PyObject *args = PyTuple_New(3);
2556 PyTuple_SetItem(args, 0, PyFloat_FromDouble(x));
2557 PyTuple_SetItem(args, 1, PyFloat_FromDouble(ymin));
2558 PyTuple_SetItem(args, 2, PyFloat_FromDouble(ymax));
2561 PyObject *kwargs = PyDict_New();
2562 for (std::map<std::string, std::string>::const_iterator it = keywords.begin();
2563 it != keywords.end(); ++it) {
2564 PyDict_SetItemString(kwargs, it->first.c_str(),
2565 PyString_FromString(it->second.c_str()));
2568 PyObject *res = PyObject_Call(
2578inline void axvspan(
double xmin,
double xmax,
double ymin = 0.,
2580 const std::map<std::string, std::string> &keywords =
2581 std::map<std::string, std::string>()) {
2583 PyObject *args = PyTuple_New(4);
2584 PyTuple_SetItem(args, 0, PyFloat_FromDouble(xmin));
2585 PyTuple_SetItem(args, 1, PyFloat_FromDouble(xmax));
2586 PyTuple_SetItem(args, 2, PyFloat_FromDouble(ymin));
2587 PyTuple_SetItem(args, 3, PyFloat_FromDouble(ymax));
2590 PyObject *kwargs = PyDict_New();
2591 for (
auto it = keywords.begin(); it != keywords.end(); ++it) {
2592 if (it->first ==
"linewidth" || it->first ==
"alpha") {
2593 PyDict_SetItemString(kwargs, it->first.c_str(),
2594 PyFloat_FromDouble(std::stod(it->second)));
2596 PyDict_SetItemString(kwargs, it->first.c_str(),
2597 PyString_FromString(it->second.c_str()));
2601 PyObject *res = PyObject_Call(
2611 const std::map<std::string, std::string> &keywords = {}) {
2614 PyObject *pystr = PyString_FromString(str.c_str());
2615 PyObject *args = PyTuple_New(1);
2616 PyTuple_SetItem(args, 0, pystr);
2618 PyObject *kwargs = PyDict_New();
2619 for (
auto it = keywords.begin(); it != keywords.end(); ++it) {
2620 PyDict_SetItemString(kwargs, it->first.c_str(),
2621 PyUnicode_FromString(it->second.c_str()));
2624 PyObject *res = PyObject_Call(
2627 throw std::runtime_error(
"Call to xlabel() failed.");
2635 const std::map<std::string, std::string> &keywords = {}) {
2638 PyObject *pystr = PyString_FromString(str.c_str());
2639 PyObject *args = PyTuple_New(1);
2640 PyTuple_SetItem(args, 0, pystr);
2642 PyObject *kwargs = PyDict_New();
2643 for (
auto it = keywords.begin(); it != keywords.end(); ++it) {
2644 PyDict_SetItemString(kwargs, it->first.c_str(),
2645 PyUnicode_FromString(it->second.c_str()));
2648 PyObject *res = PyObject_Call(
2651 throw std::runtime_error(
"Call to ylabel() failed.");
2660 const std::map<std::string, std::string> &keywords = {}) {
2667 static PyObject *mpl_toolkitsmod =
nullptr, *axis3dmod =
nullptr;
2668 if (!mpl_toolkitsmod) {
2669 PyObject *mpl_toolkits = PyString_FromString(
"mpl_toolkits");
2670 PyObject *axis3d = PyString_FromString(
"mpl_toolkits.mplot3d");
2671 if (!mpl_toolkits || !axis3d) {
2672 throw std::runtime_error(
"couldnt create string");
2675 mpl_toolkitsmod = PyImport_Import(mpl_toolkits);
2676 Py_DECREF(mpl_toolkits);
2677 if (!mpl_toolkitsmod) {
2678 throw std::runtime_error(
"Error loading module mpl_toolkits!");
2681 axis3dmod = PyImport_Import(axis3d);
2684 throw std::runtime_error(
"Error loading module mpl_toolkits.mplot3d!");
2688 PyObject *pystr = PyString_FromString(str.c_str());
2689 PyObject *args = PyTuple_New(1);
2690 PyTuple_SetItem(args, 0, pystr);
2692 PyObject *kwargs = PyDict_New();
2693 for (
auto it = keywords.begin(); it != keywords.end(); ++it) {
2694 PyDict_SetItemString(kwargs, it->first.c_str(),
2695 PyUnicode_FromString(it->second.c_str()));
2702 throw std::runtime_error(
"Call to gca() failed.");
2705 PyObject *zlabel = PyObject_GetAttrString(ax,
"set_zlabel");
2707 throw std::runtime_error(
"Attribute set_zlabel not found.");
2710 PyObject *res = PyObject_Call(zlabel, args, kwargs);
2712 throw std::runtime_error(
"Call to set_zlabel() failed.");
2725 PyObject *pyflag = flag ? Py_True : Py_False;
2728 PyObject *args = PyTuple_New(1);
2729 PyTuple_SetItem(args, 0, pyflag);
2731 PyObject *res = PyObject_CallObject(
2734 throw std::runtime_error(
"Call to grid() failed.");
2740inline void show(
const bool block =
true) {
2749 PyObject *kwargs = PyDict_New();
2750 PyDict_SetItemString(kwargs,
"block", Py_False);
2758 throw std::runtime_error(
"Call to show() failed.");
2771 throw std::runtime_error(
"Call to close() failed.");
2780 PyObject *kwargs = PyDict_New();
2788 throw std::runtime_error(
"Call to show() failed.");
2801 throw std::runtime_error(
"Call to draw() failed.");
2806template <
typename Numeric>
inline void pause(Numeric interval) {
2809 PyObject *args = PyTuple_New(1);
2810 PyTuple_SetItem(args, 0, PyFloat_FromDouble(interval));
2812 PyObject *res = PyObject_CallObject(
2815 throw std::runtime_error(
"Call to pause() failed.");
2821inline void save(
const std::string &filename,
const int dpi = 0) {
2824 PyObject *pyfilename = PyString_FromString(filename.c_str());
2826 PyObject *args = PyTuple_New(1);
2827 PyTuple_SetItem(args, 0, pyfilename);
2829 PyObject *kwargs = PyDict_New();
2832 PyDict_SetItemString(kwargs,
"dpi", PyLong_FromLong(dpi));
2835 PyObject *res = PyObject_Call(
2838 throw std::runtime_error(
"Call to save() failed.");
2845inline void rcparams(
const std::map<std::string, std::string> &keywords = {}) {
2847 PyObject *args = PyTuple_New(0);
2848 PyObject *kwargs = PyDict_New();
2849 for (
auto it = keywords.begin(); it != keywords.end(); ++it) {
2850 if (
"text.usetex" == it->first)
2851 PyDict_SetItemString(kwargs, it->first.c_str(),
2852 PyLong_FromLong(std::stoi(it->second.c_str())));
2854 PyDict_SetItemString(kwargs, it->first.c_str(),
2855 PyString_FromString(it->second.c_str()));
2858 PyObject *update = PyObject_GetAttrString(
2860 PyObject *res = PyObject_Call(update, args, kwargs);
2862 throw std::runtime_error(
"Call to rcParams.update() failed.");
2877 throw std::runtime_error(
"Call to clf() failed.");
2890 throw std::runtime_error(
"Call to cla() failed.");
2903 throw std::runtime_error(
"Call to ion() failed.");
2908inline std::vector<std::array<double, 2>>
2910 const std::map<std::string, std::string> &keywords = {}) {
2913 PyObject *args = PyTuple_New(1);
2914 PyTuple_SetItem(args, 0, PyLong_FromLong(numClicks));
2917 PyObject *kwargs = PyDict_New();
2918 for (std::map<std::string, std::string>::const_iterator it = keywords.begin();
2919 it != keywords.end(); ++it) {
2920 PyDict_SetItemString(kwargs, it->first.c_str(),
2921 PyUnicode_FromString(it->second.c_str()));
2924 PyObject *res = PyObject_Call(
2930 throw std::runtime_error(
"Call to ginput() failed.");
2932 const size_t len = PyList_Size(res);
2933 std::vector<std::array<double, 2>> out;
2935 for (
size_t i = 0; i < len; i++) {
2936 PyObject *current = PyList_GetItem(res, i);
2937 std::array<double, 2> position;
2938 position[0] = PyFloat_AsDouble(PyTuple_GetItem(current, 0));
2939 position[1] = PyFloat_AsDouble(PyTuple_GetItem(current, 1));
2940 out.push_back(position);
2951 PyObject *res = PyObject_CallObject(
2956 throw std::runtime_error(
"Call to tight_layout() failed.");
2965template <
typename T>
2967 std::remove_pointer<std::remove_reference<T>>>::type;
2979 struct Derived : T, Fallback {};
2981 template <
typename U, U>
struct Check;
2983 template <
typename U>
2984 static std::true_type
2988 template <
typename U>
2989 static std::false_type
test(Check<
void (Fallback::*)(), &U::operator()> *);
2992 typedef decltype(test<Derived>(
nullptr))
type;
2993 typedef decltype(&Fallback::operator())
dtype;
2994 static constexpr bool value = type::value;
3006 template <
typename IterableX,
typename IterableY>
3008 const std::string &format) {
3013 using std::distance;
3016 auto xs = distance(begin(x), end(x));
3017 auto ys = distance(begin(y), end(y));
3018 assert(xs == ys &&
"x and y data must have the same number of elements!");
3020 PyObject *xlist = PyList_New(xs);
3021 PyObject *ylist = PyList_New(ys);
3022 PyObject *pystring = PyString_FromString(format.c_str());
3024 auto itx = begin(x), ity = begin(y);
3025 for (
size_t i = 0; i < xs; ++i) {
3026 PyList_SetItem(xlist, i, PyFloat_FromDouble(*itx++));
3027 PyList_SetItem(ylist, i, PyFloat_FromDouble(*ity++));
3030 PyObject *plot_args = PyTuple_New(3);
3031 PyTuple_SetItem(plot_args, 0, xlist);
3032 PyTuple_SetItem(plot_args, 1, ylist);
3033 PyTuple_SetItem(plot_args, 2, pystring);
3035 PyObject *res = PyObject_CallObject(
3038 Py_DECREF(plot_args);
3047 template <
typename Iterable,
typename Callable>
3049 const std::string &format) {
3050 if (begin(ticks) == end(ticks))
3055 std::vector<double> y;
3056 for (
auto x : ticks)
3065template <
typename... Args>
bool plot() {
return true; }
3067template <
typename A,
typename B,
typename... Args>
3068bool plot(
const A &a,
const B &b,
const std::string &format, Args... args) {
3078inline bool plot(
const std::vector<double> &x,
const std::vector<double> &y,
3079 const std::string &format =
"") {
3080 return plot<double, double>(x, y, format);
3083inline bool plot(
const std::vector<double> &y,
const std::string &format =
"") {
3084 return plot<double>(y, format);
3087inline bool plot(
const std::vector<double> &x,
const std::vector<double> &y,
3088 const std::map<std::string, std::string> &keywords) {
3089 return plot<double>(x, y, keywords);
3099 template <
typename Numeric>
3100 Plot(
const std::string &name,
const std::vector<Numeric> &x,
3101 const std::vector<Numeric> &y,
const std::string &format =
"") {
3104 assert(x.size() == y.size());
3106 PyObject *kwargs = PyDict_New();
3108 PyDict_SetItemString(kwargs,
"label", PyString_FromString(name.c_str()));
3113 PyObject *pystring = PyString_FromString(format.c_str());
3115 PyObject *plot_args = PyTuple_New(3);
3116 PyTuple_SetItem(plot_args, 0, xarray);
3117 PyTuple_SetItem(plot_args, 1, yarray);
3118 PyTuple_SetItem(plot_args, 2, pystring);
3120 PyObject *res = PyObject_Call(
3124 Py_DECREF(plot_args);
3127 line = PyList_GetItem(res, 0);
3139 Plot(
const std::string &name =
"",
const std::string &format =
"")
3140 :
Plot(name,
std::vector<double>(),
std::vector<double>(), format) {}
3142 template <
typename Numeric>
3143 bool update(
const std::vector<Numeric> &x,
const std::vector<Numeric> &y) {
3144 assert(x.size() == y.size());
3149 PyObject *plot_args = PyTuple_New(2);
3150 PyTuple_SetItem(plot_args, 0, xarray);
3151 PyTuple_SetItem(plot_args, 1, yarray);
3153 PyObject *res = PyObject_CallObject(
set_data_fct, plot_args);
3162 bool clear() {
return update(std::vector<double>(), std::vector<double>()); }
3167 auto remove_fct = PyObject_GetAttrString(
line,
"remove");
3168 PyObject *args = PyTuple_New(0);
3169 PyObject *res = PyObject_CallObject(remove_fct, args);
Definition matplotlibcpp.h:3096
PyObject * set_data_fct
Definition matplotlibcpp.h:3187
bool clear()
Definition matplotlibcpp.h:3162
Plot(const std::string &name, const std::vector< Numeric > &x, const std::vector< Numeric > &y, const std::string &format="")
Definition matplotlibcpp.h:3100
~Plot()
Definition matplotlibcpp.h:3176
Plot(const std::string &name="", const std::string &format="")
Definition matplotlibcpp.h:3139
void decref()
Definition matplotlibcpp.h:3179
bool update(const std::vector< Numeric > &x, const std::vector< Numeric > &y)
Definition matplotlibcpp.h:3143
void remove()
Definition matplotlibcpp.h:3165
PyObject * line
Definition matplotlibcpp.h:3186
typename std::is_function< std::remove_pointer< std::remove_reference< T > > >::type is_function
Definition matplotlibcpp.h:2967
PyObject * get_listlist(const std::vector< std::vector< Numeric > > &ll)
Definition matplotlibcpp.h:452
PyObject * get_array(const std::vector< Numeric > &v)
Definition matplotlibcpp.h:387
is_function< T > type
Definition matplotlibcpp.h:2972
is_callable_impl< std::is_class< T >::value, T >::type type
Definition matplotlibcpp.h:3000
PyObject * get_2darray(const std::vector<::std::vector< Numeric > > &v)
Definition matplotlibcpp.h:407
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)
Definition matplotlibcpp.h:977
static std::string s_backend
Definition matplotlibcpp.h:45
Definition matplotlibcpp.h:2997
Definition matplotlibcpp.h:2969
Definition matplotlibcpp.h:3003
Definition matplotlibcpp.h:42
std::vector< std::array< double, 2 > > ginput(const int numClicks=1, const std::map< std::string, std::string > &keywords={})
Definition matplotlibcpp.h:2909
bool fill(const std::vector< Numeric > &x, const std::vector< Numeric > &y, const std::map< std::string, std::string > &keywords)
Definition matplotlibcpp.h:840
void contour(const std::vector<::std::vector< Numeric > > &x, const std::vector<::std::vector< Numeric > > &y, const std::vector<::std::vector< Numeric > > &z, const std::map< std::string, std::string > &keywords={})
Definition matplotlibcpp.h:625
void plot3(const std::vector< Numeric > &x, const std::vector< Numeric > &y, const std::vector< Numeric > &z, const std::map< std::string, std::string > &keywords=std::map< std::string, std::string >(), const long fig_number=0)
Definition matplotlibcpp.h:699
std::array< double, 2 > xlim()
Definition matplotlibcpp.h:2215
void tight_layout()
Definition matplotlibcpp.h:2948
bool scatter(const std::vector< NumericX > &x, const std::vector< NumericY > &y, const double s=1.0, const std::map< std::string, std::string > &keywords={})
Definition matplotlibcpp.h:1062
bool named_hist(std::string label, const std::vector< Numeric > &y, long bins=10, std::string color="b", double alpha=1.0)
Definition matplotlibcpp.h:1414
void rcparams(const std::map< std::string, std::string > &keywords={})
Definition matplotlibcpp.h:2845
void set_aspect_equal()
Definition matplotlibcpp.h:2147
long figure(long number=-1)
Definition matplotlibcpp.h:2010
bool scatter_colored(const std::vector< NumericX > &x, const std::vector< NumericY > &y, const std::vector< NumericColors > &colors, const double s=1.0, const std::map< std::string, std::string > &keywords={})
Definition matplotlibcpp.h:1095
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={})
Definition matplotlibcpp.h:1750
void margins(Numeric margin)
Definition matplotlibcpp.h:2351
bool annotate(std::string annotation, double x, double y)
Definition matplotlibcpp.h:308
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)
Definition matplotlibcpp.h:1014
bool semilogy(const std::vector< NumericX > &x, const std::vector< NumericY > &y, const std::string &s="")
Definition matplotlibcpp.h:1696
bool named_semilogy(const std::string &name, const std::vector< NumericX > &x, const std::vector< NumericY > &y, const std::string &format="")
Definition matplotlibcpp.h:1880
bool arrow(Numeric x, Numeric y, Numeric end_x, Numeric end_y, const std::string &fc="r", const std::string ec="k", Numeric head_length=0.25, Numeric head_width=0.1625)
Definition matplotlibcpp.h:914
void close()
Definition matplotlibcpp.h:2763
bool fignum_exists(long number)
Definition matplotlibcpp.h:2045
bool named_plot(const std::string &name, const std::vector< Numeric > &y, const std::string &format="")
Definition matplotlibcpp.h:1791
void tick_params(const std::map< std::string, std::string > &keywords, const std::string axis="both")
Definition matplotlibcpp.h:2381
void suptitle(const std::string &suptitlestr, const std::map< std::string, std::string > &keywords={})
Definition matplotlibcpp.h:2480
bool loglog(const std::vector< NumericX > &x, const std::vector< NumericY > &y, const std::string &s="")
Definition matplotlibcpp.h:1723
void title(const std::string &titlestr, const std::map< std::string, std::string > &keywords={})
Definition matplotlibcpp.h:2456
void axvline(double x, double ymin=0., double ymax=1., const std::map< std::string, std::string > &keywords=std::map< std::string, std::string >())
Definition matplotlibcpp.h:2549
bool bar(const std::vector< Numeric > &x, const std::vector< Numeric > &y, std::string ec="black", std::string ls="-", double lw=1.0, const std::map< std::string, std::string > &keywords={})
Definition matplotlibcpp.h:1303
void subplot(long nrows, long ncols, long plot_number)
Definition matplotlibcpp.h:2409
std::array< double, 2 > ylim()
Definition matplotlibcpp.h:2230
void backend(const std::string &name)
Definition matplotlibcpp.h:306
bool subplots_adjust(const std::map< std::string, double > &keywords={})
Definition matplotlibcpp.h:1389
bool plot()
Definition matplotlibcpp.h:3065
void legend()
Definition matplotlibcpp.h:2085
void figure_size(size_t w, size_t h)
Definition matplotlibcpp.h:2062
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)
Definition matplotlibcpp.h:875
void xlabel(const std::string &str, const std::map< std::string, std::string > &keywords={})
Definition matplotlibcpp.h:2610
bool named_loglog(const std::string &name, const std::vector< NumericX > &x, const std::vector< NumericY > &y, const std::string &format="")
Definition matplotlibcpp.h:1911
void ion()
Definition matplotlibcpp.h:2895
void colorbar(PyObject *mappable=NULL, const std::map< std::string, float > &keywords={})
Definition matplotlibcpp.h:1982
bool boxplot(const std::vector< std::vector< Numeric > > &data, const std::vector< std::string > &labels={}, const std::map< std::string, std::string > &keywords={})
Definition matplotlibcpp.h:1241
void cla()
Definition matplotlibcpp.h:2882
void pause(Numeric interval)
Definition matplotlibcpp.h:2806
void axvspan(double xmin, double xmax, double ymin=0., double ymax=1., const std::map< std::string, std::string > &keywords=std::map< std::string, std::string >())
Definition matplotlibcpp.h:2578
void spy(const std::vector<::std::vector< Numeric > > &x, const double markersize=-1, const std::map< std::string, std::string > &keywords={})
Definition matplotlibcpp.h:668
void plot_surface(const std::vector<::std::vector< Numeric > > &x, const std::vector<::std::vector< Numeric > > &y, const std::vector<::std::vector< Numeric > > &z, const std::map< std::string, std::string > &keywords=std::map< std::string, std::string >(), const long fig_number=0)
Definition matplotlibcpp.h:504
void axis(const std::string &axisstr)
Definition matplotlibcpp.h:2504
void axhline(double y, double xmin=0., double xmax=1., const std::map< std::string, std::string > &keywords=std::map< std::string, std::string >())
Definition matplotlibcpp.h:2520
void yticks(const std::vector< Numeric > &ticks, const std::vector< std::string > &labels={}, const std::map< std::string, std::string > &keywords={})
Definition matplotlibcpp.h:2299
void subplot2grid(long nrows, long ncols, long rowid=0, long colid=0, long rowspan=1, long colspan=1)
Definition matplotlibcpp.h:2427
void set_zlabel(const std::string &str, const std::map< std::string, std::string > &keywords={})
Definition matplotlibcpp.h:2659
void set_aspect(Numeric ratio)
Definition matplotlibcpp.h:2118
bool barh(const std::vector< Numeric > &x, const std::vector< Numeric > &y, std::string ec="black", std::string ls="-", double lw=1.0, const std::map< std::string, std::string > &keywords={})
Definition matplotlibcpp.h:1355
void show(const bool block=true)
Definition matplotlibcpp.h:2740
void xkcd()
Definition matplotlibcpp.h:2776
void xticks(const std::vector< Numeric > &ticks, const std::vector< std::string > &labels={}, const std::map< std::string, std::string > &keywords={})
Definition matplotlibcpp.h:2246
void draw()
Definition matplotlibcpp.h:2793
void save(const std::string &filename, const int dpi=0)
Definition matplotlibcpp.h:2821
bool semilogx(const std::vector< NumericX > &x, const std::vector< NumericY > &y, const std::string &s="")
Definition matplotlibcpp.h:1669
void text(Numeric x, Numeric y, const std::string &s="")
Definition matplotlibcpp.h:1965
void ylabel(const std::string &str, const std::map< std::string, std::string > &keywords={})
Definition matplotlibcpp.h:2634
void clf()
Definition matplotlibcpp.h:2869
void grid(bool flag)
Definition matplotlibcpp.h:2722
bool named_semilogx(const std::string &name, const std::vector< NumericX > &x, const std::vector< NumericY > &y, const std::string &format="")
Definition matplotlibcpp.h:1849
bool stem(const std::vector< Numeric > &x, const std::vector< Numeric > &y, const std::map< std::string, std::string > &keywords)
Definition matplotlibcpp.h:805
bool hist(const std::vector< Numeric > &y, long bins=10, std::string color="b", double alpha=1.0, bool cumulative=false)
Definition matplotlibcpp.h:946
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={})
Definition matplotlibcpp.h:1503
Definition matplotlibcpp.h:47
PyObject * s_python_function_axhline
Definition matplotlibcpp.h:77
PyObject * s_python_function_clf
Definition matplotlibcpp.h:89
PyObject * s_python_function_xlim
Definition matplotlibcpp.h:71
PyObject * s_python_function_errorbar
Definition matplotlibcpp.h:90
PyObject * s_python_function_axvline
Definition matplotlibcpp.h:78
PyObject * s_python_function_arrow
Definition matplotlibcpp.h:48
PyObject * s_python_function_subplot2grid
Definition matplotlibcpp.h:69
PyObject * s_python_function_barh
Definition matplotlibcpp.h:100
PyObject * s_python_function_hist
Definition matplotlibcpp.h:64
PyObject * s_python_function_suptitle
Definition matplotlibcpp.h:98
PyObject * s_python_function_boxplot
Definition matplotlibcpp.h:67
PyObject * s_python_function_xlabel
Definition matplotlibcpp.h:80
PyObject * s_python_function_spy
Definition matplotlibcpp.h:104
PyObject * s_python_function_legend
Definition matplotlibcpp.h:70
PyObject * s_python_function_margins
Definition matplotlibcpp.h:85
PyObject * s_python_function_ginput
Definition matplotlibcpp.h:73
static _interpreter & interkeeper(bool should_kill)
Definition matplotlibcpp.h:124
PyObject * s_python_function_stem
Definition matplotlibcpp.h:95
PyObject * s_python_function_grid
Definition matplotlibcpp.h:87
PyObject * s_python_function_semilogy
Definition matplotlibcpp.h:60
PyObject * s_python_function_draw
Definition matplotlibcpp.h:51
PyObject * s_python_function_fignum_exists
Definition matplotlibcpp.h:55
PyObject * s_python_function_xticks
Definition matplotlibcpp.h:83
PyObject * s_python_function_contour
Definition matplotlibcpp.h:58
PyObject * s_python_function_close
Definition matplotlibcpp.h:50
static _interpreter & get()
Definition matplotlibcpp.h:119
PyObject * s_python_function_axis
Definition matplotlibcpp.h:76
PyObject * s_python_function_plot
Definition matplotlibcpp.h:56
PyObject * s_python_function_rcparams
Definition matplotlibcpp.h:103
_interpreter()
Definition matplotlibcpp.h:163
PyObject * s_python_function_figure
Definition matplotlibcpp.h:54
PyObject * s_python_function_gca
Definition matplotlibcpp.h:82
PyObject * s_python_function_axvspan
Definition matplotlibcpp.h:79
PyObject * s_python_function_loglog
Definition matplotlibcpp.h:61
PyObject * s_python_function_tight_layout
Definition matplotlibcpp.h:92
PyObject * s_python_function_save
Definition matplotlibcpp.h:53
PyObject * s_python_function_title
Definition matplotlibcpp.h:75
PyObject * s_python_function_fill
Definition matplotlibcpp.h:62
PyObject * s_python_function_show
Definition matplotlibcpp.h:49
PyObject * s_python_function_quiver
Definition matplotlibcpp.h:57
PyObject * safe_import(PyObject *module, std::string fname)
Definition matplotlibcpp.h:131
static _interpreter & kill()
Definition matplotlibcpp.h:121
PyObject * s_python_function_ylabel
Definition matplotlibcpp.h:81
PyObject * s_python_empty_tuple
Definition matplotlibcpp.h:94
PyObject * s_python_function_cla
Definition matplotlibcpp.h:88
~_interpreter()
Definition matplotlibcpp.h:291
PyObject * s_python_function_semilogx
Definition matplotlibcpp.h:59
PyObject * s_python_function_colorbar
Definition matplotlibcpp.h:101
PyObject * s_python_function_imshow
Definition matplotlibcpp.h:65
PyObject * s_python_function_bar
Definition matplotlibcpp.h:99
PyObject * s_python_function_subplot
Definition matplotlibcpp.h:68
PyObject * s_python_function_xkcd
Definition matplotlibcpp.h:96
PyObject * s_python_function_ion
Definition matplotlibcpp.h:72
void import_numpy()
Definition matplotlibcpp.h:156
PyObject * s_python_colormap
Definition matplotlibcpp.h:93
PyObject * s_python_function_pause
Definition matplotlibcpp.h:52
PyObject * s_python_function_yticks
Definition matplotlibcpp.h:84
PyObject * s_python_function_subplots_adjust
Definition matplotlibcpp.h:102
PyObject * s_python_function_scatter
Definition matplotlibcpp.h:66
PyObject * s_python_function_annotate
Definition matplotlibcpp.h:91
PyObject * s_python_function_fill_between
Definition matplotlibcpp.h:63
PyObject * s_python_function_ylim
Definition matplotlibcpp.h:74
PyObject * s_python_function_tick_params
Definition matplotlibcpp.h:86
PyObject * s_python_function_text
Definition matplotlibcpp.h:97
static std::true_type test(...)
decltype(test< Derived >(nullptr)) type
Definition matplotlibcpp.h:2992
decltype(&Fallback::operator()) dtype
Definition matplotlibcpp.h:2993
static std::false_type test(Check< void(Fallback::*)(), &U::operator()> *)
bool operator()(const IterableX &x, const IterableY &y, const std::string &format)
Definition matplotlibcpp.h:3007
bool operator()(const Iterable &ticks, const Callable &f, const std::string &format)
Definition matplotlibcpp.h:3048
Definition matplotlibcpp.h:339
static const NPY_TYPES type
Definition matplotlibcpp.h:340