8000 try with std::ss · Tucker-Coughlin/matplotlib-cpp@85ba26c · GitHub
[go: up one dir, main page]

Skip to content

Commit 85ba26c

Browse files
committed
try with std::ss
1 parent bff24dd commit 85ba26c

File tree

1 file changed

+31
-96
lines changed

1 file changed

+31
-96
lines changed

matplotlibcpp.h

Lines changed: 31 additions & 96 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ struct _interpreter {
9393
PyObject *s_python_function_suptitle;
9494
PyObject *s_python_function_bar;
9595
PyObject *s_python_function_subplots_adjust;
96+
PyObject *s_python_function_imshow;
9697

9798
/* For now, _interpreter is implemented as a singleton since its currently not
9899
possible to have multiple independent embedded python interpreters without
@@ -227,6 +228,7 @@ struct _interpreter {
227228
s_python_function_bar = PyObject_GetAttrString(pymod, "bar");
228229
s_python_function_subplots_adjust =
229230
PyObject_GetAttrString(pymod, "subplots_adjust");
231+
s_python_function_imshow = PyObject_GetAttrString(pymod, "imshow");
230232

231233
if (!s_python_function_show || !s_python_function_close ||
232234
!s_python_function_draw || !s_python_function_pause ||
@@ -248,7 +250,8 @@ struct _interpreter {
248250
!s_python_function_tight_layout || !s_python_function_stem ||
249251
!s_python_function_xkcd || !s_python_function_text ||
250252
!s_python_function_suptitle || !s_python_function_bar ||
251-
!s_python_function_subplots_adjust || !s_python_function_spy) {
253+
!s_python_function_subplots_adjust || !s_python_function_spy ||
254+
!s_python_function_imshow) {
252255
throw std::runtime_error("Couldn't find required function!");
253256
}
254257

@@ -292,7 +295,9 @@ struct _interpreter {
292295
!PyFunction_Check(s_python_function_text) ||
293296
!PyFunction_Check(s_python_function_suptitle) ||
294297
!PyFunction_Check(s_python_function_bar) ||
295-
!PyFunction_Check(s_python_function_subplots_adjust)) {
298+
!PyFunction_Check(s_python_function_subplots_adjust)
299+
!PyFunction_Check(s_python_function_imshow)
300+
) {
296301
throw std::runtime_error(
297302
"Python object is unexpectedly not a PyFunction.");
298303
}
@@ -509,10 +514,7 @@ bool plot_base(PyObject *const pyfunc, const VectorX &x, const VectorY &y,
509514
PyTuple_SetItem(plot_args, 2, pystring);
510515

511516
PyObject *kwargs = PyDict_New();
512-
for (auto const &item : keywords) {
513-
PyDict_SetItemString(kwargs, item.first.c_str(),
514-
PyString_FromString(item.second.c_str()));
515-
}
517+
detail::process_keywords(kwargs, keywords);
516518

517519
PyObject *res = PyObject_Call(pyfunc, plot_args, kwargs);
518520

@@ -736,11 +738,7 @@ void plot_surface(const Matrix &x, const Matrix &y, const Matrix &z,
736738

737739
PyDict_SetItemString(kwargs, "cmap", python_colormap_coolwarm);
738740

739-
for (std::map<std::string, std::string>::const_iterator it = keywords.begin();
740-
it != keywords.end(); ++it) {
741-
PyDict_SetItemString(kwargs, it->first.c_str(),
742-
PyString_FromString(it->second.c_str()));
743-
}
741+
detail::process_keywords(kwargs, keywords);
744742

745743
PyObject *fig =
746744
PyObject_CallObject(detail::_interpreter::get().s_python_function_figure,
@@ -810,11 +808,8 @@ void contour(const Matrix &x, const Matrix &y, const Matrix &z,
810808

811809
PyDict_SetItemString(kwargs, "cmap", python_colormap_coolwarm);
812810

813-
for (std::map<std::string, std::string>::const_iterator it = keywords.begin();
814-
it != keywords.end(); ++it) {
815-
PyDict_SetItemString(kwargs, it->first.c_str(),
816-
PyString_FromString(it->second.c_str()));
817-
}
811+
// add keywords to kwargs
812+
detail::process_keywords(kwargs, keywords);
818813

819814
PyObject *res = PyObject_Call(
820815
detail::_interpreter::get().s_python_function_contour, args, kwargs);
@@ -843,11 +838,7 @@ bool stem(const std::vector<Numeric> &x, const std::vector<Numeric> &y,
843838

844839
// construct keyword args
845840
PyObject *kwargs = PyDict_New();
846-
for (std::map<std::string, std::string>::const_iterator it = keywords.begin();
847-
it != keywords.end(); ++it) {
848-
PyDict_SetItemString(kwargs, it->first.c_str(),
849-
PyString_FromString(it->second.c_str()));
850-
}
841+
detail::process_keywords(kwargs, keywords);
851842

852843
PyObject *res = PyObject_Call(
853844
detail::_interpreter::get().s_python_function_stem, args, kwargs);
@@ -876,10 +867,7 @@ bool fill(const std::vector<Numeric> &x, const std::vector<Numeric> &y,
876867

877868
// construct keyword args
878869
PyObject *kwargs = PyDict_New();
879-
for (auto it = keywords.begin(); it != keywords.end(); ++it) {
880-
PyDict_SetItemString(kwargs, it->first.c_str(),
881-
PyUnicode_FromString(it->second.c_str()));
882-
}
870+
detail::process_keywords(kwargs, keywords);
883871

884872
PyObject *res = PyObject_Call(
885873
detail::_interpreter::get().s_python_function_fill, args, kwargs);
@@ -913,11 +901,7 @@ bool fill_between(const std::vector<Numeric> &x, const std::vector<Numeric> &y1,
913901

914902
// construct keyword args
915903
PyObject *kwargs = PyDict_New();
916-
for (std::map<std::string, std::string>::const_iterator it = keywords.begin();
917-
it != keywords.end(); ++it) {
918-
PyDict_SetItemString(kwargs, it->first.c_str(),
919-
PyUnicode_FromString(it->second.c_str()));
920-
}
904+
detail::process_keywords(kwargs, keywords);
921905

922906
PyObject *res = PyObject_Call(
923907
detail::_interpreter::get().s_python_function_fill_between, args, kwargs);
@@ -972,11 +956,9 @@ bool scatter(const VectorX &x, const VectorY &y, const double s = 1.0,
972956

973957
PyObject *kwargs = PyDict_New();
974958
PyDict_SetItemString(kwargs, "s", PyFloat_FromDouble(s));
975-
for (std::map<std::string, std::string>::const_iterator it = keywords.begin();
976-
it != keywords.end(); ++it) {
977-
PyDict_SetItemString(kwargs, it->first.c_str(),
978-
PyUnicode_FromString(it->second.c_str()));
979-
}
959+
960+
// add keywords to kwargs
961+
detail::process_keywords(kwargs, keywords);
980962

981963
PyObject *plot_args = PyTuple_New(2);
982964
PyTuple_SetItem(plot_args, 0, xarray);
@@ -1063,14 +1045,10 @@ bool bar(const std::vector<Numeric> &y, std::string ec = "black",
10631045
}
10641046

10651047
inline bool
1066-
subplots_adjust(const std::map<std::string, double> &keywords = {}) {
1048+
subplots_adjust(const std::map<std::string, std::string> &keywords = {}) {
10671049

10681050
PyObject *kwargs = PyDict_New();
1069-
for (std::map<std::string, double>::const_iterator it = keywords.begin();
1070-
it != keywords.end(); ++it) {
1071-
PyDict_SetItemString(kwargs, it->first.c_str(),
1072-
PyFloat_FromDouble(it->second));
1073-
}
1051+
detail::process_keywords(kwargs, keywords);
10741052

10751053
PyObject *plot_args = PyTuple_New(0);
10761054

@@ -1131,11 +1109,7 @@ bool quiver(const std::vector<NumericX> &x, const std::vector<NumericY> &y,
11311109

11321110
// construct keyword args
11331111
PyObject *kwargs = PyDict_New();
1134-
for (std::map<std::string, std::string>::const_iterator it = keywords.begin();
1135-
it != keywords.end(); ++it) {
1136-
PyDict_SetItemString(kwargs, it->first.c_str(),
1137-
PyUnicode_FromString(it->second.c_str()));
1138-
}
1112+
detail::process_keywords(kwargs, keywords);
11391113

11401114
PyObject *res = PyObject_Call(
11411115
detail::_interpreter::get().s_python_function_quiver, plot_args, kwargs);
@@ -1159,11 +1133,7 @@ void axhline(const NumericY y,
11591133
PyDict_SetItemString(kwargs, "y", PyFloat_FromDouble(y));
11601134

11611135
// add other keywords
1162-
for (std::map<std::string, std::string>::const_iterator it = keywords.begin();
1163-
it != keywords.end(); ++it) {
1164-
PyDict_SetItemString(kwargs, it->first.c_str(),
1165-
PyUnicode_FromString(it->second.c_str()));
1166-
}
1136+
detail::process_keywords(kwargs, keywords);
11671137

11681138
PyObject *res =
11691139
PyObject_Call(detail::_interpreter::get().s_python_function_axhline,
@@ -1188,11 +1158,7 @@ void axvline(const NumericX x,
11881158
PyDict_SetItemString(kwargs, "x", PyFloat_FromDouble(x));
11891159

11901160
// add other keywords
1191-
for (std::map<std::string, std::string>::const_iterator it = keywords.begin();
1192-
it != keywords.end(); ++it) {
1193-
PyDict_SetItemString(kwargs, it->first.c_str(),
1194-
PyUnicode_FromString(it->second.c_str()));
1195-
}
1161+
detail::process_keywords(kwargs, keywords);
11961162

11971163
PyObject *res =
11981164
PyObject_Call(detail::_interpreter::get().s_python_function_axvline,
@@ -1242,11 +1208,7 @@ bool errorbar(const VectorX &x, const VectorY &y, const VectorY &yerr,
12421208

12431209
// construct keyword args
12441210
PyObject *kwargs = PyDict_New();
1245-
for (std::map<std::string, std::string>::const_iterator it = keywords.begin();
1246-
it != keywords.end(); ++it) {
1247-
PyDict_SetItemString(kwargs, it->first.c_str(),
1248-
PyString_FromString(it->second.c_str()));
1249-
}
1211+
detail::process_keywords(kwargs, keywords);
12501212

12511213
PyDict_SetItemString(kwargs, "yerr", yerrarray);
12521214

@@ -1538,11 +1500,7 @@ inline void xticks(const std::vector<Numeric> &ticks,
15381500

15391501
// construct keyword args
15401502
PyObject *kwargs = PyDict_New();
1541-
for (std::map<std::string, std::string>::const_iterator it = keywords.begin();
1542-
it != keywords.end(); ++it) {
1543-
PyDict_SetItemString(kwargs, it->first.c_str(),
1544-
PyString_FromString(it->second.c_str()));
1545-
}
1503+
detail::process_keywords(kwargs, keywords);
15461504

15471505
PyObject *res = PyObject_Call(
15481506
detail::_interpreter::get().s_python_function_xticks, args, kwargs);
@@ -1589,11 +1547,7 @@ inline void yticks(const std::vector<Numeric> &ticks,
15891547

15901548
// construct keyword args
15911549
PyObject *kwargs = PyDict_New();
1592-
for (std::map<std::string, std::string>::const_iterator it = keywords.begin();
1593-
it != keywords.end(); ++it) {
1594-
PyDict_SetItemString(kwargs, it->first.c_str(),
1595-
PyString_FromString(it->second.c_str()));
1596-
}
1550+
detail::process_keywords(kwargs, keywords);
15971551

15981552
PyObject *res = PyObject_Call(
15991553
detail::_interpreter::get().s_python_function_yticks, args, kwargs);
@@ -1639,10 +1593,7 @@ inline void title(const std::string &titlestr,
16391593
PyTuple_SetItem(args, 0, pytitlestr);
16401594

16411595
PyObject *kwargs = PyDict_New();
1642-
for (auto it = keywords.begin(); it != keywords.end(); ++it) {
1643-
PyDict_SetItemString(kwargs, it->first.c_str(),
1644-
PyUnicode_FromString(it->second.c_str()));
1645-
}
1596+
detail::process_keywords(kwargs, keywords);
16461597

16471598
PyObject *res = PyObject_Call(
16481599
detail::_interpreter::get().s_python_function_title, args, kwargs);
@@ -1663,10 +1614,7 @@ inline void suptitle(const std::string &suptitlestr,
16631614
PyTuple_SetItem(args, 0, pysuptitlestr);
16641615

16651616
PyObject *kwargs = PyDict_New();
1666-
for (auto it = keywords.begin(); it != keywords.end(); ++it) {
1667-
PyDict_SetItemString(kwargs, it->first.c_str(),
1668-
PyUnicode_FromString(it->second.c_str()));
1669-
}
1617+
detail::process_keywords(kwargs, keywords);
16701618

16711619
PyObject *res = PyObject_Call(
16721620
detail::_interpreter::get().s_python_function_suptitle, args, kwargs);
@@ -1699,10 +1647,7 @@ inline void xlabel(const std::string &str,
16991647
PyTuple_SetItem(args, 0, pystr);
17001648

17011649
PyObject *kwargs = PyDict_New();
1702-
for (auto it = keywords.begin(); it != keywords.end(); ++it) {
1703-
PyDict_SetItemString(kwargs, it->first.c_str(),
1704-
PyUnicode_FromString(it->second.c_str()));
1705-
}
1650+
detail::process_keywords(kwargs, keywords);
17061651

17071652
PyObject *res = PyObject_Call(
17081653
detail::_interpreter::get().s_python_function_xlabel, args, kwargs);
@@ -1721,10 +1666,7 @@ inline void ylabel(const std::string &str,
17211666
PyTuple_SetItem(args, 0, pystr);
17221667

17231668
PyObject *kwargs = PyDict_New();
1724-
for (auto it = keywords.begin(); it != keywords.end(); ++it) {
1725-
PyDict_SetItemString(kwargs, it->first.c_str(),
1726-
PyUnicode_FromString(it->second.c_str()));
1727-
}
1669+
detail::process_keywords(kwargs, keywords);
17281670

17291671
PyObject *res = PyObject_Call(
17301672
detail::_interpreter::get().s_python_function_ylabel, args, kwargs);
@@ -1831,10 +1773,7 @@ inline void savefig(const std::string &filename,
18311773
PyTuple_SetItem(args, 0, pyfilename);
18321774

18331775
PyObject *kwargs = PyDict_New();
1834-
for (auto it = keywords.begin(); it != keywords.end(); ++it) {
1835-
PyDict_SetItemString(kwargs, it->first.c_str(),
1836-
PyUnicode_FromString(it->second.c_str()));
1837-
}
1776+
detail::process_keywords(kwargs, keywords);
18381777

18391778
PyObject *res = PyObject_Call(
18401779
detail::_interpreter::get().s_python_function_save, args, kwargs);
@@ -1881,11 +1820,7 @@ ginput(const int numClicks = 1,
18811820

18821821
// construct keyword args
18831822
PyObject *kwargs = PyDict_New();
1884-
for (std::map<std::string, std::string>::const_iterator it = keywords.begin();
1885-
it != keywords.end(); ++it) {
1886-
PyDict_SetItemString(kwargs, it->first.c_str(),
1887-
PyUnicode_FromString(it->second.c_str()));
1888-
}
1823+
detail::process_keywords(kwargs, keywords);
18891824

18901825
PyObject *res = PyObject_Call(
18911826
detail::_interpreter::get().s_python_function_ginput, args, kwargs);

0 commit comments

Comments
 (0)
0