FML
Loading...
Searching...
No Matches
FML::UTILS::Matplotlib Namespace Reference

Make plots with matplotlib from within C++. More...

Data Structures

class  Plot
 
struct  select_npy_type
 
struct  select_npy_type< bool >
 
struct  select_npy_type< double >
 
struct  select_npy_type< float >
 
struct  select_npy_type< int16_t >
 
struct  select_npy_type< int32_t >
 
struct  select_npy_type< int64_t >
 
struct  select_npy_type< int8_t >
 
struct  select_npy_type< uint16_t >
 
struct  select_npy_type< uint32_t >
 
struct  select_npy_type< uint64_t >
 
struct  select_npy_type< uint8_t >
 

Functions

void backend (const std::string &name)
 
bool annotate (std::string annotation, double x, double y)
 
template<typename Numeric >
PyObject * get_array (const std::vector< Numeric > &v)
 
template<typename Numeric >
PyObject * get_2darray (const std::vector<::std::vector< Numeric > > &v)
 
template<typename Numeric >
bool plot (const std::vector< Numeric > &x, const std::vector< Numeric > &y, const std::map< std::string, std::string > &keywords)
 
template<typename Numeric >
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 >())
 
template<typename Numeric >
bool stem (const std::vector< Numeric > &x, const std::vector< Numeric > &y, const std::map< std::string, std::string > &keywords)
 
template<typename Numeric >
bool fill (const std::vector< Numeric > &x, const std::vector< Numeric > &y, const std::map< std::string, std::string > &keywords)
 
template<typename Numeric >
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)
 
template<typename Numeric >
bool hist (const std::vector< Numeric > &y, long bins=10, std::string color="b", double alpha=1.0, bool cumulative=false)
 
void imshow (const unsigned char *ptr, const int rows, const int columns, const int colors, const std::map< std::string, std::string > &keywords={})
 
void imshow (const float *ptr, const int rows, const int columns, const int colors, const std::map< std::string, std::string > &keywords={})
 
void imshow (const cv::Mat &image, const std::map< std::string, std::string > &keywords={})
 
template<typename NumericX , typename NumericY >
bool scatter (const std::vector< NumericX > &x, const std::vector< NumericY > &y, const double s=1.0, const std::unordered_map< std::string, std::string > &keywords={})
 
template<typename Numeric >
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={})
 
template<typename Numeric >
bool bar (const std::vector< Numeric > &y, std::string ec="black", std::string ls="-", double lw=1.0, const std::map< std::string, std::string > &keywords={})
 
bool subplots_adjust (const std::map< std::string, double > &keywords={})
 
template<typename Numeric >
bool named_hist (std::string label, const std::vector< Numeric > &y, long bins=10, std::string color="b", double alpha=1.0)
 
template<typename NumericX , typename NumericY >
bool plot (const std::vector< NumericX > &x, const std::vector< NumericY > &y, const std::string &s="")
 
template<typename NumericX , typename NumericY , typename NumericU , typename NumericW >
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={})
 
template<typename NumericX , typename NumericY >
bool stem (const std::vector< NumericX > &x, const std::vector< NumericY > &y, const std::string &s="")
 
template<typename NumericX , typename NumericY >
bool semilogx (const std::vector< NumericX > &x, const std::vector< NumericY > &y, const std::string &s="")
 
template<typename NumericX , typename NumericY >
bool semilogy (const std::vector< NumericX > &x, const std::vector< NumericY > &y, const std::string &s="")
 
template<typename NumericX , typename NumericY >
bool loglog (const std::vector< NumericX > &x, const std::vector< NumericY > &y, const std::string &s="")
 
template<typename NumericX , typename NumericY >
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={})
 
template<typename Numeric >
bool named_plot (const std::string &name, const std::vector< Numeric > &y, const std::string &format="")
 
template<typename Numeric >
bool named_plot (const std::string &name, const std::vector< Numeric > &x, const std::vector< Numeric > &y, const std::string &format="")
 
template<typename Numeric >
bool named_semilogx (const std::string &name, const std::vector< Numeric > &x, const std::vector< Numeric > &y, const std::string &format="")
 
template<typename Numeric >
bool named_semilogy (const std::string &name, const std::vector< Numeric > &x, const std::vector< Numeric > &y, const std::string &format="")
 
template<typename Numeric >
bool named_loglog (const std::string &name, const std::vector< Numeric > &x, const std::vector< Numeric > &y, const std::string &format="")
 
template<typename Numeric >
bool plot (const std::vector< Numeric > &y, const std::string &format="")
 
template<typename Numeric >
bool plot (const std::vector< Numeric > &y, const std::map< std::string, std::string > &keywords)
 
template<typename Numeric >
bool stem (const std::vector< Numeric > &y, const std::string &format="")
 
template<typename Numeric >
void text (Numeric x, Numeric y, const std::string &s="")
 
long figure (long number=-1)
 
bool fignum_exists (long number)
 
void figure_size (size_t w, size_t h)
 
void legend ()
 
template<typename Numeric >
void ylim (Numeric left, Numeric right)
 
template<typename Numeric >
void xlim (Numeric left, Numeric right)
 
double * xlim ()
 
double * ylim ()
 
template<typename Numeric >
void xticks (const std::vector< Numeric > &ticks, const std::vector< std::string > &labels={}, const std::map< std::string, std::string > &keywords={})
 
template<typename Numeric >
void xticks (const std::vector< Numeric > &ticks, const std::map< std::string, std::string > &keywords)
 
template<typename Numeric >
void yticks (const std::vector< Numeric > &ticks, const std::vector< std::string > &labels={}, const std::map< std::string, std::string > &keywords={})
 
template<typename Numeric >
void yticks (const std::vector< Numeric > &ticks, const std::map< std::string, std::string > &keywords)
 
void tick_params (const std::map< std::string, std::string > &keywords, const std::string axis="both")
 
void subplot (long nrows, long ncols, long plot_number)
 
void subplot2grid (long nrows, long ncols, long rowid=0, long colid=0, long rowspan=1, long colspan=1)
 
void title (const std::string &titlestr, const std::map< std::string, std::string > &keywords={})
 
void suptitle (const std::string &suptitlestr, const std::map< std::string, std::string > &keywords={})
 
void axis (const std::string &axisstr)
 
void xscale (const std::string &xscale)
 
void yscale (const std::string &yscale)
 
void xlabel (const std::string &str, const std::map< std::string, std::string > &keywords={})
 
void ylabel (const std::string &str, const std::map< std::string, std::string > &keywords={})
 
void grid (bool flag)
 
void show (const bool block=true)
 
void close ()
 
void xkcd ()
 
void draw ()
 
template<typename Numeric >
void pause (Numeric interval)
 
void save (const std::string &filename)
 
void clf ()
 
void ion ()
 
std::vector< std::array< double, 2 > > ginput (const int numClicks=1, const std::map< std::string, std::string > &keywords={})
 
void tight_layout ()
 
template<typename... Args>
bool plot ()
 
template<typename A , typename B , typename... Args>
bool plot (const A &a, const B &b, const std::string &format, Args... args)
 
bool plot (const std::vector< double > &x, const std::vector< double > &y, const std::string &format="")
 
bool plot (const std::vector< double > &y, const std::string &format="")
 
bool plot (const std::vector< double > &x, const std::vector< double > &y, const std::map< std::string, std::string > &keywords)
 

Detailed Description

Make plots with matplotlib from within C++.

Function Documentation

◆ annotate()

bool FML::UTILS::Matplotlib::annotate ( std::string annotation,
double x,
double y )
inline

Definition at line 274 of file Matplotlibcpp.h.

274 {
275 detail::_interpreter::get(); // the interpreter must be initialized before calling Python
276 PyObject * xy = PyTuple_New(2);
277 PyObject * str = PyString_FromString(annotation.c_str());
278
279 PyTuple_SetItem(xy, 0, PyFloat_FromDouble(x));
280 PyTuple_SetItem(xy, 1, PyFloat_FromDouble(y));
281
282 PyObject * kwargs = PyDict_New();
283 PyDict_SetItemString(kwargs, "xy", xy);
284
285 PyObject * args = PyTuple_New(1);
286 PyTuple_SetItem(args, 0, str);
287
288 PyObject * res = PyObject_Call(detail::_interpreter::get().s_python_function_annotate, args, kwargs);
289
290 Py_DECREF(args);
291 Py_DECREF(kwargs);
292
293 if (res)
294 Py_DECREF(res);
295
296 return res;
297 }

◆ axis()

void FML::UTILS::Matplotlib::axis ( const std::string & axisstr)
inline

Definition at line 1614 of file Matplotlibcpp.h.

1614 {
1615 detail::_interpreter::get(); // the interpreter must be initialized before calling Python
1616 PyObject * str = PyString_FromString(axisstr.c_str());
1617 PyObject * args = PyTuple_New(1);
1618 PyTuple_SetItem(args, 0, str);
1619
1620 PyObject * res = PyObject_CallObject(detail::_interpreter::get().s_python_function_axis, args);
1621 if (!res)
1622 throw std::runtime_error("Call to title() failed.");
1623
1624 Py_DECREF(args);
1625 Py_DECREF(res);
1626 }

◆ backend()

void FML::UTILS::Matplotlib::backend ( const std::string & name)
inline

Definition at line 272 of file Matplotlibcpp.h.

272{ detail::s_backend = name; }
std::string name

◆ bar() [1/2]

template<typename Numeric >
bool FML::UTILS::Matplotlib::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 at line 784 of file Matplotlibcpp.h.

