@@ -93,6 +93,7 @@ struct _interpreter {
93
93
PyObject *s_python_function_suptitle;
94
94
PyObject *s_python_function_bar;
95
95
PyObject *s_python_function_subplots_adjust;
96
+ PyObject *s_python_function_imshow;
96
97
97
98
/* For now, _interpreter is implemented as a singleton since its currently not
98
99
possible to have multiple independent embedded python interpreters without
@@ -227,6 +228,7 @@ struct _interpreter {
227
228
s_python_function_bar = PyObject_GetAttrString (pymod, " bar" );
228
229
s_python_function_subplots_adjust =
229
230
PyObject_GetAttrString (pymod, " subplots_adjust" );
231
+ s_python_function_imshow = PyObject_GetAttrString (pymod, " imshow" );
230
232
231
233
if (!s_python_function_show || !s_python_function_close ||
232
234
!s_python_function_draw || !s_python_function_pause ||
@@ -248,7 +250,8 @@ struct _interpreter {
248
250
!s_python_function_tight_layout || !s_python_function_stem ||
249
251
!s_python_function_xkcd || !s_python_function_text ||
250
252
!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) {
252
255
throw std::runtime_error (" Couldn't find required function!" );
253
256
}
254
257
@@ -292,7 +295,9 @@ struct _interpreter {
292
295
!PyFunction_Check (s_python_function_text) ||
293
296
!PyFunction_Check (s_python_function_suptitle) ||
294
297
!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
+ ) {
296
301
throw std::runtime_error (
297
302
" Python object is unexpectedly not a PyFunction." );
298
303
}
@@ -509,10 +514,7 @@ bool plot_base(PyObject *const pyfunc, const VectorX &x, const VectorY &y,
509
514
PyTuple_SetItem (plot_args, 2 , pystring);
510
515
511
516
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);
516
518
517
519
PyObject *res = PyObject_Call (pyfunc, plot_args, kwargs);
518
520
@@ -736,11 +738,7 @@ void plot_surface(const Matrix &x, const Matrix &y, const Matrix &z,
736
738
737
739
PyDict_SetItemString (kwargs, " cmap" , python_colormap_coolwarm);
738
740
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);
744
742
745
743
PyObject *fig =
746
744
PyObject_CallObject (detail::_interpreter::get ().s_python_function_figure ,
@@ -810,11 +808,8 @@ void contour(const Matrix &x, const Matrix &y, const Matrix &z,
810
808
811
809
PyDict_SetItemString (kwargs, " cmap" , python_colormap_coolwarm);
812
810
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);
818
813
819
814
PyObject *res = PyObject_Call (
820
815
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,
843
838
844
839
// construct keyword args
845
840
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);
851
842
852
843
PyObject *res = PyObject_Call (
853
844
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,
876
867
877
868
// construct keyword args
878
869
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);
883
871
884
872
PyObject *res = PyObject_Call (
885
873
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,
913
901
914
902
// construct keyword args
915
903
PyObject *kwargs = PyDict_New ();
916
- for (std::map<std::string, std::string>::const_iterator it = keywords.begin ();
917
- it != keywords.end (); ++it) {
918
- PyDict_SetItemSt
F438
ring (kwargs, it->first .c_str (),
919
- PyUnicode_FromString (it->second .c_str ()));
920
- }
904
+ detail::process_keywords (kwargs, keywords);
921
905
922
906
PyObject *res = PyObject_Call (
923
907
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,
972
956
973
957
PyObject *kwargs = PyDict_New ();
974
958
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);
980
962
981
963
PyObject *plot_args = PyTuple_New (2 );
982
964
PyTuple_SetItem (plot_args, 0 , xarray);
@@ -1063,14 +1045,10 @@ bool bar(const std::vector<Numeric> &y, std::string ec = "black",
1063
1045
}
1064
1046
1065
1047
inline bool
1066
- subplots_adjust (const std::map<std::string, double > &keywords = {}) {
1048
+ subplots_adjust (const std::map<std::string, std::string > &keywords = {}) {
1067
1049
1068
1050
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);
1074
1052
1075
1053
PyObject *plot_args = PyTuple_New (0 );
1076
1054
@@ -1131,11 +1109,7 @@ bool quiver(const std::vector<NumericX> &x, const std::vector<NumericY> &y,
1131
1109
1132
1110
// construct keyword args
1133
1111
PyObject *kwa
10000
rgs = 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);
1139
1113
1140
1114
PyObject *res = PyObject_Call (
1141
1115
detail::_interpreter::get ().s_python_function_quiver , plot_args, kwargs);
@@ -1159,11 +1133,7 @@ void axhline(const NumericY y,
1159
1133
PyDict_SetItemString (kwargs, " y" , PyFloat_FromDouble (y));
1160
1134
1161
1135
// 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);
1167
1137
1168
1138
PyObject *res =
1169
1139
PyObject_Call (detail::_interpreter::get ().s_python_function_axhline ,
@@ -1188,11 +1158,7 @@ void axvline(const NumericX x,
1188
1158
PyDict_SetItemString (kwargs, " x" , PyFloat_FromDouble (x));
1189
1159
1190
1160
// 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);
1196
1162
1197
1163
PyObject *res =
1198
1164
PyObject_Call (detail::_interpreter::get ().s_python_function_axvline ,
@@ -1242,11 +1208,7 @@ bool errorbar(const VectorX &x, const VectorY &y, const VectorY &yerr,
1242
1208
1243
1209
// construct keyword args
1244
1210
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);
1250
1212
1251
1213
PyDict_SetItemString (kwargs, " yerr" , yerrarray);
1252
1214
@@ -1538,11 +1500,7 @@ inline void xticks(const std::vector<Numeric> &ticks,
1538
1500
1539
1501
// construct keyword args
1540
1502
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);
1546
1504
1547
1505
PyObject *res = PyObject_Call (
1548
1506
detail::_interpreter::get ().s_python_function_xticks , args, kwargs);
@@ -1589,11 +1547,7 @@ inline void yticks(const std::vector<Numeric> &ticks,
1589
1547
1590
1548
// construct keyword args
1591
1549
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);
1597
1551
1598
1552
PyObject *res = PyObject_Call (
1599
1553
detail::_interpreter::get ().s_python_function_yticks , args, kwargs);
@@ -1639,10 +1593,7 @@ inline void title(const std::string &titlestr,
1639
1593
PyTuple_SetItem (args, 0 , pytitlestr);
1640
1594
1641
1595
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);
1646
1597
1647
1598
PyObject *res = PyObject_Call (
1648
1599
detail::_interpreter::get ().s_python_function_title , args, kwargs);
@@ -1663,10 +1614,7 @@ inline void suptitle(const std::string &suptitlestr,
1663
1614
PyTuple_SetItem (args, 0 , pysuptitlestr);
1664
1615
1665
1616
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);
1670
1618
1671
1619
PyObject *res = PyObject_Call (
1672
1620
detail::_interpreter::get ().s_python_function_suptitle , args, kwargs);
@@ -1699,10 +1647,7 @@ inline void xlabel(const std::string &str,
1699
1647
PyTuple_SetItem (args, 0 , pystr);
1700
1648
1701
1649
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);
1706
1651
1707
1652
PyObject *res = PyObject_Call (
1708
1653
detail::_interpreter::get ().s_python_function_xlabel , args, kwargs);
@@ -1721,10 +1666,7 @@ inline void ylabel(const std::string &str,
1721
1666
PyTuple_SetItem (args, 0 , pystr);
1722
1667
1723
1668
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);
1728
1670
1729
1671
PyObject *res = PyObject_Call (
1730
1672
detail::_interpreter::get ().s_python_function_ylabel , args, kwargs);
@@ -1831,10 +1773,7 @@ inline void savefig(const std::string &filename,
1831
1773
PyTuple_SetItem (args, 0 , pyfilename);
1832
1774
1833
1775
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);
1838
1777
1839
1778
PyObject *res = PyObject_Call (
1840
1779
detail::_interpreter::get ().s_python_function_save , args, kwargs);
@@ -1881,11 +1820,7 @@ ginput(const int numClicks = 1,
1881
1820
1882
1821
// construct keyword args
1883
1822
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);
1889
1824
1890
1825
PyObject *res = PyObject_Call (
1891
1826
detail::_interpreter::get ().s_python_function_ginput , args, kwargs);
0 commit comments