789 {}) {
790 PyObject * xarray = get_array(x);
791 PyObject * yarray = get_array(y);
792
793 PyObject * kwargs = PyDict_New();
794
795 PyDict_SetItemString(kwargs, "ec", PyString_FromString(ec.c_str()));
796 PyDict_SetItemString(kwargs, "ls", PyString_FromString(ls.c_str()));
797 PyDict_SetItemString(kwargs, "lw", PyFloat_FromDouble(lw));
798
799 for (std::map<std::string, std::string>::const_iterator it = keywords.begin(); it != keywords.end();
800 ++it) {
801 PyDict_SetItemString(kwargs, it->first.c_str(), PyUnicode_FromString(it->second.c_str()));
802 }
803
804 PyObject * plot_args = PyTuple_New(2);
805 PyTuple_SetItem(plot_args, 0, xarray);
806 PyTuple_SetItem(plot_args, 1, yarray);
807
808 PyObject * res = PyObject_Call(detail::_interpreter::get().s_python_function_bar, plot_args, kwargs);
809
810 Py_DECREF(plot_args);
811 Py_DECREF(kwargs);
812 if (res)
813 Py_DECREF(res);
814
815 return res;
816 }

◆ bar() [2/2]

template<typename Numeric >
bool FML::UTILS::Matplotlib::bar ( 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 at line 819 of file Matplotlibcpp.h.

823 {}) {
824 using T = typename std::remove_reference<decltype(y)>::type::value_type;
825
826 std::vector<T> x;
827 for (std::size_t i = 0; i < y.size(); i++) {
828 x.push_back(i);
829 }
830
831 return bar(x, y, ec, ls, lw, keywords);
832 }

◆ clf()

void FML::UTILS::Matplotlib::clf ( )
inline

Definition at line 1798 of file Matplotlibcpp.h.

1798 {
1799 PyObject * res = PyObject_CallObject(detail::_interpreter::get().s_python_function_clf,
1800 detail::_interpreter::get().s_python_empty_tuple);
1801
1802 if (!res)
1803 throw std::runtime_error("Call to clf() failed.");
1804
1805 Py_DECREF(res);
1806 }

◆ close()

void FML::UTILS::Matplotlib::close ( )
inline

Definition at line 1732 of file Matplotlibcpp.h.

1732 {
1733 PyObject * res = PyObject_CallObject(detail::_interpreter::get().s_python_function_close,
1734 detail::_interpreter::get().s_python_empty_tuple);
1735
1736 if (!res)
1737 throw std::runtime_error("Call to close() failed.");
1738
1739 Py_DECREF(res);
1740 }

◆ draw()

void FML::UTILS::Matplotlib::draw ( )
inline

Definition at line 1759 of file Matplotlibcpp.h.

1759 {
1760 PyObject * res = PyObject_CallObject(detail::_interpreter::get().s_python_function_draw,
1761 detail::_interpreter::get().s_python_empty_tuple);
1762
1763 if (!res)
1764 throw std::runtime_error("Call to draw() failed.");
1765
1766 Py_DECREF(res);
1767 }

◆ errorbar()

template<typename NumericX , typename NumericY >
bool FML::UTILS::Matplotlib::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 at line 1035 of file Matplotlibcpp.h.

1038 {}) {
1039 assert(x.size() == y.size());
1040
1041 PyObject * xarray = get_array(x);
1042 PyObject * yarray = get_array(y);
1043 PyObject * yerrarray = get_array(yerr);
1044
1045 // construct keyword args
1046 PyObject * kwargs = PyDict_New();
1047 for (std::map<std::string, std::string>::const_iterator it = keywords.begin(); it != keywords.end();
1048 ++it) {
1049 PyDict_SetItemString(kwargs, it->first.c_str(), PyString_FromString(it->second.c_str()));
1050 }
1051
1052 PyDict_SetItemString(kwargs, "yerr", yerrarray);
1053
1054 PyObject * plot_args = PyTuple_New(2);
1055 PyTuple_SetItem(plot_args, 0, xarray);
1056 PyTuple_SetItem(plot_args, 1, yarray);
1057
1058 PyObject * res =
1059 PyObject_Call(detail::_interpreter::get().s_python_function_errorbar, plot_args, kwargs);
1060
1061 Py_DECREF(kwargs);
1062 Py_DECREF(plot_args);
1063
1064 if (res)
1065 Py_DECREF(res);
1066 else
1067 throw std::runtime_error("Call to errorbar() failed.");
1068
1069 return res;
1070 }

◆ fignum_exists()

bool FML::UTILS::Matplotlib::fignum_exists ( long number)
inline

Definition at line 1285 of file Matplotlibcpp.h.

1285 {
1286 // Make sure interpreter is initialised
1287 detail::_interpreter::get();
1288
1289 PyObject * args = PyTuple_New(1);
1290 PyTuple_SetItem(args, 0, PyLong_FromLong(number));
1291 PyObject * res = PyObject_CallObject(detail::_interpreter::get().s_python_function_fignum_exists, args);
1292 if (!res)
1293 throw std::runtime_error("Call to fignum_exists() failed.");
1294
1295 bool ret = PyObject_IsTrue(res);
1296 Py_DECREF(res);
1297 Py_DECREF(args);
1298
1299 return ret;
1300 }

◆ figure()

long FML::UTILS::Matplotlib::figure ( long number = -1)
inline

Definition at line 1254 of file Matplotlibcpp.h.

1254 {
1255 PyObject * res;
1256 if (number == -1)
1257 res = PyObject_CallObject(detail::_interpreter::get().s_python_function_figure,
1258 detail::_interpreter::get().s_python_empty_tuple);
1259 else {
1260 assert(number > 0);
1261
1262 // Make sure interpreter is initialised
1263 detail::_interpreter::get();
1264
1265 PyObject * args = PyTuple_New(1);
1266 PyTuple_SetItem(args, 0, PyLong_FromLong(number));
1267 res = PyObject_CallObject(detail::_interpreter::get().s_python_function_figure, args);
1268 Py_DECREF(args);
1269 }
1270
1271 if (!res)
1272 throw std::runtime_error("Call to figure() failed.");
1273
1274 PyObject * num = PyObject_GetAttrString(res, "number");
1275 if (!num)
1276 throw std::runtime_error("Could not get number attribute of figure object");
1277 const long figureNumber = PyLong_AsLong(num);
1278
1279 Py_DECREF(num);
1280 Py_DECREF(res);
1281
1282 return figureNumber;
1283 }
std::string num(double x)
Definition test_camb.cpp:34

◆ figure_size()

void FML::UTILS::Matplotlib::figure_size ( size_t w,
size_t h )
inline

Definition at line 1302 of file Matplotlibcpp.h.

1302 {
1303 // Make sure interpreter is initialised
1304 detail::_interpreter::get();
1305
1306 const size_t dpi = 100;
1307 PyObject * size = PyTuple_New(2);
1308 PyTuple_SetItem(size, 0, PyFloat_FromDouble((double)w / dpi));
1309 PyTuple_SetItem(size, 1, PyFloat_FromDouble((double)h / dpi));
1310
1311 PyObject * kwargs = PyDict_New();
1312 PyDict_SetItemString(kwargs, "figsize", size);
1313 PyDict_SetItemString(kwargs, "dpi", PyLong_FromSize_t(dpi));
1314
1315 PyObject * res = PyObject_Call(detail::_interpreter::get().s_python_function_figure,
1316 detail::_interpreter::get().s_python_empty_tuple,
1317 kwargs);
1318
1319 Py_DECREF(kwargs);
1320
1321 if (!res)
1322 throw std::runtime_error("Call to figure_size() failed.");
1323 Py_DECREF(res);
1324 }
const double h
Definition Main.cpp:66

◆ fill()

template<typename Numeric >
bool FML::UTILS::Matplotlib::fill ( const std::vector< Numeric > & x,
const std::vector< Numeric > & y,
const std::map< std::string, std::string > & keywords )

Definition at line 574 of file Matplotlibcpp.h.

576 {
577 assert(x.size() == y.size());
578
579 // using numpy arrays
580 PyObject * xarray = get_array(x);
581 PyObject * yarray = get_array(y);
582
583 // construct positional args
584 PyObject * args = PyTuple_New(2);
585 PyTuple_SetItem(args, 0, xarray);
586 PyTuple_SetItem(args, 1, yarray);
587
588 // construct keyword args
589 PyObject * kwargs = PyDict_New();
590 for (auto it = keywords.begin(); it != keywords.end(); ++it) {
591 PyDict_SetItemString(kwargs, it->first.c_str(), PyUnicode_FromString(it->second.c_str()));
592 }
593
594 PyObject * res = PyObject_Call(detail::_interpreter::get().s_python_function_fill, args, kwargs);
595
596 Py_DECREF(args);
597 Py_DECREF(kwargs);
598
599 if (res)
600 Py_DECREF(res);
601
602 return res;
603 }
PyObject * get_array(const std::vector< Numeric > &v)

◆ fill_between()

template<typename Numeric >
bool FML::UTILS::Matplotlib::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 at line 606 of file Matplotlibcpp.h.

609 {
610 assert(x.size() == y1.size());
611 assert(x.size() == y2.size());
612
613 // using numpy arrays
614 PyObject * xarray = get_array(x);
615 PyObject * y1array = get_array(y1);
616 PyObject * y2array = get_array(y2);
617
618 // construct positional args
619 PyObject * args = PyTuple_New(3);
620 PyTuple_SetItem(args, 0, xarray);
621 PyTuple_SetItem(args, 1, y1array);
622 PyTuple_SetItem(args, 2, y2array);
623
624 // construct keyword args
625 PyObject * kwargs = PyDict_New();
626 for (std::map<std::string, std::string>::const_iterator it = keywords.begin(); it != keywords.end();
627 ++it) {
628 PyDict_SetItemString(kwargs, it->first.c_str(), PyUnicode_FromString(it->second.c_str()));
629 }
630
631 PyObject * res =
632 PyObject_Call(detail::_interpreter::get().s_python_function_fill_between, args, kwargs);
633
634 Py_DECREF(args);
635 Py_DECREF(kwargs);
636 if (res)
637 Py_DECREF(res);
638
639 return res;
640 }

◆ get_2darray()

template<typename Numeric >
PyObject * FML::UTILS::Matplotlib::get_2darray ( const std::vector<::std::vector< Numeric > > & v)

Definition at line 369 of file Matplotlibcpp.h.

369 {
370 detail::_interpreter::get(); // interpreter needs to be initialized for the
371 // numpy commands to work
372 if (v.size() < 1)
373 throw std::runtime_error("get_2d_array v too small");
374
375 npy_intp vsize[2] = {static_cast<npy_intp>(v.size()), static_cast<npy_intp>(v[0].size())};
376
377 PyArrayObject * varray = (PyArrayObject *)PyArray_SimpleNew(2, vsize, NPY_DOUBLE);
378
379 double * vd_begin = static_cast<double *>(PyArray_DATA(varray));
380
381 for (const ::std::vector<Numeric> & v_row : v) {
382 if (v_row.size() != static_cast<size_t>(vsize[1]))
383 throw std::runtime_error("Missmatched array size");
384 std::copy(v_row.begin(), v_row.end(), vd_begin);
385 vd_begin += vsize[1];
386 }
387
388 return reinterpret_cast<PyObject *>(varray);
389 }

◆ get_array()

template<typename Numeric >
PyObject * FML::UTILS::Matplotlib::get_array ( const std::vector< Numeric > & v)

Definition at line 351 of file Matplotlibcpp.h.

351 {
352 detail::_interpreter::get(); // interpreter needs to be initialized for the
353 // numpy commands to work
354 NPY_TYPES type = select_npy_type<Numeric>::type;
355 if (type == NPY_NOTYPE) {
356 std::vector<double> vd(v.size());
357 npy_intp vsize = v.size();
358 std::copy(v.begin(), v.end(), vd.begin());
359 PyObject * varray = PyArray_SimpleNewFromData(1, &vsize, NPY_DOUBLE, (void *)(vd.data()));
360 return varray;
361 }
362
363 npy_intp vsize = v.size();
364 PyObject * varray = PyArray_SimpleNewFromData(1, &vsize, type, (void *)(v.data()));
365 return varray;
366 }

◆ ginput()

std::vector< std::array< double, 2 > > FML::UTILS::Matplotlib::ginput ( const int numClicks = 1,
const std::map< std::string, std::string > & keywords = {} )
inline

Definition at line 1818 of file Matplotlibcpp.h.

1819 {}) {
1820 detail::_interpreter::get(); // the interpreter must be initialized before calling Python
1821 PyObject * args = PyTuple_New(1);
1822 PyTuple_SetItem(args, 0, PyLong_FromLong(numClicks));
1823
1824 // construct keyword args
1825 PyObject * kwargs = PyDict_New();
1826 for (std::map<std::string, std::string>::const_iterator it = keywords.begin(); it != keywords.end();
1827 ++it) {
1828 PyDict_SetItemString(kwargs, it->first.c_str(), PyUnicode_FromString(it->second.c_str()));
1829 }
1830
1831 PyObject * res = PyObject_Call(detail::_interpreter::get().s_python_function_ginput, args, kwargs);
1832
1833 Py_DECREF(kwargs);
1834 Py_DECREF(args);
1835 if (!res)
1836 throw std::runtime_error("Call to ginput() failed.");
1837
1838 const size_t len = PyList_Size(res);
1839 std::vector<std::array<double, 2>> out;
1840 out.reserve(len);
1841 for (size_t i = 0; i < len; i++) {
1842 PyObject * current = PyList_GetItem(res, i);
1843 std::array<double, 2> position;
1844 position[0] = PyFloat_AsDouble(PyTuple_GetItem(current, 0));
1845 position[1] = PyFloat_AsDouble(PyTuple_GetItem(current, 1));
1846 out.push_back(position);
1847 }
1848 Py_DECREF(res);
1849
1850 return out;
1851 }

◆ grid()

void FML::UTILS::Matplotlib::grid ( bool flag)
inline

Definition at line 1696 of file Matplotlibcpp.h.

1696 {
1697 detail::_interpreter::get(); // the interpreter must be initialized before calling Python
1698 PyObject * pyflag = flag ? Py_True : Py_False;
1699 Py_INCREF(pyflag);
1700
1701 PyObject * args = PyTuple_New(1);
1702 PyTuple_SetItem(args, 0, pyflag);
1703
1704 PyObject * res = PyObject_CallObject(detail::_interpreter::get().s_python_function_grid, args);
1705 if (!res)
1706 throw std::runtime_error("Call to grid() failed.");
1707
1708 Py_DECREF(args);
1709 Py_DECREF(res);
1710 }

◆ hist()

template<typename Numeric >
bool FML::UTILS::Matplotlib::hist ( const std::vector< Numeric > & y,
long bins = 10,
std::string color = "b",
double alpha = 1.0,
bool cumulative = false )

Definition at line 643 of file Matplotlibcpp.h.

647 {
648 PyObject * yarray = get_array(y);
649
650 PyObject * kwargs = PyDict_New();
651 PyDict_SetItemString(kwargs, "bins", PyLong_FromLong(bins));
652 PyDict_SetItemString(kwargs, "color", PyString_FromString(color.c_str()));
653 PyDict_SetItemString(kwargs, "alpha", PyFloat_FromDouble(alpha));
654 PyDict_SetItemString(kwargs, "cumulative", cumulative ? Py_True : Py_False);
655
656 PyObject * plot_args = PyTuple_New(1);
657
658 PyTuple_SetItem(plot_args, 0, yarray);
659
660 PyObject * res = PyObject_Call(detail::_interpreter::get().s_python_function_hist, plot_args, kwargs);
661
662 Py_DECREF(plot_args);
663 Py_DECREF(kwargs);
664 if (res)
665 Py_DECREF(res);
666
667 return res;
668 }
double alpha(V3 a, V3 b)

◆ imshow() [1/3]

void FML::UTILS::Matplotlib::imshow ( const cv::Mat & image,
const std::map< std::string, std::string > & keywords = {} )

Definition at line 722 of file Matplotlibcpp.h.

722 {}) {
723 // Convert underlying type of matrix, if needed
724 cv::Mat image2;
725 NPY_TYPES npy_type = NPY_UINT8;
726 switch (image.type() & CV_MAT_DEPTH_MASK) {
727 case CV_8U:
728 image2 = image;
729 break;
730 case CV_32F:
731 image2 = image;
732 npy_type = NPY_FLOAT;
733 break;
734 default:
735 image.convertTo(image2, CV_MAKETYPE(CV_8U, image.channels()));
736 }
737
738 // If color image, convert from BGR to RGB
739 switch (image2.channels()) {
740 case 3:
741 cv::cvtColor(image2, image2, CV_BGR2RGB);
742 break;
743 case 4:
744 cv::cvtColor(image2, image2, CV_BGRA2RGBA);
745 }
746
747 internal::imshow(image2.data, npy_type, image2.rows, image2.cols, image2.channels(), keywords);
748 }

◆ imshow() [2/3]

void FML::UTILS::Matplotlib::imshow ( const float * ptr,
const int rows,
const int columns,
const int colors,
const std::map< std::string, std::string > & keywords = {} )
inline

Definition at line 713 of file Matplotlibcpp.h.

717 {}) {
718 internal::imshow((void *)ptr, NPY_FLOAT, rows, columns, colors, keywords);
719 }
std::vector< std::vector< std::string > > rows

◆ imshow() [3/3]

void FML::UTILS::Matplotlib::imshow ( const unsigned char * ptr,
const int rows,
const int columns,
const int colors,
const std::map< std::string, std::string > & keywords = {} )
inline

Definition at line 705 of file Matplotlibcpp.h.

709 {}) {
710 internal::imshow((void *)ptr, NPY_UINT8, rows, columns, colors, keywords);
711 }

◆ ion()

void FML::UTILS::Matplotlib::ion ( )
inline

Definition at line 1808 of file Matplotlibcpp.h.

1808 {
1809 PyObject * res = PyObject_CallObject(detail::_interpreter::get().s_python_function_ion,
1810 detail::_interpreter::get().s_python_empty_tuple);
1811
1812 if (!res)
1813 throw std::runtime_error("Call to ion() failed.");
1814
1815 Py_DECREF(res);
1816 }

◆ legend()

void FML::UTILS::Matplotlib::legend ( )
inline

Definition at line 1326 of file Matplotlibcpp.h.

1326 {
1327 PyObject * res = PyObject_CallObject(detail::_interpreter::get().s_python_function_legend,
1328 detail::_interpreter::get().s_python_empty_tuple);
1329 if (!res)
1330 throw std::runtime_error("Call to legend() failed.");
1331
1332 Py_DECREF(res);
1333 }

◆ loglog()

template<typename NumericX , typename NumericY >
bool FML::UTILS::Matplotlib::loglog ( const std::vector< NumericX > & x,
const std::vector< NumericY > & y,
const std::string & s = "" )

Definition at line 1012 of file Matplotlibcpp.h.

1012 {
1013 assert(x.size() == y.size());
1014
1015 PyObject * xarray = get_array(x);
1016 PyObject * yarray = get_array(y);
1017
1018 PyObject * pystring = PyString_FromString(s.c_str());
1019
1020 PyObject * plot_args = PyTuple_New(3);
1021 PyTuple_SetItem(plot_args, 0, xarray);
1022 PyTuple_SetItem(plot_args, 1, yarray);
1023 PyTuple_SetItem(plot_args, 2, pystring);
1024
1025 PyObject * res = PyObject_CallObject(detail::_interpreter::get().s_python_function_loglog, plot_args);
1026
1027 Py_DECREF(plot_args);
1028 if (res)
1029 Py_DECREF(res);
1030
1031 return res;
1032 }

◆ named_hist()

template<typename Numeric >
bool FML::UTILS::Matplotlib::named_hist ( std::string label,
const std::vector< Numeric > & y,
long bins = 10,
std::string color = "b",
double alpha = 1.0 )

Definition at line 855 of file Matplotlibcpp.h.

859 {
860 PyObject * yarray = get_array(y);
861
862 PyObject * kwargs = PyDict_New();
863 PyDict_SetItemString(kwargs, "label", PyString_FromString(label.c_str()));
864 PyDict_SetItemString(kwargs, "bins", PyLong_FromLong(bins));
865 PyDict_SetItemString(kwargs, "color", PyString_FromString(color.c_str()));
866 PyDict_SetItemString(kwargs, "alpha", PyFloat_FromDouble(alpha));
867
868 PyObject * plot_args = PyTuple_New(1);
869 PyTuple_SetItem(plot_args, 0, yarray);
870
871 PyObject * res = PyObject_Call(detail::_interpreter::get().s_python_function_hist, plot_args, kwargs);
872
873 Py_DECREF(plot_args);
874 Py_DECREF(kwargs);
875 if (res)
876 Py_DECREF(res);
877
878 return res;
879 }

◆ named_loglog()

template<typename Numeric >
bool FML::UTILS::Matplotlib::named_loglog ( const std::string & name,
const std::vector< Numeric > & x,
const std::vector< Numeric > & y,
const std::string & format = "" )

Definition at line 1187 of file Matplotlibcpp.h.

1190 {
1191 detail::_interpreter::get(); // the interpreter must be initialized before calling Python
1192 PyObject * kwargs = PyDict_New();
1193 PyDict_SetItemString(kwargs, "label", PyString_FromString(name.c_str()));
1194
1195 PyObject * xarray = get_array(x);
1196 PyObject * yarray = get_array(y);
1197
1198 PyObject * pystring = PyString_FromString(format.c_str());
1199
1200 PyObject * plot_args = PyTuple_New(3);
1201 PyTuple_SetItem(plot_args, 0, xarray);
1202 PyTuple_SetItem(plot_args, 1, yarray);
1203 PyTuple_SetItem(plot_args, 2, pystring);
1204 PyObject * res = PyObject_Call(detail::_interpreter::get().s_python_function_loglog, plot_args, kwargs);
1205
1206 Py_DECREF(kwargs);
1207 Py_DECREF(plot_args);
1208 if (res)
1209 Py_DECREF(res);
1210
1211 return res;
1212 }

◆ named_plot() [1/2]

template<typename Numeric >
bool FML::UTILS::Matplotlib::named_plot ( const std::string & name,
const std::vector< Numeric > & x,
const std::vector< Numeric > & y,
const std::string & format = "" )

Definition at line 1098 of file Matplotlibcpp.h.

1101 {
1102 detail::_interpreter::get(); // the interpreter must be initialized before calling Python
1103 PyObject * kwargs = PyDict_New();
1104 PyDict_SetItemString(kwargs, "label", PyString_FromString(name.c_str()));
1105
1106 PyObject * xarray = get_array(x);
1107 PyObject * yarray = get_array(y);
1108
1109 PyObject * pystring = PyString_FromString(format.c_str());
1110
1111 PyObject * plot_args = PyTuple_New(3);
1112 PyTuple_SetItem(plot_args, 0, xarray);
1113 PyTuple_SetItem(plot_args, 1, yarray);
1114 PyTuple_SetItem(plot_args, 2, pystring);
1115
1116 PyObject * res = PyObject_Call(detail::_interpreter::get().s_python_function_plot, plot_args, kwargs);
1117
1118 Py_DECREF(kwargs);
1119 Py_DECREF(plot_args);
1120 if (res)
1121 Py_DECREF(res);
1122
1123 return res;
1124 }

◆ named_plot() [2/2]

template<typename Numeric >
bool FML::UTILS::Matplotlib::named_plot ( const std::string & name,
const std::vector< Numeric > & y,
const std::string & format = "" )

Definition at line 1073 of file Matplotlibcpp.h.

1073 {
1074 detail::_interpreter::get(); // the interpreter must be initialized before calling Python
1075 PyObject * kwargs = PyDict_New();
1076 PyDict_SetItemString(kwargs, "label", PyString_FromString(name.c_str()));
1077
1078 PyObject * yarray = get_array(y);
1079
1080 PyObject * pystring = PyString_FromString(format.c_str());
1081
1082 PyObject * plot_args = PyTuple_New(2);
1083
1084 PyTuple_SetItem(plot_args, 0, yarray);
1085 PyTuple_SetItem(plot_args, 1, pystring);
1086
1087 PyObject * res = PyObject_Call(detail::_interpreter::get().s_python_function_plot, plot_args, kwargs);
1088
1089 Py_DECREF(kwargs);
1090 Py_DECREF(plot_args);
1091 if (res)
1092 Py_DECREF(res);
1093
1094 return res;
1095 }

◆ named_semilogx()

template<typename Numeric >
bool FML::UTILS::Matplotlib::named_semilogx ( const std::string & name,
const std::vector< Numeric > & x,
const std::vector< Numeric > & y,
const std::string & format = "" )

Definition at line 1127 of file Matplotlibcpp.h.

1130 {
1131 detail::_interpreter::get(); // the interpreter must be initialized before calling Python
1132 PyObject * kwargs = PyDict_New();
1133 PyDict_SetItemString(kwargs, "label", PyString_FromString(name.c_str()));
1134
1135 PyObject * xarray = get_array(x);
1136 PyObject * yarray = get_array(y);
1137
1138 PyObject * pystring = PyString_FromString(format.c_str());
1139
1140 PyObject * plot_args = PyTuple_New(3);
1141 PyTuple_SetItem(plot_args, 0, xarray);
1142 PyTuple_SetItem(plot_args, 1, yarray);
1143 PyTuple_SetItem(plot_args, 2, pystring);
1144
1145 PyObject * res =
1146 PyObject_Call(detail::_interpreter::get().s_python_function_semilogx, plot_args, kwargs);
1147
1148 Py_DECREF(kwargs);
1149 Py_DECREF(plot_args);
1150 if (res)
1151 Py_DECREF(res);
1152
1153 return res;
1154 }

◆ named_semilogy()

template<typename Numeric >
bool FML::UTILS::Matplotlib::named_semilogy ( const std::string & name,
const std::vector< Numeric > & x,
const std::vector< Numeric > & y,
const std::string & format = "" )

Definition at line 1157 of file Matplotlibcpp.h.

1160 {
1161 detail::_interpreter::get(); // the interpreter must be initialized before calling Python
1162 PyObject * kwargs = PyDict_New();
1163 PyDict_SetItemString(kwargs, "label", PyString_FromString(name.c_str()));
1164
1165 PyObject * xarray = get_array(x);
1166 PyObject * yarray = get_array(y);
1167
1168 PyObject * pystring = PyString_FromString(format.c_str());
1169
1170 PyObject * plot_args = PyTuple_New(3);
1171 PyTuple_SetItem(plot_args, 0, xarray);
1172 PyTuple_SetItem(plot_args, 1, yarray);
1173 PyTuple_SetItem(plot_args, 2, pystring);
1174
1175 PyObject * res =
1176 PyObject_Call(detail::_interpreter::get().s_python_function_semilogy, plot_args, kwargs);
1177
1178 Py_DECREF(kwargs);
1179 Py_DECREF(plot_args);
1180 if (res)
1181 Py_DECREF(res);
1182
1183 return res;
1184 }

◆ pause()

template<typename Numeric >
void FML::UTILS::Matplotlib::pause ( Numeric interval)
inline

Definition at line 1770 of file Matplotlibcpp.h.

1770 {
1771 detail::_interpreter::get(); // the interpreter must be initialized before calling Python
1772 PyObject * args = PyTuple_New(1);
1773 PyTuple_SetItem(args, 0, PyFloat_FromDouble(interval));
1774
1775 PyObject * res = PyObject_CallObject(detail::_interpreter::get().s_python_function_pause, args);
1776 if (!res)
1777 throw std::runtime_error("Call to pause() failed.");
1778
1779 Py_DECREF(args);
1780 Py_DECREF(res);
1781 }

◆ plot() [1/9]

template<typename... Args>
bool FML::UTILS::Matplotlib::plot ( )

Definition at line 1971 of file Matplotlibcpp.h.

1971 {
1972 return true;
1973 }

◆ plot() [2/9]

template<typename A , typename B , typename... Args>
bool FML::UTILS::Matplotlib::plot ( const A & a,
const B & b,
const std::string & format,
Args... args )

Definition at line 1976 of file Matplotlibcpp.h.

1976 {
1977 return detail::plot_impl<typename detail::is_callable<B>::type>()(a, b, format) && plot(args...);
1978 }
bool plot(const std::vector< Numeric > &x, const std::vector< Numeric > &y, const std::map< std::string, std::string > &keywords)

◆ plot() [3/9]

bool FML::UTILS::Matplotlib::plot ( const std::vector< double > & x,
const std::vector< double > & y,
const std::map< std::string, std::string > & keywords )
inline

Definition at line 1993 of file Matplotlibcpp.h.

1995 {
1996 return plot<double>(x, y, keywords);
1997 }

◆ plot() [4/9]

bool FML::UTILS::Matplotlib::plot ( const std::vector< double > & x,
const std::vector< double > & y,
const std::string & format = "" )
inline

Definition at line 1985 of file Matplotlibcpp.h.

1985 {
1986 return plot<double, double>(x, y, format);
1987 }

◆ plot() [5/9]

bool FML::UTILS::Matplotlib::plot ( const std::vector< double > & y,
const std::string & format = "" )
inline

Definition at line 1989 of file Matplotlibcpp.h.

1989 {
1990 return plot<double>(y, format);
1991 }

◆ plot() [6/9]

template<typename Numeric >
bool FML::UTILS::Matplotlib::plot ( const std::vector< Numeric > & x,
const std::vector< Numeric > & y,
const std::map< std::string, std::string > & keywords )

Definition at line 407 of file Matplotlibcpp.h.

409 {
410 assert(x.size() == y.size());
411
412 // using numpy arrays
413 PyObject * xarray = get_array(x);
414 PyObject * yarray = get_array(y);
415
416 // construct positional args
417 PyObject * args = PyTuple_New(2);
418 PyTuple_SetItem(args, 0, xarray);
419 PyTuple_SetItem(args, 1, yarray);
420
421 // construct keyword args
422 PyObject * kwargs = PyDict_New();
423 for (std::map<std::string, std::string>::const_iterator it = keywords.begin(); it != keywords.end();
424 ++it) {
425 PyDict_SetItemString(kwargs, it->first.c_str(), PyString_FromString(it->second.c_str()));
426 }
427
428 PyObject * res = PyObject_Call(detail::_interpreter::get().s_python_function_plot, args, kwargs);
429
430 Py_DECREF(args);
431 Py_DECREF(kwargs);
432 if (res)
433 Py_DECREF(res);
434
435 return res;
436 }

◆ plot() [7/9]

template<typename Numeric >
bool FML::UTILS::Matplotlib::plot ( const std::vector< Numeric > & y,
const std::map< std::string, std::string > & keywords )

Definition at line 1223 of file Matplotlibcpp.h.

1223 {
1224 std::vector<Numeric> x(y.size());
1225 for (size_t i = 0; i < x.size(); ++i)
1226 x.at(i) = i;
1227 return plot(x, y, keywords);
1228 }

◆ plot() [8/9]

template<typename Numeric >
bool FML::UTILS::Matplotlib::plot ( const std::vector< Numeric > & y,
const std::string & format = "" )

Definition at line 1215 of file Matplotlibcpp.h.

1215 {
1216 std::vector<Numeric> x(y.size());
1217 for (size_t i = 0; i < x.size(); ++i)
1218 x.at(i) = i;
1219 return plot(x, y, format);
1220 }

◆ plot() [9/9]

template<typename NumericX , typename NumericY >
bool FML::UTILS::Matplotlib::plot ( const std::vector< NumericX > & x,
const std::vector< NumericY > & y,
const std::string & s = "" )

Definition at line 882 of file Matplotlibcpp.h.

882 {
883 assert(x.size() == y.size());
884
885 PyObject * xarray = get_array(x);
886 PyObject * yarray = get_array(y);
887
888 PyObject * pystring = PyString_FromString(s.c_str());
889
890 PyObject * plot_args = PyTuple_New(3);
891 PyTuple_SetItem(plot_args, 0, xarray);
892 PyTuple_SetItem(plot_args, 1, yarray);
893 PyTuple_SetItem(plot_args, 2, pystring);
894
895 PyObject * res = PyObject_CallObject(detail::_interpreter::get().s_python_function_plot, plot_args);
896
897 Py_DECREF(plot_args);
898 if (res)
899 Py_DECREF(res);
900
901 return res;
902 }

◆ plot_surface()

template<typename Numeric >
void FML::UTILS::Matplotlib::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>() )

Definition at line 443 of file Matplotlibcpp.h.

446 {
447 // We lazily load the modules here the first time this function is called
448 // because I'm not sure that we can assume "matplotlib installed" implies
449 // "mpl_toolkits installed" on all platforms, and we don't want to require
450 // it for people who don't need 3d plots.
451 static PyObject *mpl_toolkitsmod = nullptr, *axis3dmod = nullptr;
452 if (!mpl_toolkitsmod) {
453 detail::_interpreter::get();
454
455 PyObject * mpl_toolkits = PyString_FromString("mpl_toolkits");
456 PyObject * axis3d = PyString_FromString("mpl_toolkits.mplot3d");
457 if (!mpl_toolkits || !axis3d) {
458 throw std::runtime_error("couldnt create string");
459 }
460
461 mpl_toolkitsmod = PyImport_Import(mpl_toolkits);
462 Py_DECREF(mpl_toolkits);
463 if (!mpl_toolkitsmod) {
464 throw std::runtime_error("Error loading module mpl_toolkits!");
465 }
466
467 axis3dmod = PyImport_Import(axis3d);
468 Py_DECREF(axis3d);
469 if (!axis3dmod) {
470 throw std::runtime_error("Error loading module mpl_toolkits.mplot3d!");
471 }
472 }
473
474 assert(x.size() == y.size());
475 assert(y.size() == z.size());
476
477 // using numpy arrays
478 PyObject * xarray = get_2darray(x);
479 PyObject * yarray = get_2darray(y);
480 PyObject * zarray = get_2darray(z);
481
482 // construct positional args
483 PyObject * args = PyTuple_New(3);
484 PyTuple_SetItem(args, 0, xarray);
485 PyTuple_SetItem(args, 1, yarray);
486 PyTuple_SetItem(args, 2, zarray);
487
488 // Build up the kw args.
489 PyObject * kwargs = PyDict_New();
490 PyDict_SetItemString(kwargs, "rstride", PyInt_FromLong(1));
491 PyDict_SetItemString(kwargs, "cstride", PyInt_FromLong(1));
492
493 PyObject * python_colormap_coolwarm =
494 PyObject_GetAttrString(detail::_interpreter::get().s_python_colormap, "coolwarm");
495
496 PyDict_SetItemString(kwargs, "cmap", python_colormap_coolwarm);
497
498 for (std::map<std::string, std::string>::const_iterator it = keywords.begin(); it != keywords.end();
499 ++it) {
500 PyDict_SetItemString(kwargs, it->first.c_str(), PyString_FromString(it->second.c_str()));
501 }
502
503 PyObject * fig = PyObject_CallObject(detail::_interpreter::get().s_python_function_figure,
504 detail::_interpreter::get().s_python_empty_tuple);
505 if (!fig)
506 throw std::runtime_error("Call to figure() failed.");
507
508 PyObject * gca_kwargs = PyDict_New();
509 PyDict_SetItemString(gca_kwargs, "projection", PyString_FromString("3d"));
510
511 PyObject * gca = PyObject_GetAttrString(fig, "gca");
512 if (!gca)
513 throw std::runtime_error("No gca");
514 Py_INCREF(gca);
515 PyObject * axis = PyObject_Call(gca, detail::_interpreter::get().s_python_empty_tuple, gca_kwargs);
516
517 if (!axis)
518 throw std::runtime_error("No axis");
519 Py_INCREF(axis);
520
521 Py_DECREF(gca);
522 Py_DECREF(gca_kwargs);
523
524 PyObject * plot_surface = PyObject_GetAttrString(axis, "plot_surface");
525 if (!plot_surface)
526 throw std::runtime_error("No surface");
527 Py_INCREF(plot_surface);
528 PyObject * res = PyObject_Call(plot_surface, args, kwargs);
529 if (!res)
530 throw std::runtime_error("failed surface");
531 Py_DECREF(plot_surface);
532
533 Py_DECREF(axis);
534 Py_DECREF(args);
535 Py_DECREF(kwargs);
536 if (res)
537 Py_DECREF(res);
538 }
void axis(const std::string &axisstr)
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 >())
PyObject * get_2darray(const std::vector<::std::vector< Numeric > > &v)

◆ quiver()

template<typename NumericX , typename NumericY , typename NumericU , typename NumericW >
bool FML::UTILS::Matplotlib::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 at line 905 of file Matplotlibcpp.h.

909 {}) {
910 assert(x.size() == y.size() and x.size() == u.size() and u.size() == w.size());
911
912 PyObject * xarray = get_array(x);
913 PyObject * yarray = get_array(y);
914 PyObject * uarray = get_array(u);
915 PyObject * warray = get_array(w);
916
917 PyObject * plot_args = PyTuple_New(4);
918 PyTuple_SetItem(plot_args, 0, xarray);
919 PyTuple_SetItem(plot_args, 1, yarray);
920 PyTuple_SetItem(plot_args, 2, uarray);
921 PyTuple_SetItem(plot_args, 3, warray);
922
923 // construct keyword args
924 PyObject * kwargs = PyDict_New();
925 for (std::map<std::string, std::string>::const_iterator it = keywords.begin(); it != keywords.end();
926 ++it) {
927 PyDict_SetItemString(kwargs, it->first.c_str(), PyUnicode_FromString(it->second.c_str()));
928 }
929
930 PyObject * res = PyObject_Call(detail::_interpreter::get().s_python_function_quiver, plot_args, kwargs);
931
932 Py_DECREF(kwargs);
933 Py_DECREF(plot_args);
934 if (res)
935 Py_DECREF(res);
936
937 return res;
938 }
Vec u
Definition Minimizer.cpp:27

◆ save()

void FML::UTILS::Matplotlib::save ( const std::string & filename)
inline

Definition at line 1783 of file Matplotlibcpp.h.

1783 {
1784 detail::_interpreter::get(); // the interpreter must be initialized before calling Python
1785 PyObject * pyfilename = PyString_FromString(filename.c_str());
1786
1787 PyObject * args = PyTuple_New(1);
1788 PyTuple_SetItem(args, 0, pyfilename);
1789
1790 PyObject * res = PyObject_CallObject(detail::_interpreter::get().s_python_function_save, args);
1791 if (!res)
1792 throw std::runtime_error("Call to save() failed.");
1793
1794 Py_DECREF(args);
1795 Py_DECREF(res);
1796 }
const std::string filename
Definition Main.cpp:63

◆ scatter()

template<typename NumericX , typename NumericY >
bool FML::UTILS::Matplotlib::scatter ( const std::vector< NumericX > & x,
const std::vector< NumericY > & y,
const double s = 1.0,
const std::unordered_map< std::string, std::string > & keywords = {} )

Definition at line 753 of file Matplotlibcpp.h.

756 {}) {
757 assert(x.size() == y.size());
758
759 PyObject * xarray = get_array(x);
760 PyObject * yarray = get_array(y);
761
762 PyObject * kwargs = PyDict_New();
763 PyDict_SetItemString(kwargs, "s", PyLong_FromLong(s));
764 for (const auto & it : keywords) {
765 PyDict_SetItemString(kwargs, it.first.c_str(), PyString_FromString(it.second.c_str()));
766 }
767
768 PyObject * plot_args = PyTuple_New(2);
769 PyTuple_SetItem(plot_args, 0, xarray);
770 PyTuple_SetItem(plot_args, 1, yarray);
771
772 PyObject * res =
773 PyObject_Call(detail::_interpreter::get().s_python_function_scatter, plot_args, kwargs);
774
775 Py_DECREF(plot_args);
776 Py_DECREF(kwargs);
777 if (res)
778 Py_DECREF(res);
779
780 return res;
781 }

◆ semilogx()

template<typename NumericX , typename NumericY >
bool FML::UTILS::Matplotlib::semilogx ( const std::vector< NumericX > & x,
const std::vector< NumericY > & y,
const std::string & s = "" )

Definition at line 965 of file Matplotlibcpp.h.

965 {
966 assert(x.size() == y.size());
967
968 PyObject * xarray = get_array(x);
969 PyObject * yarray = get_array(y);
970
971 PyObject * pystring = PyString_FromString(s.c_str());
972
973 PyObject * plot_args = PyTuple_New(3);
974 PyTuple_SetItem(plot_args, 0, xarray);
975 PyTuple_SetItem(plot_args, 1, yarray);
976 PyTuple_SetItem(plot_args, 2, pystring);
977
978 PyObject * res = PyObject_CallObject(detail::_interpreter::get().s_python_function_semilogx, plot_args);
979
980 Py_DECREF(plot_args);
981 if (res)
982 Py_DECREF(res);
983
984 return res;
985 }

◆ semilogy()

template<typename NumericX , typename NumericY >
bool FML::UTILS::Matplotlib::semilogy ( const std::vector< NumericX > & x,
const std::vector< NumericY > & y,
const std::string & s = "" )

Definition at line 989 of file Matplotlibcpp.h.

989 {
990 assert(x.size() == y.size());
991
992 PyObject * xarray = get_array(x);
993 PyObject * yarray = get_array(y);
994
995 PyObject * pystring = PyString_FromString(s.c_str());
996
997 PyObject * plot_args = PyTuple_New(3);
998 PyTuple_SetItem(plot_args, 0, xarray);
999 PyTuple_SetItem(plot_args, 1, yarray);
1000 PyTuple_SetItem(plot_args, 2, pystring);
1001
1002 PyObject * res = PyObject_CallObject(detail::_interpreter::get().s_python_function_semilogy, plot_args);
1003
1004 Py_DECREF(plot_args);
1005 if (res)
1006 Py_DECREF(res);
1007
1008 return res;
1009 }

◆ show()

void FML::UTILS::Matplotlib::show ( const bool block = true)
inline

Definition at line 1712 of file Matplotlibcpp.h.

1712 {
1713 PyObject * res;
1714 if (block) {
1715 res = PyObject_CallObject(detail::_interpreter::get().s_python_function_show,
1716 detail::_interpreter::get().s_python_empty_tuple);
1717 } else {
1718 PyObject * kwargs = PyDict_New();
1719 PyDict_SetItemString(kwargs, "block", Py_False);
1720 res = PyObject_Call(detail::_interpreter::get().s_python_function_show,
1721 detail::_interpreter::get().s_python_empty_tuple,
1722 kwargs);
1723 Py_DECREF(kwargs);
1724 }
1725
1726 if (!res)
1727 throw std::runtime_error("Call to show() failed.");
1728
1729 Py_DECREF(res);
1730 }

◆ stem() [1/3]

template<typename Numeric >
bool FML::UTILS::Matplotlib::stem ( const std::vector< Numeric > & x,
const std::vector< Numeric > & y,
const std::map< std::string, std::string > & keywords )

Definition at line 542 of file Matplotlibcpp.h.

544 {
545 assert(x.size() == y.size());
546
547 // using numpy arrays
548 PyObject * xarray = get_array(x);
549 PyObject * yarray = get_array(y);
550
551 // construct positional args
552 PyObject * args = PyTuple_New(2);
553 PyTuple_SetItem(args, 0, xarray);
554 PyTuple_SetItem(args, 1, yarray);
555
556 // construct keyword args
557 PyObject * kwargs = PyDict_New();
558 for (std::map<std::string, std::string>::const_iterator it = keywords.begin(); it != keywords.end();
559 ++it) {
560 PyDict_SetItemString(kwargs, it->first.c_str(), PyString_FromString(it->second.c_str()));
561 }
562
563 PyObject * res = PyObject_Call(detail::_interpreter::get().s_python_function_stem, args, kwargs);
564
565 Py_DECREF(args);
566 Py_DECREF(kwargs);
567 if (res)
568 Py_DECREF(res);
569
570 return res;
571 }

◆ stem() [2/3]

template<typename Numeric >
bool FML::UTILS::Matplotlib::stem ( const std::vector< Numeric > & y,
const std::string & format = "" )

Definition at line 1231 of file Matplotlibcpp.h.

1231 {
1232 std::vector<Numeric> x(y.size());
1233 for (size_t i = 0; i < x.size(); ++i)
1234 x.at(i) = i;
1235 return stem(x, y, format);
1236 }
bool stem(const std::vector< Numeric > &x, const std::vector< Numeric > &y, const std::map< std::string, std::string > &keywords)

◆ stem() [3/3]

template<typename NumericX , typename NumericY >
bool FML::UTILS::Matplotlib::stem ( const std::vector< NumericX > & x,
const std::vector< NumericY > & y,
const std::string & s = "" )

Definition at line 941 of file Matplotlibcpp.h.

941 {
942 assert(x.size() == y.size());
943
944 PyObject * xarray = get_array(x);
945 PyObject * yarray = get_array(y);
946
947 PyObject * pystring = PyString_FromString(s.c_str());
948
949 PyObject * plot_args = PyTuple_New(3);
950 PyTuple_SetItem(plot_args, 0, xarray);
951 PyTuple_SetItem(plot_args, 1, yarray);
952 PyTuple_SetItem(plot_args, 2, pystring);
953
954 PyObject * res = PyObject_CallObject(detail::_interpreter::get().s_python_function_stem, plot_args);
955
956 Py_DECREF(plot_args);
957 if (res)
958 Py_DECREF(res);
959
960 return res;
961 }

◆ subplot()

void FML::UTILS::Matplotlib::subplot ( long nrows,
long ncols,
long plot_number )
inline

Definition at line 1530 of file Matplotlibcpp.h.

1530 {
1531 detail::_interpreter::get(); // the interpreter must be initialized before calling Python
1532 // construct positional args
1533 PyObject * args = PyTuple_New(3);
1534 PyTuple_SetItem(args, 0, PyFloat_FromDouble(nrows));
1535 PyTuple_SetItem(args, 1, PyFloat_FromDouble(ncols));
1536 PyTuple_SetItem(args, 2, PyFloat_FromDouble(plot_number));
1537
1538 PyObject * res = PyObject_CallObject(detail::_interpreter::get().s_python_function_subplot, args);
1539 if (!res)
1540 throw std::runtime_error("Call to subplot() failed.");
1541
1542 Py_DECREF(args);
1543 Py_DECREF(res);
1544 }

◆ subplot2grid()

void FML::UTILS::Matplotlib::subplot2grid ( long nrows,
long ncols,
long rowid = 0,
long colid = 0,
long rowspan = 1,
long colspan = 1 )
inline

Definition at line 1547 of file Matplotlibcpp.h.

1547 {
1548 detail::_interpreter::get(); // the interpreter must be initialized before calling Python
1549 PyObject * shape = PyTuple_New(2);
1550 PyTuple_SetItem(shape, 0, PyLong_FromLong(nrows));
1551 PyTuple_SetItem(shape, 1, PyLong_FromLong(ncols));
1552
1553 PyObject * loc = PyTuple_New(2);
1554 PyTuple_SetItem(loc, 0, PyLong_FromLong(rowid));
1555 PyTuple_SetItem(loc, 1, PyLong_FromLong(colid));
1556
1557 PyObject * args = PyTuple_New(4);
1558 PyTuple_SetItem(args, 0, shape);
1559 PyTuple_SetItem(args, 1, loc);
1560 PyTuple_SetItem(args, 2, PyLong_FromLong(rowspan));
1561 PyTuple_SetItem(args, 3, PyLong_FromLong(colspan));
1562
1563 PyObject * res = PyObject_CallObject(detail::_interpreter::get().s_python_function_subplot2grid, args);
1564 if (!res)
1565 throw std::runtime_error("Call to subplot2grid() failed.");
1566
1567 Py_DECREF(shape);
1568 Py_DECREF(loc);
1569 Py_DECREF(args);
1570 Py_DECREF(res);
1571 }
void shape(const std::set< Cell > &cells, Boundary b, double volume, double area, double h, int chi, const std::string &label)

◆ subplots_adjust()

bool FML::UTILS::Matplotlib::subplots_adjust ( const std::map< std::string, double > & keywords = {})
inline

Definition at line 834 of file Matplotlibcpp.h.

834 {}) {
835 detail::_interpreter::get(); // the interpreter must be initialized before calling Python
836 PyObject * kwargs = PyDict_New();
837 for (std::map<std::string, double>::const_iterator it = keywords.begin(); it != keywords.end(); ++it) {
838 PyDict_SetItemString(kwargs, it->first.c_str(), PyFloat_FromDouble(it->second));
839 }
840
841 PyObject * plot_args = PyTuple_New(0);
842
843 PyObject * res =
844 PyObject_Call(detail::_interpreter::get().s_python_function_subplots_adjust, plot_args, kwargs);
845
846 Py_DECREF(plot_args);
847 Py_DECREF(kwargs);
848 if (res)
849 Py_DECREF(res);
850
851 return res;
852 }

◆ suptitle()

void FML::UTILS::Matplotlib::suptitle ( const std::string & suptitlestr,
const std::map< std::string, std::string > & keywords = {} )
inline

Definition at line 1593 of file Matplotlibcpp.h.

1594 {}) {
1595 detail::_interpreter::get(); // the interpreter must be initialized before calling Python
1596 PyObject * pysuptitlestr = PyString_FromString(suptitlestr.c_str());
1597 PyObject * args = PyTuple_New(1);
1598 PyTuple_SetItem(args, 0, pysuptitlestr);
1599
1600 PyObject * kwargs = PyDict_New();
1601 for (auto it = keywords.begin(); it != keywords.end(); ++it) {
1602 PyDict_SetItemString(kwargs, it->first.c_str(), PyUnicode_FromString(it->second.c_str()));
1603 }
1604
1605 PyObject * res = PyObject_Call(detail::_interpreter::get().s_python_function_suptitle, args, kwargs);
1606 if (!res)
1607 throw std::runtime_error("Call to suptitle() failed.");
1608
1609 Py_DECREF(args);
1610 Py_DECREF(kwargs);
1611 Py_DECREF(res);
1612 }

◆ text()

template<typename Numeric >
void FML::UTILS::Matplotlib::text ( Numeric x,
Numeric y,
const std::string & s = "" )

Definition at line 1239 of file Matplotlibcpp.h.

1239 {
1240 detail::_interpreter::get(); // the interpreter must be initialized before calling Python
1241 PyObject * args = PyTuple_New(3);
1242 PyTuple_SetItem(args, 0, PyFloat_FromDouble(x));
1243 PyTuple_SetItem(args, 1, PyFloat_FromDouble(y));
1244 PyTuple_SetItem(args, 2, PyString_FromString(s.c_str()));
1245
1246 PyObject * res = PyObject_CallObject(detail::_interpreter::get().s_python_function_text, args);
1247 if (!res)
1248 throw std::runtime_error("Call to text() failed.");
1249
1250 Py_DECREF(args);
1251 Py_DECREF(res);
1252 }

◆ tick_params()

void FML::UTILS::Matplotlib::tick_params ( const std::map< std::string, std::string > & keywords,
const std::string axis = "both" )
inline

Definition at line 1505 of file Matplotlibcpp.h.

1506 {
1507 detail::_interpreter::get(); // the interpreter must be initialized before calling Python
1508 // construct positional args
1509 PyObject * args;
1510 args = PyTuple_New(1);
1511 PyTuple_SetItem(args, 0, PyString_FromString(axis.c_str()));
1512
1513 // construct keyword args
1514 PyObject * kwargs = PyDict_New();
1515 for (std::map<std::string, std::string>::const_iterator it = keywords.begin(); it != keywords.end();
1516 ++it) {
1517 PyDict_SetItemString(kwargs, it->first.c_str(), PyString_FromString(it->second.c_str()));
1518 }
1519
1520 PyObject * res = PyObject_Call(detail::_interpreter::get().s_python_function_tick_params, args, kwargs);
1521
1522 Py_DECREF(args);
1523 Py_DECREF(kwargs);
1524 if (!res)
1525 throw std::runtime_error("Call to tick_params() failed");
1526
1527 Py_DECREF(res);
1528 }

◆ tight_layout()

void FML::UTILS::Matplotlib::tight_layout ( )
inline

Definition at line 1854 of file Matplotlibcpp.h.

1854 {
1855 PyObject * res = PyObject_CallObject(detail::_interpreter::get().s_python_function_tight_layout,
1856 detail::_interpreter::get().s_python_empty_tuple);
1857
1858 if (!res)
1859 throw std::runtime_error("Call to tight_layout() failed.");
1860
1861 Py_DECREF(res);
1862 }

◆ title()

void FML::UTILS::Matplotlib::title ( const std::string & titlestr,
const std::map< std::string, std::string > & keywords = {} )
inline

Definition at line 1573 of file Matplotlibcpp.h.

1573 {}) {
1574 detail::_interpreter::get(); // the interpreter must be initialized before calling Python
1575 PyObject * pytitlestr = PyString_FromString(titlestr.c_str());
1576 PyObject * args = PyTuple_New(1);
1577 PyTuple_SetItem(args, 0, pytitlestr);
1578
1579 PyObject * kwargs = PyDict_New();
1580 for (auto it = keywords.begin(); it != keywords.end(); ++it) {
1581 PyDict_SetItemString(kwargs, it->first.c_str(), PyUnicode_FromString(it->second.c_str()));
1582 }
1583
1584 PyObject * res = PyObject_Call(detail::_interpreter::get().s_python_function_title, args, kwargs);
1585 if (!res)
1586 throw std::runtime_error("Call to title() failed.");
1587
1588 Py_DECREF(args);
1589 Py_DECREF(kwargs);
1590 Py_DECREF(res);
1591 }

◆ xkcd()

void FML::UTILS::Matplotlib::xkcd ( )
inline

Definition at line 1742 of file Matplotlibcpp.h.

1742 {
1743 detail::_interpreter::get(); // the interpreter must be initialized before calling Python
1744 PyObject * res;
1745 PyObject * kwargs = PyDict_New();
1746
1747 res = PyObject_Call(detail::_interpreter::get().s_python_function_xkcd,
1748 detail::_interpreter::get().s_python_empty_tuple,
1749 kwargs);
1750
1751 Py_DECREF(kwargs);
1752
1753 if (!res)
1754 throw std::runtime_error("Call to show() failed.");
1755
1756 Py_DECREF(res);
1757 }

◆ xlabel()

void FML::UTILS::Matplotlib::xlabel ( const std::string & str,
const std::map< std::string, std::string > & keywords = {} )
inline

Definition at line 1656 of file Matplotlibcpp.h.

1656 {}) {
1657 detail::_interpreter::get(); // the interpreter must be initialized before calling Python
1658 PyObject * pystr = PyString_FromString(str.c_str());
1659 PyObject * args = PyTuple_New(1);
1660 PyTuple_SetItem(args, 0, pystr);
1661
1662 PyObject * kwargs = PyDict_New();
1663 for (auto it = keywords.begin(); it != keywords.end(); ++it) {
1664 PyDict_SetItemString(kwargs, it->first.c_str(), PyUnicode_FromString(it->second.c_str()));
1665 }
1666
1667 PyObject * res = PyObject_Call(detail::_interpreter::get().s_python_function_xlabel, args, kwargs);
1668 if (!res)
1669 throw std::runtime_error("Call to xlabel() failed.");
1670
1671 Py_DECREF(args);
1672 Py_DECREF(kwargs);
1673 Py_DECREF(res);
1674 }

◆ xlim() [1/2]

double * FML::UTILS::Matplotlib::xlim ( )
inline

Definition at line 1371 of file Matplotlibcpp.h.

1371 {
1372 detail::_interpreter::get(); // the interpreter must be initialized before calling Python
1373 PyObject * args = PyTuple_New(0);
1374 PyObject * res = PyObject_CallObject(detail::_interpreter::get().s_python_function_xlim, args);
1375 PyObject * left = PyTuple_GetItem(res, 0);
1376 PyObject * right = PyTuple_GetItem(res, 1);
1377
1378 double * arr = new double[2];
1379 arr[0] = PyFloat_AsDouble(left);
1380 arr[1] = PyFloat_AsDouble(right);
1381
1382 if (!res)
1383 throw std::runtime_error("Call to xlim() failed.");
1384
1385 Py_DECREF(res);
1386 return arr;
1387 }

◆ xlim() [2/2]

template<typename Numeric >
void FML::UTILS::Matplotlib::xlim ( Numeric left,
Numeric right )

Definition at line 1354 of file Matplotlibcpp.h.

1354 {
1355 detail::_interpreter::get(); // the interpreter must be initialized before calling Python
1356 PyObject * list = PyList_New(2);
1357 PyList_SetItem(list, 0, PyFloat_FromDouble(left));
1358 PyList_SetItem(list, 1, PyFloat_FromDouble(right));
1359
1360 PyObject * args = PyTuple_New(1);
1361 PyTuple_SetItem(args, 0, list);
1362
1363 PyObject * res = PyObject_CallObject(detail::_interpreter::get().s_python_function_xlim, args);
1364 if (!res)
1365 throw std::runtime_error("Call to xlim() failed.");
1366
1367 Py_DECREF(args);
1368 Py_DECREF(res);
1369 }

◆ xscale()

void FML::UTILS::Matplotlib::xscale ( const std::string & xscale)
inline

Definition at line 1628 of file Matplotlibcpp.h.

1628 {
1629 detail::_interpreter::get(); // the interpreter must be initialized before calling Python
1630 PyObject * str = PyString_FromString(xscale.c_str());
1631 PyObject * args = PyTuple_New(1);
1632 PyTuple_SetItem(args, 0, str);
1633
1634 PyObject * res = PyObject_CallObject(detail::_interpreter::get().s_python_function_xscale, args);
1635 if (!res)
1636 throw std::runtime_error("Call to title() failed.");
1637
1638 Py_DECREF(args);
1639 Py_DECREF(res);
1640 }

◆ xticks() [1/2]

template<typename Numeric >
void FML::UTILS::Matplotlib::xticks ( const std::vector< Numeric > & ticks,
const std::map< std::string, std::string > & keywords )
inline

Definition at line 1451 of file Matplotlibcpp.h.

1452 {
1453 xticks(ticks, {}, keywords);
1454 }
void xticks(const std::vector< Numeric > &ticks, const std::vector< std::string > &labels={}, const std::map< std::string, std::string > &keywords={})

◆ xticks() [2/2]

template<typename Numeric >
void FML::UTILS::Matplotlib::xticks ( const std::vector< Numeric > & ticks,
const std::vector< std::string > & labels = {},
const std::map< std::string, std::string > & keywords = {} )
inline

Definition at line 1408 of file Matplotlibcpp.h.

1409 {},
1410 const std::map<std::string, std::string> & keywords = {}) {
1411 assert(labels.size() == 0 || ticks.size() == labels.size());
1412
1413 // using numpy array
1414 PyObject * ticksarray = get_array(ticks);
1415
1416 PyObject * args;
1417 if (labels.size() == 0) {
1418 // construct positional args
1419 args = PyTuple_New(1);
1420 PyTuple_SetItem(args, 0, ticksarray);
1421 } else {
1422 // make tuple of tick labels
1423 PyObject * labelstuple = PyTuple_New(labels.size());
1424 for (size_t i = 0; i < labels.size(); i++)
1425 PyTuple_SetItem(labelstuple, i, PyUnicode_FromString(labels[i].c_str()));
1426
1427 // construct positional args
1428 args = PyTuple_New(2);
1429 PyTuple_SetItem(args, 0, ticksarray);
1430 PyTuple_SetItem(args, 1, labelstuple);
1431 }
1432
1433 // construct keyword args
1434 PyObject * kwargs = PyDict_New();
1435 for (std::map<std::string, std::string>::const_iterator it = keywords.begin(); it != keywords.end();
1436 ++it) {
1437 PyDict_SetItemString(kwargs, it->first.c_str(), PyString_FromString(it->second.c_str()));
1438 }
1439
1440 PyObject * res = PyObject_Call(detail::_interpreter::get().s_python_function_xticks, args, kwargs);
1441
1442 Py_DECREF(args);
1443 Py_DECREF(kwargs);
1444 if (!res)
1445 throw std::runtime_error("Call to xticks() failed");
1446
1447 Py_DECREF(res);
1448 }

◆ ylabel()

void FML::UTILS::Matplotlib::ylabel ( const std::string & str,
const std::map< std::string, std::string > & keywords = {} )
inline

Definition at line 1676 of file Matplotlibcpp.h.

1676 {}) {
1677 detail::_interpreter::get(); // the interpreter must be initialized before calling Python
1678 PyObject * pystr = PyString_FromString(str.c_str());
1679 PyObject * args = PyTuple_New(1);
1680 PyTuple_SetItem(args, 0, pystr);
1681
1682 PyObject * kwargs = PyDict_New();
1683 for (auto it = keywords.begin(); it != keywords.end(); ++it) {
1684 PyDict_SetItemString(kwargs, it->first.c_str(), PyUnicode_FromString(it->second.c_str()));
1685 }
1686
1687 PyObject * res = PyObject_Call(detail::_interpreter::get().s_python_function_ylabel, args, kwargs);
1688 if (!res)
1689 throw std::runtime_error("Call to ylabel() failed.");
1690
1691 Py_DECREF(args);
1692 Py_DECREF(kwargs);
1693 Py_DECREF(res);
1694 }

◆ ylim() [1/2]

double * FML::UTILS::Matplotlib::ylim ( )
inline

Definition at line 1389 of file Matplotlibcpp.h.

1389 {
1390 detail::_interpreter::get(); // the interpreter must be initialized before calling Python
1391 PyObject * args = PyTuple_New(0);
1392 PyObject * res = PyObject_CallObject(detail::_interpreter::get().s_python_function_ylim, args);
1393 PyObject * left = PyTuple_GetItem(res, 0);
1394 PyObject * right = PyTuple_GetItem(res, 1);
1395
1396 double * arr = new double[2];
1397 arr[0] = PyFloat_AsDouble(left);
1398 arr[1] = PyFloat_AsDouble(right);
1399
1400 if (!res)
1401 throw std::runtime_error("Call to ylim() failed.");
1402
1403 Py_DECREF(res);
1404 return arr;
1405 }

◆ ylim() [2/2]

template<typename Numeric >
void FML::UTILS::Matplotlib::ylim ( Numeric left,
Numeric right )

Definition at line 1336 of file Matplotlibcpp.h.

1336 {
1337 detail::_interpreter::get(); // the interpreter must be initialized before calling Python
1338 PyObject * list = PyList_New(2);
1339 PyList_SetItem(list, 0, PyFloat_FromDouble(left));
1340 PyList_SetItem(list, 1, PyFloat_FromDouble(right));
1341
1342 PyObject * args = PyTuple_New(1);
1343 PyTuple_SetItem(args, 0, list);
1344
1345 PyObject * res = PyObject_CallObject(detail::_interpreter::get().s_python_function_ylim, args);
1346 if (!res)
1347 throw std::runtime_error("Call to ylim() failed.");
1348
1349 Py_DECREF(args);
1350 Py_DECREF(res);
1351 }

◆ yscale()

void FML::UTILS::Matplotlib::yscale ( const std::string & yscale)
inline

Definition at line 1642 of file Matplotlibcpp.h.

1642 {
1643 detail::_interpreter::get(); // the interpreter must be initialized before calling Python
1644 PyObject * str = PyString_FromString(yscale.c_str());
1645 PyObject * args = PyTuple_New(1);
1646 PyTuple_SetItem(args, 0, str);
1647
1648 PyObject * res = PyObject_CallObject(detail::_interpreter::get().s_python_function_yscale, args);
1649 if (!res)
1650 throw std::runtime_error("Call to title() failed.");
1651
1652 Py_DECREF(args);
1653 Py_DECREF(res);
1654 }

◆ yticks() [1/2]

template<typename Numeric >
void FML::UTILS::Matplotlib::yticks ( const std::vector< Numeric > & ticks,
const std::map< std::string, std::string > & keywords )
inline

Definition at line 1500 of file Matplotlibcpp.h.

1501 {
1502 yticks(ticks, {}, keywords);
1503 }
void yticks(const std::vector< Numeric > &ticks, const std::vector< std::string > &labels={}, const std::map< std::string, std::string > &keywords={})

◆ yticks() [2/2]

template<typename Numeric >
void FML::UTILS::Matplotlib::yticks ( const std::vector< Numeric > & ticks,
const std::vector< std::string > & labels = {},
const std::map< std::string, std::string > & keywords = {} )
inline

Definition at line 1457 of file Matplotlibcpp.h.

1458 {},
1459 const std::map<std::string, std::string> & keywords = {}) {
1460 assert(labels.size() == 0 || ticks.size() == labels.size());
1461
1462 // using numpy array
1463 PyObject * ticksarray = get_array(ticks);
1464
1465 PyObject * args;
1466 if (labels.size() == 0) {
1467 // construct positional args
1468 args = PyTuple_New(1);
1469 PyTuple_SetItem(args, 0, ticksarray);
1470 } else {
1471 // make tuple of tick labels
1472 PyObject * labelstuple = PyTuple_New(labels.size());
1473 for (size_t i = 0; i < labels.size(); i++)
1474 PyTuple_SetItem(labelstuple, i, PyUnicode_FromString(labels[i].c_str()));
1475
1476 // construct positional args
1477 args = PyTuple_New(2);
1478 PyTuple_SetItem(args, 0, ticksarray);
1479 PyTuple_SetItem(args, 1, labelstuple);
1480 }
1481
1482 // construct keyword args
1483 PyObject * kwargs = PyDict_New();
1484 for (std::map<std::string, std::string>::const_iterator it = keywords.begin(); it != keywords.end();
1485 ++it) {
1486 PyDict_SetItemString(kwargs, it->first.c_str(), PyString_FromString(it->second.c_str()));
1487 }
1488
1489 PyObject * res = PyObject_Call(detail::_interpreter::get().s_python_function_yticks, args, kwargs);
1490
1491 Py_DECREF(args);
1492 Py_DECREF(kwargs);
1493 if (!res)
1494 throw std::runtime_error("Call to yticks() failed");
1495
1496 Py_DECREF(res);
1497 }